Gathering detailed insights and metrics for snapdragon-util
Gathering detailed insights and metrics for snapdragon-util
Utilities for the snapdragon parser/compiler.
npm install snapdragon-util
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.4
Supply Chain
100
Quality
78
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
4,309,632,029
Last Day
1,896,483
Last Week
10,172,549
Last Month
42,626,549
Last Year
548,056,731
MIT License
17 Stars
52 Commits
7 Forks
4 Watchers
1 Branches
4 Contributors
Updated on Aug 26, 2022
Minified
Minified + Gzipped
Latest Version
5.0.1
Package Id
snapdragon-util@5.0.1
Size
10.56 kB
NPM Version
5.6.0
Node Version
9.1.0
Published on
Jan 11, 2018
Cumulative downloads
Total Downloads
Last Day
6.1%
1,896,483
Compared to previous day
Last Week
2.7%
10,172,549
Compared to previous week
Last Month
1.9%
42,626,549
Compared to previous month
Last Year
-23.8%
548,056,731
Compared to previous year
1
Utilities for the snapdragon parser/compiler.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
1$ npm install --save snapdragon-util
1var util = require('snapdragon-util');
Returns true if the given value is a node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node'); 2var node = new Node({type: 'foo'}); 3console.log(utils.isNode(node)); //=> true 4console.log(utils.isNode({})); //=> false
Emit an empty string for the given node
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{undefined}Example
1// do nothing for beginning-of-string 2snapdragon.compiler.set('bos', utils.noop);
Returns node.value
or node.val
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{String}: returnsExample
1const star = new Node({type: 'star', value: '*'}); 2const slash = new Node({type: 'slash', val: '/'}); 3console.log(utils.value(star)) //=> '*' 4console.log(utils.value(slash)) //=> '/'
Append node.value
to compiler.output
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{undefined}Example
1snapdragon.compiler.set('text', utils.identity);
Previously named .emit
, this method appends the given value
to compiler.output
for the given node. Useful when you know what value should be appended advance, regardless of the actual value of node.value
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Function}: Returns a compiler middleware function.Example
1snapdragon.compiler 2 .set('i', function(node) { 3 this.mapVisit(node); 4 }) 5 .set('i.open', utils.append('<i>')) 6 .set('i.close', utils.append('</i>'))
Used in compiler middleware, this onverts an AST node into an empty text
node and deletes node.nodes
if it exists. The advantage of this method is that, as opposed to completely removing the node, indices will not need to be re-calculated in sibling nodes, and nothing is appended to the output.
Params
node
{Object}: Instance of snapdragon-nodenodes
{Array}: Optionally pass a new nodes
value, to replace the existing node.nodes
array.Example
1utils.toNoop(node);
2// convert `node.nodes` to the given value instead of deleting it
3utils.toNoop(node, []);
Visit node
with the given fn
. The built-in .visit
method in snapdragon automatically calls registered compilers, this allows you to pass a visitor function.
Params
node
{Object}: Instance of snapdragon-nodefn
{Function}returns
{Object}: returns the node after recursively visiting all child nodes.Example
1snapdragon.compiler.set('i', function(node) { 2 utils.visit(node, function(childNode) { 3 // do stuff with "childNode" 4 return childNode; 5 }); 6});
Map visit the given fn
over node.nodes
. This is called by visit, use this method if you do not want fn
to be called on the first node.
Params
node
{Object}: Instance of snapdragon-nodeoptions
{Object}fn
{Function}returns
{Object}: returns the nodeExample
1snapdragon.compiler.set('i', function(node) { 2 utils.mapVisit(node, function(childNode) { 3 // do stuff with "childNode" 4 return childNode; 5 }); 6});
Unshift an *.open
node onto node.nodes
.
Params
node
{Object}: Instance of snapdragon-nodeNode
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{Object}: Returns the created opening node.Example
1var Node = require('snapdragon-node'); 2snapdragon.parser.set('brace', function(node) { 3 var match = this.match(/^{/); 4 if (match) { 5 var parent = new Node({type: 'brace'}); 6 utils.addOpen(parent, Node); 7 console.log(parent.nodes[0]): 8 // { type: 'brace.open', value: '' }; 9 10 // push the parent "brace" node onto the stack 11 this.push(parent); 12 13 // return the parent node, so it's also added to the AST 14 return brace; 15 } 16});
Push a *.close
node onto node.nodes
.
Params
node
{Object}: Instance of snapdragon-nodeNode
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{Object}: Returns the created closing node.Example
1var Node = require('snapdragon-node'); 2snapdragon.parser.set('brace', function(node) { 3 var match = this.match(/^}/); 4 if (match) { 5 var parent = this.parent(); 6 if (parent.type !== 'brace') { 7 throw new Error('missing opening: ' + '}'); 8 } 9 10 utils.addClose(parent, Node); 11 console.log(parent.nodes[parent.nodes.length - 1]): 12 // { type: 'brace.close', value: '' }; 13 14 // no need to return a node, since the parent 15 // was already added to the AST 16 return; 17 } 18});
Wraps the given node
with *.open
and *.close
nodes.
Params
node
{Object}: Instance of snapdragon-nodeNode
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{Object}: Returns the nodePush the given node
onto parent.nodes
, and set parent
as `node.parent.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{Object}: Returns the child nodeExample
1var parent = new Node({type: 'foo'});
2var node = new Node({type: 'bar'});
3utils.pushNode(parent, node);
4console.log(parent.nodes[0].type) // 'bar'
5console.log(node.parent.type) // 'foo'
Unshift node
onto parent.nodes
, and set parent
as `node.parent.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{undefined}Example
1var parent = new Node({type: 'foo'});
2var node = new Node({type: 'bar'});
3utils.unshiftNode(parent, node);
4console.log(parent.nodes[0].type) // 'bar'
5console.log(node.parent.type) // 'foo'
Pop the last node
off of parent.nodes
. The advantage of using this method is that it checks for node.nodes
and works with any version of snapdragon-node
.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{Number|Undefined}: Returns the length of node.nodes
or undefined.Example
1var parent = new Node({type: 'foo'});
2utils.pushNode(parent, new Node({type: 'foo'}));
3utils.pushNode(parent, new Node({type: 'bar'}));
4utils.pushNode(parent, new Node({type: 'baz'}));
5console.log(parent.nodes.length); //=> 3
6utils.popNode(parent);
7console.log(parent.nodes.length); //=> 2
Shift the first node
off of parent.nodes
. The advantage of using this method is that it checks for node.nodes
and works with any version of snapdragon-node
.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{Number|Undefined}: Returns the length of node.nodes
or undefined.Example
1var parent = new Node({type: 'foo'});
2utils.pushNode(parent, new Node({type: 'foo'}));
3utils.pushNode(parent, new Node({type: 'bar'}));
4utils.pushNode(parent, new Node({type: 'baz'}));
5console.log(parent.nodes.length); //=> 3
6utils.shiftNode(parent);
7console.log(parent.nodes.length); //=> 2
Remove the specified node
from parent.nodes
.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{Object|undefined}: Returns the removed node, if successful, or undefined if it does not exist on parent.nodes
.Example
1var parent = new Node({type: 'abc'});
2var foo = new Node({type: 'foo'});
3utils.pushNode(parent, foo);
4utils.pushNode(parent, new Node({type: 'bar'}));
5utils.pushNode(parent, new Node({type: 'baz'}));
6console.log(parent.nodes.length); //=> 3
7utils.removeNode(parent, foo);
8console.log(parent.nodes.length); //=> 2
Returns true if node.type
matches the given type
. Throws a TypeError
if node
is not an instance of Node
.
Params
node
{Object}: Instance of snapdragon-nodetype
{String}returns
{Boolean}Example
1var Node = require('snapdragon-node'); 2var node = new Node({type: 'foo'}); 3console.log(utils.isType(node, 'foo')); // false 4console.log(utils.isType(node, 'bar')); // true
Returns true if the given node
has the given type
in node.nodes
. Throws a TypeError
if node
is not an instance of Node
.
Params
node
{Object}: Instance of snapdragon-nodetype
{String}returns
{Boolean}Example
1var Node = require('snapdragon-node'); 2var node = new Node({ 3 type: 'foo', 4 nodes: [ 5 new Node({type: 'bar'}), 6 new Node({type: 'baz'}) 7 ] 8}); 9console.log(utils.hasType(node, 'xyz')); // false 10console.log(utils.hasType(node, 'baz')); // true
Returns the first node from node.nodes
of the given type
Params
nodes
{Array}type
{String}returns
{Object|undefined}: Returns the first matching node or undefined.Example
1var node = new Node({
2 type: 'foo',
3 nodes: [
4 new Node({type: 'text', value: 'abc'}),
5 new Node({type: 'text', value: 'xyz'})
6 ]
7});
8
9var textNode = utils.firstOfType(node.nodes, 'text');
10console.log(textNode.value);
11//=> 'abc'
Returns the node at the specified index, or the first node of the given type
from node.nodes
.
Params
nodes
{Array}type
{String|Number}: Node type or index.returns
{Object}: Returns a node or undefined.Example
1var node = new Node({
2 type: 'foo',
3 nodes: [
4 new Node({type: 'text', value: 'abc'}),
5 new Node({type: 'text', value: 'xyz'})
6 ]
7});
8
9var nodeOne = utils.findNode(node.nodes, 'text');
10console.log(nodeOne.value);
11//=> 'abc'
12
13var nodeTwo = utils.findNode(node.nodes, 1);
14console.log(nodeTwo.value);
15//=> 'xyz'
Returns true if the given node is an "*.open" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({type: 'brace'});
3var open = new Node({type: 'brace.open'});
4var close = new Node({type: 'brace.close'});
5
6console.log(utils.isOpen(brace)); // false
7console.log(utils.isOpen(open)); // true
8console.log(utils.isOpen(close)); // false
Returns true if the given node is a "*.close" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({type: 'brace'});
3var open = new Node({type: 'brace.open'});
4var close = new Node({type: 'brace.close'});
5
6console.log(utils.isClose(brace)); // false
7console.log(utils.isClose(open)); // false
8console.log(utils.isClose(close)); // true
Returns true if the given node is an "*.open" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({type: 'brace'});
3var open = new Node({type: 'brace.open', value: '{'});
4var inner = new Node({type: 'text', value: 'a,b,c'});
5var close = new Node({type: 'brace.close', value: '}'});
6brace.push(open);
7brace.push(inner);
8brace.push(close);
9
10console.log(utils.isBlock(brace)); // true
Returns true if parent.nodes
has the given node
.
Params
type
{String}returns
{Boolean}Example
1const foo = new Node({type: 'foo'});
2const bar = new Node({type: 'bar'});
3cosole.log(util.hasNode(foo, bar)); // false
4foo.push(bar);
5cosole.log(util.hasNode(foo, bar)); // true
Returns true if node.nodes
has an .open
node
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({
3 type: 'brace',
4 nodes: []
5});
6
7var open = new Node({type: 'brace.open'});
8console.log(utils.hasOpen(brace)); // false
9
10brace.pushNode(open);
11console.log(utils.hasOpen(brace)); // true
Returns true if node.nodes
has a .close
node
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({
3 type: 'brace',
4 nodes: []
5});
6
7var close = new Node({type: 'brace.close'});
8console.log(utils.hasClose(brace)); // false
9
10brace.pushNode(close);
11console.log(utils.hasClose(brace)); // true
Returns true if node.nodes
has both .open
and .close
nodes
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}Example
1var Node = require('snapdragon-node');
2var brace = new Node({
3 type: 'brace',
4 nodes: []
5});
6
7var open = new Node({type: 'brace.open'});
8var close = new Node({type: 'brace.close'});
9console.log(utils.hasOpen(brace)); // false
10console.log(utils.hasClose(brace)); // false
11
12brace.pushNode(open);
13brace.pushNode(close);
14console.log(utils.hasOpen(brace)); // true
15console.log(utils.hasClose(brace)); // true
Push the given node
onto the state.inside
array for the given type. This array is used as a specialized "stack" for only the given node.type
.
Params
state
{Object}: The compiler.state
object or custom state object.node
{Object}: Instance of snapdragon-nodereturns
{Array}: Returns the state.inside
stack for the given type.Example
1var state = { inside: {}}; 2var node = new Node({type: 'brace'}); 3utils.addType(state, node); 4console.log(state.inside); 5//=> { brace: [{type: 'brace'}] }
Remove the given node
from the state.inside
array for the given type. This array is used as a specialized "stack" for only the given node.type
.
Params
state
{Object}: The compiler.state
object or custom state object.node
{Object}: Instance of snapdragon-nodereturns
{Array}: Returns the state.inside
stack for the given type.Example
1var state = { inside: {}}; 2var node = new Node({type: 'brace'}); 3utils.addType(state, node); 4console.log(state.inside); 5//=> { brace: [{type: 'brace'}] } 6utils.removeType(state, node); 7//=> { brace: [] }
Returns true if node.value
is an empty string, or node.nodes
does not contain any non-empty text nodes.
Params
node
{Object}: Instance of snapdragon-nodefn
{Function}returns
{Boolean}Example
1var node = new Node({type: 'text'}); 2utils.isEmpty(node); //=> true 3node.value = 'foo'; 4utils.isEmpty(node); //=> false
Returns true if the state.inside
stack for the given type exists and has one or more nodes on it.
Params
state
{Object}type
{String}returns
{Boolean}Example
1var state = { inside: {}}; 2var node = new Node({type: 'brace'}); 3console.log(utils.isInsideType(state, 'brace')); //=> false 4utils.addType(state, node); 5console.log(utils.isInsideType(state, 'brace')); //=> true 6utils.removeType(state, node); 7console.log(utils.isInsideType(state, 'brace')); //=> false
Returns true if node
is either a child or grand-child of the given type
, or state.inside[type]
is a non-empty array.
Params
state
{Object}: Either the compiler.state
object, if it exists, or a user-supplied state object.node
{Object}: Instance of snapdragon-nodetype
{String}: The node.type
to check for.returns
{Boolean}Example
1var state = { inside: {}};
2var node = new Node({type: 'brace'});
3var open = new Node({type: 'brace.open'});
4console.log(utils.isInside(state, open, 'brace')); //=> false
5utils.pushNode(node, open);
6console.log(utils.isInside(state, open, 'brace')); //=> true
Get the last n
element from the given array
. Used for getting
a node from node.nodes.
Params
array
{Array}n
{Number}returns
{undefined}Cast the given value
to an array.
Params
value
{any}returns
{Array}Example
1console.log(utils.arrayify('')); 2//=> [] 3console.log(utils.arrayify('foo')); 4//=> ['foo'] 5console.log(utils.arrayify(['foo'])); 6//=> ['foo']
Convert the given value
to a string by joining with ,
. Useful
for creating a cheerio/CSS/DOM-style selector from a list of strings.
Params
value
{any}returns
{Array}Ensure that the given value is a string and call .trim()
on it,
or return an empty string.
Params
str
{String}returns
{String}Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Commits | Contributor |
---|---|
43 | jonschlinkert |
2 | realityking |
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 11, 2018.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-03-10
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 More