unist-util-is
Advanced tools
Comparing version 4.0.2 to 4.0.3
@@ -6,2 +6,6 @@ 'use strict' | ||
function convert(test) { | ||
if (test == null) { | ||
return ok | ||
} | ||
if (typeof test === 'string') { | ||
@@ -11,8 +15,4 @@ return typeFactory(test) | ||
if (test === null || test === undefined) { | ||
return ok | ||
} | ||
if (typeof test === 'object') { | ||
return ('length' in test ? anyFactory : matchesFactory)(test) | ||
return 'length' in test ? anyFactory(test) : allFactory(test) | ||
} | ||
@@ -27,26 +27,12 @@ | ||
function convertAll(tests) { | ||
var results = [] | ||
var length = tests.length | ||
var index = -1 | ||
while (++index < length) { | ||
results[index] = convert(tests[index]) | ||
} | ||
return results | ||
} | ||
// Utility assert each property in `test` is represented in `node`, and each | ||
// values are strictly equal. | ||
function matchesFactory(test) { | ||
return matches | ||
function allFactory(test) { | ||
return all | ||
function matches(node) { | ||
function all(node) { | ||
var key | ||
for (key in test) { | ||
if (node[key] !== test[key]) { | ||
return false | ||
} | ||
if (node[key] !== test[key]) return | ||
} | ||
@@ -59,11 +45,15 @@ | ||
function anyFactory(tests) { | ||
var checks = convertAll(tests) | ||
var length = checks.length | ||
var checks = [] | ||
var index = -1 | ||
return matches | ||
while (++index < tests.length) { | ||
checks[index] = convert(tests[index]) | ||
} | ||
function matches() { | ||
return any | ||
function any() { | ||
var index = -1 | ||
while (++index < length) { | ||
while (++index < checks.length) { | ||
if (checks[index].apply(this, arguments)) { | ||
@@ -73,4 +63,2 @@ return true | ||
} | ||
return false | ||
} | ||
@@ -85,3 +73,3 @@ } | ||
function type(node) { | ||
return Boolean(node && node.type === test) | ||
return node && node.type === test | ||
} | ||
@@ -88,0 +76,0 @@ } |
19
index.js
@@ -11,28 +11,23 @@ 'use strict' | ||
// When a `parent` node is known the `index` of node should also be given. | ||
// eslint-disable-next-line max-params | ||
function is(node, test, index, parent, context) { | ||
var hasParent = parent !== null && parent !== undefined | ||
var hasIndex = index !== null && index !== undefined | ||
var check = convert(test) | ||
if ( | ||
hasIndex && | ||
index != null && | ||
(typeof index !== 'number' || index < 0 || index === Infinity) | ||
) { | ||
throw new Error('Expected positive finite index or child node') | ||
throw new Error('Expected positive finite index') | ||
} | ||
if (hasParent && (!is(parent) || !parent.children)) { | ||
if (parent != null && (!is(parent) || !parent.children)) { | ||
throw new Error('Expected parent node') | ||
} | ||
if (!node || !node.type || typeof node.type !== 'string') { | ||
return false | ||
} | ||
if (hasParent !== hasIndex) { | ||
if ((parent == null) !== (index == null)) { | ||
throw new Error('Expected both parent and index') | ||
} | ||
return Boolean(check.call(context, node, index, parent)) | ||
return node && node.type && typeof node.type === 'string' | ||
? Boolean(check.call(context, node, index, parent)) | ||
: false | ||
} |
{ | ||
"name": "unist-util-is", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"description": "unist utility to check if a node passes a test", | ||
@@ -40,15 +40,15 @@ "license": "MIT", | ||
"@types/mdast": "^3.0.0", | ||
"browserify": "^16.0.0", | ||
"dtslint": "^3.0.0", | ||
"browserify": "^17.0.0", | ||
"dtslint": "^4.0.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.0.0", | ||
"xo": "^0.26.0" | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^3.0.0", | ||
"unified": "^9.0.0", | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify . -s unistUtilIs > unist-util-is.js", | ||
@@ -74,2 +74,11 @@ "build-mangle": "browserify . -s unistUtilIs -p tinyify > unist-util-is.min.js", | ||
"rules": { | ||
"eqeqeq": [ | ||
"error", | ||
"always", | ||
{ | ||
"null": "ignore" | ||
} | ||
], | ||
"max-params": "off", | ||
"no-eq-null": "off", | ||
"unicorn/prefer-type-error": "off", | ||
@@ -79,2 +88,3 @@ "unicorn/prefer-reflect-apply": "off" | ||
"ignore": [ | ||
"*.ts", | ||
"unist-util-is.js" | ||
@@ -81,0 +91,0 @@ ] |
@@ -136,7 +136,5 @@ # unist-util-is | ||
— Find all nodes between two nodes | ||
* [`unist-util-find`](https://github.com/blahah/unist-util-find) | ||
— Find nodes matching a predicate | ||
* [`unist-util-filter`](https://github.com/eush77/unist-util-filter) | ||
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) | ||
— Create a new tree with nodes that pass a check | ||
* [`unist-util-remove`](https://github.com/eush77/unist-util-remove) | ||
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove) | ||
— Remove nodes from tree | ||
@@ -182,5 +180,5 @@ | ||
[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 | ||
@@ -193,7 +191,7 @@ [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 | ||
@@ -200,0 +198,0 @@ [unist]: https://github.com/syntax-tree/unist |
13439
144
204