Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 13, 2021
1 parent 9d7f999 commit c206b25
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
40 changes: 35 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import {arrayIterate} from 'array-iterate'

// Turn `callback` into a child-modifier accepting a parent. See
// `array-iterate` for more info.
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Node} Node
*
* @callback Modifier
* @param {Node} node
* @param {number} index
* @param {Parent} parent
* @returns {number|void}
*
* @callback Modify
* @param {Parent} node
* @returns {void}
*/

/**
* Turn `callback` into a child-modifier accepting a parent.
* See `array-iterate` for more info.
*
* @param {Modifier} callback
* @returns {Modify}
*/
export function modifyChildren(callback) {
return iterator

/**
* @param {Parent} parent
* @returns {void}
*/
function iterator(parent) {
if (!parent || !parent.children) {
throw new Error('Missing children in `parent` for `modifier`')
Expand All @@ -13,8 +37,14 @@ export function modifyChildren(callback) {
arrayIterate(parent.children, iteratee, parent)
}

// Pass the context as the third argument to `callback`.
function iteratee(value, index) {
return callback(value, index, this)
/**
* Pass the context as the third argument to `callback`.
*
* @this {Parent}
* @param {Node} node
* @param {number} index
*/
function iteratee(node, index) {
return callback(node, index, this)
}
}
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,36 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"types": "index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"array-iterate": "^2.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"dtslint": "^4.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unified": "^9.0.0",
"xo": "^0.38.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -71,5 +77,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
113 changes: 82 additions & 31 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import test from 'tape'
import {modifyChildren} from './index.js'

var noop = Function.prototype
function noop() {}

test('modifyChildren()', function (t) {
t.throws(
function () {
// @ts-ignore runtime.
modifyChildren(noop)()
},
/Missing children in `parent`/,
Expand All @@ -14,22 +15,26 @@ test('modifyChildren()', function (t) {

t.throws(
function () {
// @ts-ignore runtime.
modifyChildren(noop)({})
},
/Missing children in `parent`/,
'should throw without parent'
)

t.test('should invoke `fn` for each child in `parent`', function (st) {
var values = [0, 1, 2, 3]
var context = {}
var children = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
var context = {type: 'y', children}
var n = -1

context.children = values

modifyChildren(function (child, index, parent) {
n++
st.strictEqual(child, values[n])
st.strictEqual(child, children[n])
st.strictEqual(index, n)
st.strictEqual(parent, context)
})(context)
Expand All @@ -38,63 +43,109 @@ test('modifyChildren()', function (t) {
})

t.test('should work when new children are added', function (st) {
var values = [0, 1, 2, 3, 4, 5, 6]
var children = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3},
{type: 'x', value: 4},
{type: 'x', value: 5},
{type: 'x', value: 6}
]
var n = -1

modifyChildren(function (child, index, parent) {
n++

if (index < 3) {
parent.children.push(parent.children.length)
parent.children.push({type: 'x', value: parent.children.length})
}

st.strictEqual(child, values[n])
st.strictEqual(index, values[n])
})({children: [0, 1, 2, 3]})
st.deepEqual(child, children[n])
st.deepEqual(index, n)
})({
type: 'y',
children: [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
})

st.end()
})

t.test('should skip forwards', function (st) {
var values = [0, 1, 2, 3]
var children = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
var n = -1
var context = {}

context.children = [0, 1, 3]
var context = {
type: 'y',
children: [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 3}
]
}

modifyChildren(function (child, index, parent) {
st.strictEqual(child, values[++n])
st.deepEqual(child, children[++n])

if (child === 1) {
parent.children.splice(index + 1, 0, 2)
if (child.value === 1) {
parent.children.splice(index + 1, 0, {type: 'x', value: 2})
return index + 1
}
})(context)

st.deepEqual(context.children, values)
st.deepEqual(context.children, children)

st.end()
})

t.test('should skip backwards', function (st) {
var invocations = [0, 1, -1, 0, 1, 2, 3]
var calls = [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: -1},
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
var n = -1
var context = {}
var inserted

context.children = [0, 1, 2, 3]

modifyChildren(function (child, index, parent) {
st.strictEqual(child, invocations[++n])

if (!inserted && child === 1) {
var context = {
type: 'y',
children: [
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
]
}
var inserted = false

modifyChildren(function (child, _, parent) {
st.deepEqual(child, calls[++n])

if (!inserted && child.value === 1) {
inserted = true
parent.children.unshift(-1)
parent.children.unshift({type: 'x', value: -1})
return -1
}
})(context)

st.deepEqual(context.children, [-1, 0, 1, 2, 3])
st.deepEqual(context.children, [
{type: 'x', value: -1},
{type: 'x', value: 0},
{type: 'x', value: 1},
{type: 'x', value: 2},
{type: 'x', value: 3}
])

st.end()
})
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}
21 changes: 0 additions & 21 deletions types/index.d.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions types/tslint.json

This file was deleted.

45 changes: 0 additions & 45 deletions types/unist-util-modify-children-test.ts

This file was deleted.

0 comments on commit c206b25

Please sign in to comment.