diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..019bb40 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,19 @@ +// TypeScript Version: 3.5 + +import {Node, Parent} from 'unist' +import {Test} from 'unist-util-is' + +export = findAllAfter +/** + * Unist utility to get all children of a parent after a node or index + * + * @param parent Parent to search in + * @param index or Node to start from + * @param test that Nodes must pass to be included + * @returns Array of found Nodes + */ +declare function findAllAfter( + parent: Parent, + index: number | Node, + test?: Test | Array> +): Node[] diff --git a/package.json b/package.json index c728996..53d4828 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,21 @@ }, "author": "Titus Wormer (https://wooorm.com)", "contributors": [ - "Titus Wormer (https://wooorm.com)" + "Titus Wormer (https://wooorm.com)", + "Lucas Brandstaetter (https://github.com/Roang-zero1)" ], + "files": [ + "index.js", + "index.d.ts" + ], + "types": "index.d.ts", "dependencies": { "unist-util-is": "^4.0.0" }, "devDependencies": { + "@types/tape": "^4.2.33", "browserify": "^16.0.0", + "dtslint": "^2.0.2", "nyc": "^14.0.0", "prettier": "^1.0.0", "remark": "^11.0.0", @@ -33,16 +41,18 @@ "remark-preset-wooorm": "^6.0.0", "tape": "^4.0.0", "tinyify": "^2.0.0", + "typescript": "^3.7.2", "xo": "^0.25.0" }, "scripts": { - "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", + "format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix", "build-bundle": "browserify . -s unistUtilFindAllAfter -o unist-util-find-all-after.js", "build-mangle": "browserify . -s unistUtilFindAllAfter -p tinyify -o unist-util-find-all-after.min.js", "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 .", + "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..6fd80d5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "unist-util-find-all-after": ["index.d.ts"] + } + } +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +} diff --git a/unist-util-find-all-after-test.ts b/unist-util-find-all-after-test.ts new file mode 100644 index 0000000..058b4d5 --- /dev/null +++ b/unist-util-find-all-after-test.ts @@ -0,0 +1,37 @@ +import {Node, Parent} from 'unist' +import findAllAfter = require('unist-util-find-all-after') + +const heading: Node = { + type: 'heading', + depth: 2, + children: [] +} + +const parent: Parent = { + type: 'root', + children: [heading] +} + +/*=== missing params ===*/ +// $ExpectError +findAllAfter() +// $ExpectError +findAllAfter(parent) + +/*=== find by index/node ===*/ +findAllAfter(parent, 1) +findAllAfter(parent, heading) +// $ExpectError +findAllAfter(parent, false) + +/*=== find with test ===*/ +// $ExpectError +findAllAfter(parent, 1, false) +findAllAfter(parent, 1, 'paragraph') + +/*=== invalid return ===*/ +// $ExpectError +const returnIsString: string = findAllAfter(parent, 1) + +/*=== valid return ===*/ +const nodes: Node[] = findAllAfter(parent, 1)