Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
Closes GH-1.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
ChristianMurphy authored and wooorm committed Oct 28, 2019
1 parent dc85434 commit 66e36ea
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@
"author": "azu <[email protected]>",
"contributors": [
"azu <[email protected]>",
"Titus Wormer <[email protected]> (https://wooorm.com)"
"Titus Wormer <[email protected]> (https://wooorm.com)",
"Christian Murphy <[email protected]>"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"@types/mdast": "^3.0.3",
"object-assign": "^4.0.1"
},
"devDependencies": {
"dtslint": "^0.9.9",
"nyc": "^14.0.0",
"prettier": "^1.0.0",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^5.0.0",
"tape": "^4.10.1",
"unist-builder": "^1.0.3",
"unist-util-is": "^4.0.0",
"xo": "^0.24.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
19 changes: 19 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TypeScript Version: 3.5

import {Node} from 'unist'

declare namespace unistUtilMap {
type MapFunction = (node: Node, index: number | null, parent: Node | null) => Node
}

/**
* unist utility to create a new tree by mapping all nodes with the given function.
*
* @param tree Tree to map
* @param callback Function that returns a new node
* @returns Node to be used in the new tree.
* Its children are not used: if the original node has children, those are mapped.
*/
declare function unistUtilMap(tree: Node, callback: unistUtilMap.MapFunction): Node

export = unistUtilMap
10 changes: 10 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unist-util-map": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false
}
}
37 changes: 37 additions & 0 deletions types/unist-util-map-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as map from 'unist-util-map'
import * as is from 'unist-util-is'
import {Node} from 'unist'
import {Heading, Paragraph} from 'mdast'

// $ExpectType Node
map({type: 'root'}, (node: Node) => ({type: node.type + 'test'}))
// $ExpectType Node
map({type: 'root'}, (node: Node, index: number | null) => ({
type: node.type + 'test'
}))
// $ExpectType Node
map(
{type: 'root'},
(node: Node, index: number | null, parent: Node | null) => ({
type: node.type + 'test'
})
)
// $ExpectType Node
map({type: 'root'}, (node: Node) => {
if (is<Heading>(node, 'heading')) {
const depth = node.depth < 5 ? node.depth + 1 : 6
return {
...node,
depth
}
}

if (is<Paragraph>(node, 'paragraph')) {
return {
type: 'strong',
children: node.children
}
}

return node
})

0 comments on commit 66e36ea

Please sign in to comment.