Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on:
push:
branches: [master]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [22, 24]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.DS_Store
.vscode
node_modules
build
8 changes: 0 additions & 8 deletions .snyk

This file was deleted.

18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

19 changes: 0 additions & 19 deletions CMakeLists.txt

This file was deleted.

113 changes: 34 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,126 +1,81 @@
# DevisPattern
<img src="https://avatars3.githubusercontent.com/u/21971184?v=4&amp;s=200" href="http://devisjs.surge.sh" width="250" />

>A fast native pattern matcher addon on JavaScript object properties.

>An exact-match pattern matcher on JavaScript object properties.

<p align="center">
<a href="https://www.npmjs.com/package/devisPattern"><img src="https://img.shields.io/npm/v/devisPattern.svg" alt="Version"></a>
<a href="https://www.npmjs.com/package/devisPattern"><img src="https://img.shields.io/npm/l/devisPattern.svg" alt="License"></a>
<a href="https://www.bithound.io/github/Devisjs/devisPattern"><img src="https://www.bithound.io/github/Devisjs/devisPattern/badges/score.svg" alt="bitHound Overall Score"></a>
<a href="https://www.bithound.io/github/Devisjs/devisPattern"><img src="https://www.bithound.io/github/Devisjs/devisPattern/badges/code.svg" alt="bitHound Overall Score"></a>
<a href="https://travis-ci.org/Devisjs/devisPattern"><img src="https://travis-ci.org/Devisjs/devisPattern.svg?branch=master" alt="travis"></a>

<br>

<a href="https://github.com/Devisjs/devisPattern/actions/workflows/ci.yml"><img src="https://github.com/Devisjs/devisPattern/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
</p>

This module is used by the [devis](https://github.com/Devisjs/devis) framework to route actions to their handlers.

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**


Since 2.0.0 it is plain JavaScript: no compiler, CMake or native build is needed. Node.js 22 or later is required.

## 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
const DevisPattern = require("devisPattern");
const devisPattern = new DevisPattern();

devisPattern.add({
action: 'game',
cmd: 'play'
action: "game",
cmd: "play"
}, (args, done) => {

done({
result: 'play'
});
});
devisPattern.add({
action: 'game',
cmd: 'pause'
}, (args, done)=> {

done({
result: 'pause'
result: "play"
});
});

devisPattern.call({
action: 'game',
cmd: 'play'
}, (result)=> {

console.log(result);

action: "game",
cmd: "play"
}, (result) => {
console.log(result); // { result: 'play' }
});
```

devisPattern.call({
action: 'game',
cmd: 'pause'
}, (result) =>{

## Matching rules

console.log(result);
});
A pattern matches only an identical pattern: same keys, same values, same key order. Patterns are compared by their `JSON.stringify` form, so `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` are different patterns, and `{ a: 1 }` does not match `{ a: 1, b: 2 }`.

```
When the same pattern is added twice, the first handler is kept.

## API

# The Why
### new DevisPattern()

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.
Creates a matcher. Each instance has its own patterns.

# API
### .add( {...pattern...}, handler )

### devisPattern
Registers a pattern and the handler returned when an identical pattern is looked up. Returns the instance.

Generates a new pattern matcher instance.
### .find( {...pattern...} )

Returns the handler registered for this pattern, or `null`.

### .add( {...pattern...}, object )
### .call( {...pattern...}, [{...arguments...}], callback )

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.
Calls the handler registered for this pattern with `(arguments, callback)`. `arguments` defaults to `{}`. Does nothing when no handler matches.

### .find( {...pattern...})
### .list( )

Return the unique match for this pattern, or null if not found.
Returns the registered patterns as a JSON array string.

### .call({...pattern...},{...arguments...},callback)
## Upgrading from 1.x

Act this pattern.
- The native addon is gone, so installing no longer needs CMake or a C++ toolchain.
- Instances no longer share a single pattern table. In 1.x, two instances could resolve each other's patterns to the wrong handler.
- Node.js 22 or later is required.

### .list( )
## License

Return the list of registered patterns that contain this partial
pattern.
MIT
17 changes: 8 additions & 9 deletions devisPattern.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/* Copyright (c) 2016 Devis, MIT License */
"use strict";
const Addon = require("./build/Release/addon");
const obj = new Addon.devisPattern();

class devisPattern {

constructor() {
this.path = [];
this.id = 0;
this.index = new Map();
}

add(path, handle) {
this.path[this.id] = handle;
//path = jsonSort(path);
obj.add(JSON.stringify(path), this.id);
const key = JSON.stringify(path);
// First registration wins, as with the former native addon.
if (!this.index.has(key)) {this.index.set(key, this.id);}
this.id++;
return this;
}

find(path) {
//path = jsonSort(path);

let id = obj.find(JSON.stringify(path));
if (id !== -1) {return this.path[id];}
const id = this.index.get(JSON.stringify(path));
if (id !== undefined) {return this.path[id];}
else {return null;}
}

call(path, args, callback) {
if (!callback) {
callback = args;
Expand All @@ -36,7 +35,7 @@ class devisPattern {
}

list() {
return obj.list();
return "[" + [...this.index.keys()].join(",") + "]";
}

}
Expand Down
Loading
Loading