|
1 | 1 | # @himenon/node-lib-template
|
2 | 2 |
|
| 3 | +Data structure management library that extends Composite Pattern to Path orientation. |
| 4 | + |
3 | 5 | ## Usage
|
4 | 6 |
|
5 |
| -| scripts | description | |
6 |
| -| :------------------------ | :------------------------------------------ | |
7 |
| -| `build` | typescript build and create proxy directory | |
8 |
| -| `clean` | clean up | |
9 |
| -| `format:code` | prettier | |
10 |
| -| `format:yarn:lock` | yarn.lock deduplicate | |
11 |
| -| `lerna:version:up` | lerna version up | |
12 |
| -| `test` | execute test:depcruise, test:jest | |
13 |
| -| `test:depcruise` | dependency-cruiser's test | |
14 |
| -| `test:jest` | jest test | |
15 |
| -| `ts` | execute ts-node | |
16 |
| -| `release:github:registry` | publish github registry | |
17 |
| -| `release:npm:registry` | publish npm registry | |
| 7 | +### Basic Usage |
| 8 | + |
| 9 | +```ts |
| 10 | +import { Operator, Node } from "@himenon/path-oriented-data-structure"; |
| 11 | + |
| 12 | +const operator = new Operator("tree"); |
| 13 | + |
| 14 | +const NODE_KIND_A = "node_a" as const; |
| 15 | +const NODE_KIND_B = "node_b" as const; |
| 16 | + |
| 17 | +operator.set("a", new Node(NODE_KIND_A, "node_a1")); |
| 18 | +operator.set("a/b", new Node(NODE_KIND_A, "node_a2")); |
| 19 | +operator.set("a/b/c", new Node(NODE_KIND_A, "node_A3")); |
| 20 | + |
| 21 | +operator.set("a", new Node(NODE_KIND_B, "node_b1")); |
| 22 | +operator.set("a/b", new Node(NODE_KIND_B, "node_b2")); |
| 23 | +operator.set("a/b/c", new Node(NODE_KIND_B, "node_b3")); |
| 24 | + |
| 25 | +operator.getHierarchy(); // Results below |
| 26 | +``` |
| 27 | + |
| 28 | +<details> |
| 29 | +<summary>Result: operator.getHierarchy()</summary> |
| 30 | +<code> |
| 31 | +<pre> |
| 32 | +{ |
| 33 | + "name": ".", |
| 34 | + "children": { |
| 35 | + "node_a:a": { |
| 36 | + "name": "node_a1" |
| 37 | + }, |
| 38 | + "tree:a": { |
| 39 | + "name": "a", |
| 40 | + "children": { |
| 41 | + "node_a:b": { |
| 42 | + "name": "node_a2" |
| 43 | + }, |
| 44 | + "tree:b": { |
| 45 | + "name": "b", |
| 46 | + "children": { |
| 47 | + "node_a:c": { |
| 48 | + "name": "node_A3" |
| 49 | + }, |
| 50 | + "node_b:c": { |
| 51 | + "name": "node_b3" |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + "node_b:b": { |
| 56 | + "name": "node_b2" |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + "node_b:a": { |
| 61 | + "name": "node_b1" |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +</pre> |
| 66 | +</code> |
| 67 | +</details> |
| 68 | + |
| 69 | +### Extended usage of `Node` |
| 70 | + |
| 71 | +```ts |
| 72 | +import { Operator, Node } from "@himenon/path-oriented-data-structure"; |
| 73 | + |
| 74 | +export type KindOfString = "string"; |
| 75 | +export type KindOfNumber = "number"; |
| 76 | +export type Kind = KindOfNumber | KindOfString; |
| 77 | + |
| 78 | +export class StringValueNode extends Node<KindOfString> { |
| 79 | + constructor(name: string, private value: string) { |
| 80 | + super("string", name); |
| 81 | + } |
| 82 | + public getValue(): string { |
| 83 | + return this.value; |
| 84 | + } |
| 85 | + public setValue(value: string): void { |
| 86 | + this.value = value; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export class NumberValueNode extends Node<KindOfNumber> { |
| 91 | + constructor(name: string, private value: number) { |
| 92 | + super("number", name); |
| 93 | + } |
| 94 | + public getValue(): number { |
| 95 | + return this.value; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export type GetNode<T extends Kind> = T extends KindOfString ? StringValueNode : T extends KindOfNumber ? NumberValueNode : never; |
| 100 | + |
| 101 | +// Type Safe method |
| 102 | +export const createGetChildByPaths = (operator: Operator<string>) => <T extends Kind>(path: string, kind: T): GetNode<T> | undefined => { |
| 103 | + return operator.getChildByPaths(path, kind) as GetNode<T> | undefined; |
| 104 | +}; |
| 105 | + |
| 106 | +const operator = new Operator("tree"); |
| 107 | + |
| 108 | +operator.set("a/b", new StringValueNode("stringValue", "hello world")); |
| 109 | +operator.set("a/b", new NumberValueNode("numberValue", 123455)); |
| 110 | + |
| 111 | +operator.getHierarchy(); // Results below |
| 112 | + |
| 113 | +const getChildByPaths = createGetChildByPaths(operator); |
| 114 | + |
| 115 | +getChildByPaths("a/b", "string"); // ReturnType: StringValueNode | undefined |
| 116 | +getChildByPaths("a/b", "number"); // ReturnType: NumberValueNode | undefined |
| 117 | +``` |
| 118 | + |
| 119 | +<details> |
| 120 | +<summary>Result: operator.getHierarchy()</summary> |
| 121 | +<code> |
| 122 | +<pre> |
| 123 | +{ |
| 124 | + "name": ".", |
| 125 | + "children": { |
| 126 | + "tree:a": { |
| 127 | + "name": "a", |
| 128 | + "children": { |
| 129 | + "string:b": { |
| 130 | + "name": "stringValue" |
| 131 | + }, |
| 132 | + "number:b": { |
| 133 | + "name": "numberValue" |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +</pre> |
| 140 | +</code> |
| 141 | +</details> |
| 142 | + |
| 143 | +## API |
| 144 | + |
| 145 | +### `Operator` |
| 146 | + |
| 147 | +#### `getChildByPaths(path: string, kind: string): Component | undefined` |
18 | 148 |
|
19 |
| -## Features |
| 149 | +#### `set(path: string, component: Component): void` |
20 | 150 |
|
21 |
| -- [Proxy Directory](https://himenon.github.io/docs/javascript/proxy-directory-design-pattern/) |
| 151 | +#### `remove(path: string, kind: string): void` |
22 | 152 |
|
23 |
| -## Release |
| 153 | +#### `copy(from: string, to: string, kind: string): boolean` |
24 | 154 |
|
25 |
| -- Automatic version updates are performed when merged into the `main` branch. |
| 155 | +#### `move(from: string, to: string, kind: string): boolean` |
26 | 156 |
|
27 | 157 | ## LICENCE
|
28 | 158 |
|
29 |
| -[@himenon-node-lib-template](https://github.com/Himenon/node-lib-template)・MIT |
| 159 | +[@himenon/path-oriented-data-structure](https://github.com/Himenon/path-oriented-data-structure)・MIT |
0 commit comments