Skip to content

Add types #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@
},
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Merlijn Vos <merlijn@soverin.net>"
],
"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": {
Expand All @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
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-modify-children": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false
}
}
43 changes: 43 additions & 0 deletions types/unist-util-modify-children-test.ts
Original file line number Diff line number Diff line change
@@ -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
})