unist-util-remove
Advanced tools
Comparing version 2.0.0 to 2.0.1
63
index.js
@@ -7,59 +7,38 @@ 'use strict' | ||
function remove(ast, opts, test) { | ||
var is | ||
var cascade | ||
function remove(tree, options, test) { | ||
var is = convert(test || options) | ||
var cascade = options.cascade == null ? true : options.cascade | ||
if (!test) { | ||
test = opts | ||
opts = {} | ||
} | ||
return preorder(tree, null, null) | ||
cascade = opts.cascade | ||
cascade = cascade === null || cascade === undefined ? true : cascade | ||
is = convert(test) | ||
return preorder(ast, null, null) | ||
// Check and remove nodes recursively in preorder. | ||
// For each composite node, modify its children array in-place. | ||
function preorder(node, nodeIndex, parent) { | ||
var children | ||
var length | ||
var index | ||
var position | ||
var child | ||
function preorder(node, index, parent) { | ||
var children = node.children | ||
var childIndex = -1 | ||
var position = 0 | ||
if (is(node, nodeIndex, parent)) { | ||
if (is(node, index, parent)) { | ||
return null | ||
} | ||
children = node.children | ||
if (children && children.length) { | ||
// Move all living children to the beginning of the children array. | ||
while (++childIndex < children.length) { | ||
if (preorder(children[childIndex], childIndex, node)) { | ||
children[position++] = children[childIndex] | ||
} | ||
} | ||
if (!children || children.length === 0) { | ||
return node | ||
} | ||
// Move all living children to the beginning of the children array. | ||
position = 0 | ||
length = children.length | ||
index = -1 | ||
while (++index < length) { | ||
child = preorder(children[index], index, node) | ||
if (child) { | ||
children[position++] = child | ||
// Cascade delete. | ||
if (cascade && !position) { | ||
return null | ||
} | ||
} | ||
// Cascade delete. | ||
if (cascade && position === 0) { | ||
return null | ||
// Drop other nodes. | ||
children.length = position | ||
} | ||
// Drop other nodes. | ||
children.length = position | ||
return node | ||
} | ||
} |
{ | ||
"name": "unist-util-remove", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "unist utility to remove nodes from a tree", | ||
@@ -40,11 +40,11 @@ "license": "MIT", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"unist-builder": "^2.0.0", | ||
"xo": "^0.25.0" | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test", | ||
@@ -70,3 +70,14 @@ "test-coverage": "nyc --reporter lcov tape test.js", | ||
"prettier": true, | ||
"esnext": false | ||
"esnext": false, | ||
"rules": { | ||
"eqeqeq": [ | ||
"error", | ||
"always", | ||
{ | ||
"null": "ignore" | ||
} | ||
], | ||
"no-eq-null": "off", | ||
"unicorn/explicit-length-check": "off" | ||
} | ||
}, | ||
@@ -73,0 +84,0 @@ "remarkConfig": { |
@@ -136,5 +136,5 @@ # unist-util-remove | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/syntax-tree | ||
[chat]: https://github.com/syntax-tree/unist/discussions | ||
@@ -159,6 +159,6 @@ [npm]: https://docs.npmjs.com/cli/install | ||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8406
33