https://github.com/devisjs/devispattern
A fast native pattern matcher addon on JavaScript object properties
https://github.com/devisjs/devispattern
addon javascript register
Last synced: 6 months ago
JSON representation
A fast native pattern matcher addon on JavaScript object properties
- Host: GitHub
- URL: https://github.com/devisjs/devispattern
- Owner: Devisjs
- License: mit
- Created: 2016-09-03T13:14:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2025-07-21T09:03:17.000Z (8 months ago)
- Last Synced: 2025-08-09T04:36:11.929Z (8 months ago)
- Topics: addon, javascript, register
- Language: C++
- Homepage:
- Size: 328 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# DevisPattern

>A fast native pattern matcher addon on JavaScript object properties.
This addon is used by the devis framework to pattern match actions.
**Requirements:**
It's necessary, before using DevisPattern to install:
- [CMake](http://www.cmake.org/download/)(*.msi version for windows: You must check the addition of the path for all users, And restart your computer after installation)
- A proper C/C++ compiler toolchain of the given platform
- **Windows**:
- [Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools)
or a recent version of Visual C++ will do ([the free Community](https://www.visualstudio.com/products/visual-studio-community-vs) version works well)
- **Unix/linux-gnu**:
- Clang or GCC
- Ninja or Make (Ninja will be picked if both present)
- Xcode with command line tools if you are under **mac os**
## Install
```sh
npm install devisPattern
```
Or, if you download the project:
Generate the appropriate project build files for the current platform. Use `configure` for that:
```sh
node-gyp configure
```
Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file (on Windows) in the `build/` directory. Next invoke the build command:
```sh
node-gyp build
```
### Quick example
Here's how you register some patterns, and then search for matches:
```JavaScript
const devisPattern =require("devisPattern"); //or require("./devisPattern/devisPattern"); if you download the project
devisPattern.add({
action: 'game',
cmd: 'play'
}, (args, done) => {
done({
result: 'play'
});
});
devisPattern.add({
action: 'game',
cmd: 'pause'
}, (args, done)=> {
done({
result: 'pause'
});
});
devisPattern.call({
action: 'game',
cmd: 'play'
}, (result)=> {
console.log(result);
});
devisPattern.call({
action: 'game',
cmd: 'pause'
}, (result) =>{
console.log(result);
});
```
# The Why
This addon lets you build a simple decision tree so you can avoid writing if statements. It tries to make the minimum number of comparisons necessary to pick out the most specific match.
# API
### devisPattern
Generates a new pattern matcher instance.
### .add( {...pattern...}, object )
Register a pattern, and the object that will be returned if an input
matches. Both keys and values are considered to be strings. Other
types are converted to strings.
### .find( {...pattern...})
Return the unique match for this pattern, or null if not found.
### .call({...pattern...},{...arguments...},callback)
Act this pattern.
### .list( )
Return the list of registered patterns that contain this partial
pattern.