diff --git a/index.js b/index.js index 6926d44..1718b93 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ function iteratorFactory(callback) { throw new Error('Missing children in `parent` for `modifier`') } - return iterate(children, callback, parent) + iterate(children, callback, parent) } } diff --git a/package.json b/package.json index 800e901..e8b05d8 100644 --- a/package.json +++ b/package.json @@ -21,22 +21,27 @@ }, "author": "Titus Wormer (https://wooorm.com)", "contributors": [ - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Merlijn Vos " ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types/index.d.ts", "dependencies": { "array-iterate": "^1.0.0" }, "devDependencies": { "browserify": "^16.0.0", + "dtslint": "^3.3.0", "nyc": "^15.0.0", "prettier": "^1.0.0", "remark-cli": "^7.0.0", "remark-preset-wooorm": "^6.0.0", "tape": "^4.0.0", "tinyify": "^2.0.0", + "unified": "^8.4.2", "xo": "^0.26.0" }, "scripts": { @@ -46,7 +51,8 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run format && npm run build && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "prettier": { "tabWidth": 2, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..ec6473a --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,25 @@ +// TypeScript Version: 3.5 + +import {Node, Parent} from 'unist' + +declare namespace unistUtilModifyChildren { + type Modifier = ( + node: Node, + index: number, + parent: Parent + ) => number | void + + type Modify = (tree: Node) => void +} + +/** + * unist utility to modify direct children of a parent. + * + * @param callback modifier function that (optionally) returns a next position (number) to iterate. + * @returns callback to be used on the tree. + */ +declare function unistUtilModifyChildren( + modifier: unistUtilModifyChildren.Modifier +): unistUtilModifyChildren.Modify + +export = unistUtilModifyChildren diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..c09a33a --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-modify-children": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..9d87fc0 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,8 @@ + +{ + "extends": "dtslint/dtslint.json", + "rules": { + "whitespace": false, + "semicolon": false + } +} diff --git a/types/unist-util-modify-children-test.ts b/types/unist-util-modify-children-test.ts new file mode 100644 index 0000000..bd39114 --- /dev/null +++ b/types/unist-util-modify-children-test.ts @@ -0,0 +1,43 @@ +import {Node} from 'unist' + +import unified = require('unified') + +import * as modifyChildren from 'unist-util-modify-children' + +const node: Node = { + type: 'root', + children: [ + {type: 'leaf', value: '1'}, + {type: 'leaf', children: [{type: 'leaf', value: '2'}]}, + {type: 'leaf', value: '3'} + ] +} + +// $ExpectType Modify +modifyChildren((node, index) => index + 1) + +// $ExpectType Modify +modifyChildren(() => {}) + +// $ExpectError +modifyChildren(() => '') + +// $ExpectType void +modifyChildren((node, index) => index + 1)(node) + +// Usable in unified transform +unified().use(() => tree => { + const modify = modifyChildren((node, index, parent) => { + if (node.type === 'node') { + parent.children.splice(index, 1, { + type: 'subtree', + children: node.children + }) + return index + 1 + } + }) + + modify(tree) + + return tree +})