Gathering detailed insights and metrics for unist-util-is
Gathering detailed insights and metrics for unist-util-is
Gathering detailed insights and metrics for unist-util-is
Gathering detailed insights and metrics for unist-util-is
npm install unist-util-is
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
38 Stars
127 Commits
8 Forks
10 Watching
1 Branches
14 Contributors
Updated on 09 Nov 2024
JavaScript (68.92%)
TypeScript (31.08%)
Cumulative downloads
Total Downloads
Last day
-4.9%
3,124,912
Compared to previous day
Last week
3.3%
16,767,002
Compared to previous week
Last month
12.5%
70,926,859
Compared to previous month
Last year
31%
743,033,938
Compared to previous year
1
unist utility to check if nodes pass a test.
This package is a small utility that checks that a node is a certain node.
Use this small utility if you find yourself repeating code for checking what nodes are.
A similar package, hast-util-is-element
, works on hast
elements.
For more advanced tests, unist-util-select
can be used
to match against CSS selectors.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install unist-util-is
In Deno with esm.sh
:
1import {is} from 'https://esm.sh/unist-util-is@6'
In browsers with esm.sh
:
1<script type="module"> 2 import {is} from 'https://esm.sh/unist-util-is@6?bundle' 3</script>
1import {is} from 'unist-util-is' 2 3const node = {type: 'strong'} 4const parent = {type: 'paragraph', children: [node]} 5 6is() // => false 7is({children: []}) // => false 8is(node) // => true 9is(node, 'strong') // => true 10is(node, 'emphasis') // => false 11 12is(node, node) // => true 13is(parent, {type: 'paragraph'}) // => true 14is(parent, {type: 'strong'}) // => false 15 16is(node, test) // => false 17is(node, test, 4, parent) // => false 18is(node, test, 5, parent) // => true 19 20function test(node, n) { 21 return n === 5 22}
This package exports the identifiers convert
and
is
.
There is no default export.
is(node[, test[, index, parent[, context]]])
Check if node
is a Node
and whether it passes the given test.
node
(unknown
, optional)
— thing to check, typically Node
test
(Test
, optional)
— a test for a specific elementindex
(number
, optional)
— the node’s position in its parentparent
(Node
, optional)
— the node’s parentcontext
(unknown
, optional)
— context object (this
) to call test
withWhether node
is a Node
and passes a test (boolean
).
When an incorrect test
, index
, or parent
is given.
There is no error thrown when node
is not a node.
convert(test)
Generate a check from a test.
Useful if you’re going to test many nodes, for example when creating a utility where something else passes a compatible test.
The created function is a bit faster because it expects valid input only:
a node
, index
, and parent
.
test
(Test
, optional)
— a test for a specific nodeA check (Check
).
Check
Check that an arbitrary value is a node (TypeScript type).
this
(unknown
, optional)
— context object (this
) to call test
withnode
(unknown
)
— anything (typically a node)index
(number
, optional)
— the node’s position in its parentparent
(Node
, optional)
— the node’s parentWhether this is a node and passes a test (boolean
).
Test
Check for an arbitrary node (TypeScript type).
1type Test = 2 | Array<Record<string, unknown> | TestFunction | string> 3 | Record<string, unknown> 4 | TestFunction 5 | string 6 | null 7 | undefined
Checks that the given thing is a node, and then:
string
, checks that the node has that tag namefunction
, see TestFunction
object
, checks that all keys in test are in node, and that they have
(strictly) equal valuesArray
, checks if one of the subtests passTestFunction
Check if a node passes a test (TypeScript type).
node
(Node
)
— a nodeindex
(number
or undefined
)
— the node’s position in its parentparent
(Node
or undefined
)
— the node’s parentWhether this node passes the test (boolean
, optional).
convert
1import {u} from 'unist-builder' 2import {convert} from 'unist-util-is' 3 4const test = convert('leaf') 5 6const tree = u('tree', [ 7 u('node', [u('leaf', '1')]), 8 u('leaf', '2'), 9 u('node', [u('leaf', '3'), u('leaf', '4')]), 10 u('leaf', '5') 11]) 12 13const leafs = tree.children.filter(function (child, index) { 14 return test(child, index, tree) 15}) 16 17console.log(leafs)
Yields:
1[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]
This package is fully typed with TypeScript.
It exports the additional types Check
,
Test
,
TestFunction
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, unist-util-is@^6
,
compatible with Node.js 16.
unist-util-find-after
— find a node after another nodeunist-util-find-before
— find a node before another nodeunist-util-find-all-after
— find all nodes after another nodeunist-util-find-all-before
— find all nodes before another nodeunist-util-find-all-between
— find all nodes between two nodesunist-util-filter
— create a new tree with nodes that pass a checkunist-util-remove
— remove nodes from treeSee contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
MIT © Titus Wormer
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moreunist-util-visit-parents
unist utility to recursively walk over nodes, with ancestral information
unist-util-stringify-position
unist utility to serialize a node, position, or point as a human readable location
unist-util-visit
unist utility to visit nodes
unist-util-position
unist utility to get the position of a node