From 2ac6b804c42eba6cce804ff5984a71e192836aee Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 28 May 2019 22:06:04 +0200 Subject: [PATCH 1/2] Add `is.convert` to convert a test to a function Useful for reuse. Closes GH-5. --- convert.js | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 88 +++------------------------------------------------- package.json | 3 +- readme.md | 8 +++++ 4 files changed, 101 insertions(+), 85 deletions(-) create mode 100644 convert.js 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..9bad9cf 100644 --- a/readme.md +++ b/readme.md @@ -83,6 +83,14 @@ 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')`. + ## Related * [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after) From f995fc7f887bf1998444491809af070772432782 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 29 May 2019 10:04:34 +0200 Subject: [PATCH 2/2] Add example --- readme.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/readme.md b/readme.md index 9bad9cf..1d6d30d 100644 --- a/readme.md +++ b/readme.md @@ -89,8 +89,35 @@ 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)