diff --git a/convert.js b/convert.js new file mode 100644 index 0000000..f92f34f --- /dev/null +++ b/convert.js @@ -0,0 +1,87 @@ +'use strict' + +module.exports = convert + +function convert(test) { + if (typeof test === 'string') { + return typeFactory(test) + } + + if (test === null || test === undefined) { + return ok + } + + if (typeof test === 'object') { + return ('length' in test ? anyFactory : matchesFactory)(test) + } + + if (typeof test === 'function') { + return test + } + + throw new Error('Expected function, string, or object as test') +} + +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 matches(node) { + var key + + for (key in test) { + if (node[key] !== test[key]) { + return false + } + } + + return true + } +} + +function anyFactory(tests) { + var checks = convertAll(tests) + var length = checks.length + + return matches + + function matches() { + var index = -1 + + while (++index < length) { + if (checks[index].apply(this, arguments)) { + return true + } + } + + return false + } +} + +// Utility to convert a string into a function which checks a given node’s type +// for said string. +function typeFactory(test) { + return type + + function type(node) { + return Boolean(node && node.type === test) + } +} + +// Utility to return true. +function ok() { + return true +} diff --git a/index.js b/index.js index 1841150..505933a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ 'use strict' +var convert = require('./convert') + module.exports = is +is.convert = convert + // Assert if `test` passes for `node`. When a `parent` node is known the // `index` of node. // eslint-disable-next-line max-params @@ -31,87 +35,3 @@ function is(test, node, index, parent, context) { return Boolean(check.call(context, node, index, parent)) } - -function convert(test) { - if (typeof test === 'string') { - return typeFactory(test) - } - - if (test === null || test === undefined) { - return ok - } - - if (typeof test === 'object') { - return ('length' in test ? anyFactory : matchesFactory)(test) - } - - if (typeof test === 'function') { - return test - } - - throw new Error('Expected function, string, or object as test') -} - -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 matches(node) { - var key - - for (key in test) { - if (node[key] !== test[key]) { - return false - } - } - - return true - } -} - -function anyFactory(tests) { - var checks = convertAll(tests) - var length = checks.length - - return matches - - function matches() { - var index = -1 - - while (++index < length) { - if (checks[index].apply(this, arguments)) { - return true - } - } - - return false - } -} - -// Utility to convert a string into a function which checks a given node’s type -// for said string. -function typeFactory(test) { - return type - - function type(node) { - return Boolean(node && node.type === test) - } -} - -// Utility to return true. -function ok() { - return true -} diff --git a/package.json b/package.json index aa82197..e495bb8 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "Titus Wormer (https://wooorm.com)" ], "files": [ - "index.js" + "index.js", + "convert.js" ], "dependencies": {}, "devDependencies": { diff --git a/readme.md b/readme.md index 3ed2fce..1d6d30d 100644 --- a/readme.md +++ b/readme.md @@ -83,6 +83,41 @@ with `type` set to a non-empty `string`). `boolean?` — Whether `node` matches. +### `is.convert(test)` + +Create a test function from `test`, that can later be called with a `node`, +`index`, and `parent`. +Useful if you’re going to test many nodes, for example when creating a utility +where something else passes an is-compatible test. + +Can also be accessed with `require('unist-util-is/convert')`. + +For example: + +```js +var u = require('unist-builder') +var convert = require('unist-util-is/convert') + +var test = convert('leaf') + +var tree = u('tree', [ + u('node', [u('leaf', '1')]), + u('leaf', '2'), + u('node', [u('leaf', '3'), u('leaf', '4')]), + u('leaf', '5') +]) + +var leafs = tree.children.filter((child, index) => test(child, index, tree)) + +console.log(leafs) +``` + +Yields: + +```js +[({type: 'leaf', value: '2'}, {type: 'leaf', value: '5'})] +``` + ## Related * [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)