@hecom/specials
Version:
A tool to manage general or special handle.
162 lines (131 loc) • 4.84 kB
Markdown
[](https://www.npmjs.com/package/@hecom/specials)
[](https://app.travis-ci.com/hecom-rn/specials)
A tool to manage general or special handle.
Install by Yarn:
```shell
yarn add @hecom/specials
```
Install by NPM:
```shell
npm install --save @hecom/specials
```
Import the module in file:
```javascript
import * as Specials from '@hecom/specials';
```
We use a root object to store all information. In each node, we can store a default handle and an array of special handles.
* `Specials.DEFAULT_HANDLE`: Key for default handle.
* `Specials.SPECIAL_PART`: Key for part of special handle.
Here is an example of a root object:
```
{
[]: function f0 {.....},
display: {
text: {
[]: function f1 {.....},
phone: {
[]: function f4 {.....},
},
},
[]: [
{
[]: state => state === 'user',
[]: function f2 {...},
[]: 1,
[]: 0,
},
{
[]: state => state === 'org',
[]: function f3 {...},
[]: 2,
[]: 1,
}
],
},
edit: {
...
},
}
```
Each special part consists of four keys:
* `Specials.kId`: Identifier of handle. Used for unregister the handle.
* `Specials.kSpecial`: Special judging function. Returns a boolean.
* `Specials.kHandle`: Handle function when special function is valid.
* `Specials.kPriority`: Prority of handle when multi items are valid. Larger one will be used first. You can use `Specials.PRIORITY`.
* `register: (obj, path, special, handle, priority) => number | void`: Register a default or special handle with priority. Return `handleId` if `special` is not `null`.
* `unregister: (obj, path, handleId) => boolean`: Unregister a default or special handle. Return true when succeed.
* `get: (obj, path, state, params) => any`: Get handle with a `state`. Then uses handle function with `params` or just returns the handle object.
First, initialize a root object:
```javascript
const rootNode = {};
```
Register different handle:
```javascript
Specials.register(rootNode, [], undefined, y => y);
Specials.register(rootNode, 'display', undefined, y => y + 1);
Specials.register(rootNode, ['display', 'text'], undefined, y => y + 2);
Specials.register(rootNode, 'display', x => x === 1, y => y - 1); // return: 1
Specials.register(rootNode, ['display', 'text'], x => x === 2, 'P1'); // return: 1
Specials.register(rootNode, ['display', 'text'], x => x === 2, 'P2', Specials.PRIORITY.HIGH); // return: 2
```
The root object will be:
```
{
[]: y => y,
display: {
[]: y => y + 1,
text: {
[]: y => y + 2,
[]: [
{
[]: x => x === 2,
[]: 'P1',
[]: 1,
[]: 0,
},
{
[]: x => x === 2,
[]: 'P2',
[]: 2,
[]: 255,
},
],
},
[]: [
{
[]: x => x === 1,
[]: y = y - 1,
[]: 1,
[]: 0,
},
],
},
}
```
Then use `get` to get handle:
```javascript
Specials.get(rootNode, [], undefined, undefined); // function: y => y
Specials.get(rootNode, [], undefined, 100); // y=>y result: 100
Specials.get(rootNode, ['display'], undefined, 100); // y=>y+1 result: 101
Specials.get(rootNode, ['parse'], undefined, 100); // y=>y result: 100
Specials.get(rootNode, ['display'], 1, 100); // y=>y-1 result: 99
Specials.get(rootNode, ['display'], 2, 100); // y=>y+1 result: 101
Specials.get(rootNode, ['display', 'text'], 1, 100); // y=y-1 result: 99
Specials.get(rootNode, ['display', 'text'], 2, 100); // 'P2' result: 'P2'
```
Finally we can unregister default or special handle:
```javascript
Specials.unregister(rootNode, []);
Specials.unregister(rootNode, 'display');
Specials.unregister(rootNode, ['display', 'text']);
Specials.unregister(rootNode, 'display', 1); // return: true
Specials.unregister(rootNode, ['display', 'text'], 1); // return: true
Specials.unregister(rootNode, ['display', 'text'], 2); // return: true
Specials.unregister(rootNode, ['display', 'text'], 3); // return: false
```