Skip to content

Commit c206b25

Browse files
committed
Add JSDoc based types
1 parent 9d7f999 commit c206b25

File tree

9 files changed

+149
-124
lines changed

9 files changed

+149
-124
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

index.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import {arrayIterate} from 'array-iterate'
22

3-
// Turn `callback` into a child-modifier accepting a parent. See
4-
// `array-iterate` for more info.
3+
/**
4+
* @typedef {import('unist').Parent} Parent
5+
* @typedef {import('unist').Node} Node
6+
*
7+
* @callback Modifier
8+
* @param {Node} node
9+
* @param {number} index
10+
* @param {Parent} parent
11+
* @returns {number|void}
12+
*
13+
* @callback Modify
14+
* @param {Parent} node
15+
* @returns {void}
16+
*/
17+
18+
/**
19+
* Turn `callback` into a child-modifier accepting a parent.
20+
* See `array-iterate` for more info.
21+
*
22+
* @param {Modifier} callback
23+
* @returns {Modify}
24+
*/
525
export function modifyChildren(callback) {
626
return iterator
727

28+
/**
29+
* @param {Parent} parent
30+
* @returns {void}
31+
*/
832
function iterator(parent) {
933
if (!parent || !parent.children) {
1034
throw new Error('Missing children in `parent` for `modifier`')
@@ -13,8 +37,14 @@ export function modifyChildren(callback) {
1337
arrayIterate(parent.children, iteratee, parent)
1438
}
1539

16-
// Pass the context as the third argument to `callback`.
17-
function iteratee(value, index) {
18-
return callback(value, index, this)
40+
/**
41+
* Pass the context as the third argument to `callback`.
42+
*
43+
* @this {Parent}
44+
* @param {Node} node
45+
* @param {number} index
46+
*/
47+
function iteratee(node, index) {
48+
return callback(node, index, this)
1949
}
2050
}

package.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,36 @@
2727
"sideEffects": false,
2828
"type": "module",
2929
"main": "index.js",
30-
"types": "types/index.d.ts",
30+
"types": "index.d.ts",
3131
"files": [
32-
"index.js",
33-
"types/index.d.ts"
32+
"index.d.ts",
33+
"index.js"
3434
],
3535
"dependencies": {
36+
"@types/unist": "^2.0.0",
3637
"array-iterate": "^2.0.0"
3738
},
3839
"devDependencies": {
40+
"@types/tape": "^4.0.0",
3941
"c8": "^7.0.0",
4042
"dtslint": "^4.0.0",
4143
"prettier": "^2.0.0",
4244
"remark-cli": "^9.0.0",
4345
"remark-preset-wooorm": "^8.0.0",
46+
"rimraf": "^3.0.0",
4447
"tape": "^5.0.0",
48+
"type-coverage": "^2.0.0",
49+
"typescript": "^4.0.0",
4550
"unified": "^9.0.0",
4651
"xo": "^0.38.0"
4752
},
4853
"scripts": {
54+
"prepack": "npm run build && npm run format",
55+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4956
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5057
"test-api": "node test.js",
5158
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
52-
"test-types": "dtslint types",
53-
"test": "npm run format && npm run test-coverage && npm run test-types"
59+
"test": "npm run build && npm run format && npm run test-coverage"
5460
},
5561
"prettier": {
5662
"tabWidth": 2,
@@ -71,5 +77,10 @@
7177
"plugins": [
7278
"preset-wooorm"
7379
]
80+
},
81+
"typeCoverage": {
82+
"atLeast": 100,
83+
"detail": true,
84+
"strict": true
7485
}
7586
}

test.js

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import test from 'tape'
22
import {modifyChildren} from './index.js'
33

4-
var noop = Function.prototype
4+
function noop() {}
55

66
test('modifyChildren()', function (t) {
77
t.throws(
88
function () {
9+
// @ts-ignore runtime.
910
modifyChildren(noop)()
1011
},
1112
/Missing children in `parent`/,
@@ -14,22 +15,26 @@ test('modifyChildren()', function (t) {
1415

1516
t.throws(
1617
function () {
18+
// @ts-ignore runtime.
1719
modifyChildren(noop)({})
1820
},
1921
/Missing children in `parent`/,
2022
'should throw without parent'
2123
)
2224

2325
t.test('should invoke `fn` for each child in `parent`', function (st) {
24-
var values = [0, 1, 2, 3]
25-
var context = {}
26+
var children = [
27+
{type: 'x', value: 0},
28+
{type: 'x', value: 1},
29+
{type: 'x', value: 2},
30+
{type: 'x', value: 3}
31+
]
32+
var context = {type: 'y', children}
2633
var n = -1
2734

28-
context.children = values
29-
3035
modifyChildren(function (child, index, parent) {
3136
n++
32-
st.strictEqual(child, values[n])
37+
st.strictEqual(child, children[n])
3338
st.strictEqual(index, n)
3439
st.strictEqual(parent, context)
3540
})(context)
@@ -38,63 +43,109 @@ test('modifyChildren()', function (t) {
3843
})
3944

4045
t.test('should work when new children are added', function (st) {
41-
var values = [0, 1, 2, 3, 4, 5, 6]
46+
var children = [
47+
{type: 'x', value: 0},
48+
{type: 'x', value: 1},
49+
{type: 'x', value: 2},
50+
{type: 'x', value: 3},
51+
{type: 'x', value: 4},
52+
{type: 'x', value: 5},
53+
{type: 'x', value: 6}
54+
]
4255
var n = -1
4356

4457
modifyChildren(function (child, index, parent) {
4558
n++
4659

4760
if (index < 3) {
48-
parent.children.push(parent.children.length)
61+
parent.children.push({type: 'x', value: parent.children.length})
4962
}
5063

51-
st.strictEqual(child, values[n])
52-
st.strictEqual(index, values[n])
53-
})({children: [0, 1, 2, 3]})
64+
st.deepEqual(child, children[n])
65+
st.deepEqual(index, n)
66+
})({
67+
type: 'y',
68+
children: [
69+
{type: 'x', value: 0},
70+
{type: 'x', value: 1},
71+
{type: 'x', value: 2},
72+
{type: 'x', value: 3}
73+
]
74+
})
5475

5576
st.end()
5677
})
5778

5879
t.test('should skip forwards', function (st) {
59-
var values = [0, 1, 2, 3]
80+
var children = [
81+
{type: 'x', value: 0},
82+
{type: 'x', value: 1},
83+
{type: 'x', value: 2},
84+
{type: 'x', value: 3}
85+
]
6086
var n = -1
61-
var context = {}
62-
63-
context.children = [0, 1, 3]
87+
var context = {
88+
type: 'y',
89+
children: [
90+
{type: 'x', value: 0},
91+
{type: 'x', value: 1},
92+
{type: 'x', value: 3}
93+
]
94+
}
6495

6596
modifyChildren(function (child, index, parent) {
66-
st.strictEqual(child, values[++n])
97+
st.deepEqual(child, children[++n])
6798

68-
if (child === 1) {
69-
parent.children.splice(index + 1, 0, 2)
99+
if (child.value === 1) {
100+
parent.children.splice(index + 1, 0, {type: 'x', value: 2})
70101
return index + 1
71102
}
72103
})(context)
73104

74-
st.deepEqual(context.children, values)
105+
st.deepEqual(context.children, children)
75106

76107
st.end()
77108
})
78109

79110
t.test('should skip backwards', function (st) {
80-
var invocations = [0, 1, -1, 0, 1, 2, 3]
111+
var calls = [
112+
{type: 'x', value: 0},
113+
{type: 'x', value: 1},
114+
{type: 'x', value: -1},
115+
{type: 'x', value: 0},
116+
{type: 'x', value: 1},
117+
{type: 'x', value: 2},
118+
{type: 'x', value: 3}
119+
]
81120
var n = -1
82-
var context = {}
83-
var inserted
84-
85-
context.children = [0, 1, 2, 3]
86-
87-
modifyChildren(function (child, index, parent) {
88-
st.strictEqual(child, invocations[++n])
89-
90-
if (!inserted && child === 1) {
121+
var context = {
122+
type: 'y',
123+
children: [
124+
{type: 'x', value: 0},
125+
{type: 'x', value: 1},
126+
{type: 'x', value: 2},
127+
{type: 'x', value: 3}
128+
]
129+
}
130+
var inserted = false
131+
132+
modifyChildren(function (child, _, parent) {
133+
st.deepEqual(child, calls[++n])
134+
135+
if (!inserted && child.value === 1) {
91136
inserted = true
92-
parent.children.unshift(-1)
137+
parent.children.unshift({type: 'x', value: -1})
93138
return -1
94139
}
95140
})(context)
96141

97-
st.deepEqual(context.children, [-1, 0, 1, 2, 3])
142+
st.deepEqual(context.children, [
143+
{type: 'x', value: -1},
144+
{type: 'x', value: 0},
145+
{type: 'x', value: 1},
146+
{type: 'x', value: 2},
147+
{type: 'x', value: 3}
148+
])
98149

99150
st.end()
100151
})

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true
14+
}
15+
}

types/index.d.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

types/tsconfig.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

types/tslint.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

types/unist-util-modify-children-test.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)