-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* npm用ビルドを追加 * GitHub Actionでnpm publish * READMEにusageを追加
- Loading branch information
Showing
6 changed files
with
126 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: release-npm | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Setup repo | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup Deno | ||
uses: denoland/setup-deno@v1 | ||
with: | ||
deno-version: v1.x | ||
|
||
- name: Get tag version | ||
if: startsWith(github.ref, 'refs/tags/') | ||
id: version | ||
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//} | ||
|
||
- name: Build for npm | ||
run: deno task build:npm ${{steps.version.outputs.TAG_VERSION}} | ||
|
||
- uses: JS-DevTools/npm-publish@v1 | ||
if: startsWith(github.ref, 'refs/tags/') | ||
with: | ||
token: ${{ secrets.NPM_TOKEN }} | ||
package: ./npm/package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Deno | ||
name: test | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.env | ||
.env.* | ||
Thumbs.db | ||
cov_profile | ||
cov_profile | ||
npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,22 @@ | |
Each transformation function is non-destructive, making it suitable for working | ||
with immutable tree structures. | ||
|
||
## Usage | ||
|
||
tree-fns is available for Deno and Node.js. | ||
|
||
### Deno | ||
|
||
```ts | ||
import { TreeNode, walk } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
``` | ||
|
||
### npm | ||
|
||
``` | ||
npm i tree-fns | ||
``` | ||
|
||
## Development | ||
|
||
### Prerequisite | ||
|
@@ -17,14 +33,14 @@ with immutable tree structures. | |
|
||
```ts | ||
const tree: TreeNode = { | ||
id: "root", | ||
id: 'root', | ||
children: [ | ||
{ | ||
id: "1", | ||
id: '1', | ||
children: [], | ||
}, | ||
{ | ||
id: "2", | ||
id: '2', | ||
children: [], | ||
}, | ||
], | ||
|
@@ -33,8 +49,8 @@ const tree: TreeNode = { | |
|
||
```ts | ||
const tree: TreeNode<{ data: string }> = { | ||
id: "root", | ||
data: "yay", | ||
id: 'root', | ||
data: 'yay', | ||
children: [], | ||
}; | ||
``` | ||
|
@@ -48,45 +64,45 @@ Only pre-order traversing is supported. | |
```ts | ||
walk( | ||
{ | ||
id: "root", | ||
id: 'root', | ||
children: [ | ||
{ | ||
id: "1", | ||
children: [{ id: "1-1", children: [] }], | ||
id: '1', | ||
children: [{ id: '1-1', children: [] }], | ||
}, | ||
{ | ||
id: "2", | ||
children: [{ id: "2-1", children: [] }], | ||
id: '2', | ||
children: [{ id: '2-1', children: [] }], | ||
}, | ||
], | ||
}, | ||
(node, location) => { | ||
console.log(node.id); // "root" -> "1" -> "1-1" -> "2" -> "2-1" | ||
if (node.id === "1") { | ||
if (node.id === '1') { | ||
console.log(location); // { parentPath: ["root"], index: 0 } | ||
} | ||
}, | ||
} | ||
); | ||
``` | ||
|
||
### `findNode(tree, test)` | `findNodeById(tree, id)` | ||
|
||
```ts | ||
const tree: TreeNode = { | ||
id: "root", | ||
id: 'root', | ||
children: [ | ||
{ | ||
id: "1", | ||
children: [{ id: "1-1", children: [] }], | ||
id: '1', | ||
children: [{ id: '1-1', children: [] }], | ||
}, | ||
{ | ||
id: "2", | ||
children: [{ id: "2-1", children: [] }], | ||
id: '2', | ||
children: [{ id: '2-1', children: [] }], | ||
}, | ||
], | ||
}; | ||
|
||
const node = findNode(tree, (node) => node.id === "1"); | ||
const node = findNode(tree, (node) => node.id === '1'); | ||
// [ | ||
// { | ||
// id: '1', | ||
|
@@ -98,25 +114,25 @@ const node = findNode(tree, (node) => node.id === "1"); | |
// }, | ||
// ] | ||
|
||
findNode(tree, (node) => node.id === "x"); // undefined | ||
findNode(tree, (node) => node.id === 'x'); // undefined | ||
``` | ||
|
||
or | ||
|
||
```ts | ||
const node = findNodeById(tree, "1"); | ||
const node = findNodeById(tree, '1'); | ||
``` | ||
|
||
### `map(tree, mapNode)` | ||
|
||
```ts | ||
const tree: TreeNode<{ data: string }> = { | ||
id: "1", | ||
data: "foo", | ||
id: '1', | ||
data: 'foo', | ||
children: [ | ||
{ | ||
id: "2", | ||
data: "bar", | ||
id: '2', | ||
data: 'bar', | ||
children: [], | ||
}, | ||
], | ||
|
@@ -143,8 +159,8 @@ const mapped = map<{ data: string }>(tree, (node) => ({ | |
|
||
```ts | ||
const tree = { | ||
id: "1", | ||
children: [{ id: "2", children: [] }], | ||
id: '1', | ||
children: [{ id: '2', children: [] }], | ||
}; | ||
|
||
const copied = copy(tree); | ||
|
@@ -160,13 +176,13 @@ console.log(tree === copied); // false | |
|
||
```ts | ||
const srcTree = { | ||
id: "1", | ||
children: [{ id: "2", children: [] }], | ||
id: '1', | ||
children: [{ id: '2', children: [] }], | ||
}; | ||
|
||
const nodeToBeAdded = { id: "3", children: [] }; | ||
const nodeToBeAdded = { id: '3', children: [] }; | ||
|
||
const destTree = addNode(srcTree, nodeToBeAdded, { parentId: "1", index: 0 }); | ||
const destTree = addNode(srcTree, nodeToBeAdded, { parentId: '1', index: 0 }); | ||
// { | ||
// id: '1', | ||
// children: [ | ||
|
@@ -180,13 +196,13 @@ const destTree = addNode(srcTree, nodeToBeAdded, { parentId: "1", index: 0 }); | |
|
||
```ts | ||
const srcTree = { | ||
id: "1", | ||
id: '1', | ||
children: [ | ||
{ id: "2", children: [] }, | ||
{ id: "3", children: [] }, | ||
{ id: '2', children: [] }, | ||
{ id: '3', children: [] }, | ||
], | ||
}; | ||
const [destTree, removedNode] = removeNode(srcTree, "3"); | ||
const [destTree, removedNode] = removeNode(srcTree, '3'); | ||
|
||
console.log(removedNode); // { id: '3', children: [] } | ||
console.log(destTree); | ||
|
@@ -197,21 +213,21 @@ console.log(destTree); | |
// ], | ||
// } | ||
|
||
removeNode(srcTree, "x"); // undefined | ||
removeNode(srcTree, 'x'); // undefined | ||
``` | ||
|
||
### `moveNode(tree, id, dest)` | ||
|
||
```ts | ||
const srcTree = { | ||
id: "1", | ||
id: '1', | ||
children: [ | ||
{ id: "2", children: [] }, | ||
{ id: "3", children: [] }, | ||
{ id: '2', children: [] }, | ||
{ id: '3', children: [] }, | ||
], | ||
}; | ||
|
||
const destTree = moveNode(srcTree, "2", { parentId: "3", index: 0 }); | ||
const destTree = moveNode(srcTree, '2', { parentId: '3', index: 0 }); | ||
// { | ||
// id: '1', | ||
// children: [ | ||
|
@@ -226,10 +242,10 @@ const destTree = moveNode(srcTree, "2", { parentId: "3", index: 0 }); | |
|
||
```ts | ||
const tree = { | ||
id: "1", | ||
id: '1', | ||
children: [ | ||
{ id: "2", children: [] }, | ||
{ id: "3", children: [] }, | ||
{ id: '2', children: [] }, | ||
{ id: '3', children: [] }, | ||
], | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
await emptyDir("./npm"); | ||
|
||
await build({ | ||
entryPoints: ["./mod.ts"], | ||
outDir: "./npm", | ||
importMap: "./import_map.json", | ||
shims: { | ||
deno: true, | ||
}, | ||
package: { | ||
name: "tree-fns", | ||
version: Deno.args[0]?.replace(/^v/, ""), | ||
description: "Simple utility functions for tree structure", | ||
license: "MIT", | ||
repository: { | ||
type: "git", | ||
url: "git+https://github.com/qmotas/tree-fns.git", | ||
}, | ||
bugs: { | ||
url: "https://github.com/qmotas/tree-fns/issues", | ||
}, | ||
}, | ||
}); | ||
|
||
// post build steps | ||
Deno.copyFileSync("LICENSE", "npm/LICENSE"); | ||
Deno.copyFileSync("README.md", "npm/README.md"); |