Skip to content

Commit a252029

Browse files
authored
Add types
Closes GH-5. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent a551145 commit a252029

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node, Parent} from 'unist'
4+
import {Test} from 'unist-util-is'
5+
6+
export = findAllBefore
7+
8+
/**
9+
* Unist utility to get all children of a parent before a node or index
10+
*
11+
* @param parent Parent to search in
12+
* @param index or Node to start from
13+
* @param test that Nodes must pass to be included
14+
* @returns Array of found Nodes
15+
*/
16+
declare function findAllBefore<T extends Node>(
17+
parent: Parent,
18+
index: number | Node,
19+
test?: Test<T> | Array<Test<T>>
20+
): Node[]

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@
2222
"contributors": [
2323
"Titus Wormer <[email protected]> (https://wooorm.com)"
2424
],
25+
"files": [
26+
"index.js",
27+
"index.d.ts"
28+
],
29+
"types": "index.d.ts",
2530
"dependencies": {
2631
"unist-util-is": "^4.0.0"
2732
},
2833
"devDependencies": {
2934
"browserify": "^16.0.0",
35+
"dtslint": "^3.6.14",
3036
"nyc": "^15.0.0",
3137
"prettier": "^2.0.0",
3238
"remark": "^12.0.0",
@@ -43,7 +49,8 @@
4349
"build": "npm run build-bundle && npm run build-mangle",
4450
"test-api": "node test",
4551
"test-coverage": "nyc --reporter lcov tape test.js",
46-
"test": "npm run format && npm run build && npm run test-coverage"
52+
"test-types": "dtslint .",
53+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
4754
},
4855
"nyc": {
4956
"check-coverage": true,
@@ -66,7 +73,8 @@
6673
"unicorn/prefer-number-properties": "off"
6774
},
6875
"ignore": [
69-
"unist-util-find-all-before.js"
76+
"unist-util-find-all-before.js",
77+
"*.ts"
7078
]
7179
},
7280
"remarkConfig": {

tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"unist-util-find-all-before": ["index.d.ts"]
8+
}
9+
}
10+
}

tslint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false,
6+
"strict-export-declare-modifiers": false
7+
}
8+
}

unist-util-find-all-before-test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Node, Parent} from 'unist'
2+
import findAllBefore = require('unist-util-find-all-before')
3+
4+
const heading: Node = {
5+
type: 'heading',
6+
depth: 2,
7+
children: []
8+
}
9+
10+
const parent: Parent = {
11+
type: 'root',
12+
children: [heading]
13+
}
14+
15+
// Missing params
16+
// $ExpectError
17+
findAllBefore()
18+
19+
// $ExpectError
20+
findAllBefore(parent)
21+
22+
// Find by index or node
23+
findAllBefore(parent, 1)
24+
findAllBefore(parent, heading)
25+
26+
// Invalid test type
27+
// $ExpectError
28+
findAllBefore(parent, 1, false)
29+
30+
// Valid test type
31+
findAllBefore(parent, 1, 'paragraph')
32+
33+
// Invalid retrurn type
34+
// $ExpectError
35+
const returnsString: string = findAllBefore(parent, 1)
36+
37+
// Valid return type
38+
const nodes: Node[] = findAllBefore(parent, 1)

0 commit comments

Comments
 (0)