Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions convert.js
Original file line number Diff line number Diff line change
@@ -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
}
88 changes: 4 additions & 84 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"convert.js"
],
"dependencies": {},
"devDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down