Skip to content

Commit 66e36ea

Browse files
ChristianMurphywooorm
authored andcommitted
Add types
Closes GH-1. Reviewed-by: Titus Wormer <[email protected]>
1 parent dc85434 commit 66e36ea

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,35 @@
1515
"author": "azu <[email protected]>",
1616
"contributors": [
1717
18-
"Titus Wormer <[email protected]> (https://wooorm.com)"
18+
"Titus Wormer <[email protected]> (https://wooorm.com)",
19+
"Christian Murphy <[email protected]>"
1920
],
2021
"files": [
21-
"index.js"
22+
"index.js",
23+
"types/index.d.ts"
2224
],
25+
"types": "types/index.d.ts",
2326
"dependencies": {
27+
"@types/mdast": "^3.0.3",
2428
"object-assign": "^4.0.1"
2529
},
2630
"devDependencies": {
31+
"dtslint": "^0.9.9",
2732
"nyc": "^14.0.0",
2833
"prettier": "^1.0.0",
2934
"remark-cli": "^6.0.0",
3035
"remark-preset-wooorm": "^5.0.0",
3136
"tape": "^4.10.1",
3237
"unist-builder": "^1.0.3",
38+
"unist-util-is": "^4.0.0",
3339
"xo": "^0.24.0"
3440
},
3541
"scripts": {
3642
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
3743
"test-api": "node test",
3844
"test-coverage": "nyc --reporter lcov tape test.js",
39-
"test": "npm run format && npm run test-coverage"
45+
"test-types": "dtslint types",
46+
"test": "npm run format && npm run test-coverage && npm run test-types"
4047
},
4148
"nyc": {
4249
"check-coverage": true,

types/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node} from 'unist'
4+
5+
declare namespace unistUtilMap {
6+
type MapFunction = (node: Node, index: number | null, parent: Node | null) => Node
7+
}
8+
9+
/**
10+
* unist utility to create a new tree by mapping all nodes with the given function.
11+
*
12+
* @param tree Tree to map
13+
* @param callback Function that returns a new node
14+
* @returns Node to be used in the new tree.
15+
* Its children are not used: if the original node has children, those are mapped.
16+
*/
17+
declare function unistUtilMap(tree: Node, callback: unistUtilMap.MapFunction): Node
18+
19+
export = unistUtilMap

types/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-map": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

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

types/unist-util-map-tests.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as map from 'unist-util-map'
2+
import * as is from 'unist-util-is'
3+
import {Node} from 'unist'
4+
import {Heading, Paragraph} from 'mdast'
5+
6+
// $ExpectType Node
7+
map({type: 'root'}, (node: Node) => ({type: node.type + 'test'}))
8+
// $ExpectType Node
9+
map({type: 'root'}, (node: Node, index: number | null) => ({
10+
type: node.type + 'test'
11+
}))
12+
// $ExpectType Node
13+
map(
14+
{type: 'root'},
15+
(node: Node, index: number | null, parent: Node | null) => ({
16+
type: node.type + 'test'
17+
})
18+
)
19+
// $ExpectType Node
20+
map({type: 'root'}, (node: Node) => {
21+
if (is<Heading>(node, 'heading')) {
22+
const depth = node.depth < 5 ? node.depth + 1 : 6
23+
return {
24+
...node,
25+
depth
26+
}
27+
}
28+
29+
if (is<Paragraph>(node, 'paragraph')) {
30+
return {
31+
type: 'strong',
32+
children: node.children
33+
}
34+
}
35+
36+
return node
37+
})

0 commit comments

Comments
 (0)