snapdragon-util
Utilities for the snapdragon parser/compiler.
Table of Contents
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [About](#about)
Install
Install with npm:
$ npm install --save snapdragon-util
Usage
var util = require('snapdragon-util');
API
Returns true if the given value is a node.
Params
Example
var node = snapdragon.parser.node({type: 'foo'});
console.log(utils.isNode(node));
console.log(utils.isNode({}));
Emit an empty string for the given node
.
Params
Example
snapdragon.compiler.set('bos', utils.noop);
Emit val
for the given node. Useful when you know what needs to be emitted in advance and you don't need to access the actual node.
Params
Example
snapdragon.compiler
.set('i', function(node) {
this.mapVisit(node);
})
.set('i.open', utils.emit('<i>'))
.set('i.close', utils.emit('</i>'))
Converts an AST node into an empty text
node and deletes node.nodes
.
Params
Example
utils.toNoop(node);
utils.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}options
{Object}: Set options.recurse
to true call recursively call mapVisit
on node.nodes
.fn
{Function}returns
{Object}: returns the node
Example
snapdragon.compiler.set('i', function(node) {
utils.visit(node, function(node2) {
return node2;
});
});
Map visit with the given fn
over an array of AST nodes
.
Params
node
{Object}options
{Object}fn
{Function}returns
{Object}: returns the node
Example
snapdragon.compiler.set('i', function(node) {
utils.mapVisit(node, function(node2) {
return node2;
});
});
Wraps the given node
with *.open
and *.close
nodes.
Params
node
{Object}: (required)Node
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{undefined}
Unshift an *.open
node onto node.nodes
.
Params
node
{Object}Node
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{undefined}
Push a *.close
node onto node.nodes
.
Params
node
{Object}Node
{Function}: (required) Node constructor function from snapdragon-node.filter
{Function}: Optionaly specify a filter function to exclude the node.returns
{undefined}
Push the given node
onto parent.nodes
, and set parent
as `node.parent.
Params
parent
{Object}node
{Object}returns
{undefined}
Example
var parent = new Node({type: 'foo'});
var node = new Node({type: 'bar'});
utils.pushNode(parent, node);
console.log(parent.nodes[0].type)
console.log(node.parent.type)
Unshift node
onto parent.nodes
, and set parent
as `node.parent.
Params
parent
{Object}node
{Object}returns
{undefined}
Example
var parent = new Node({type: 'foo'});
var node = new Node({type: 'bar'});
utils.unshiftNode(parent, node);
console.log(parent.nodes[0].type)
console.log(node.parent.type)
Returns true if node
is a valid Node and node.type
matches the given type
.
Params
node
{Object}type
{String}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var node = new Node({type: 'foo'});
console.log(utils.isType(node, 'foo'));
console.log(utils.isType(node, 'bar'));
Returns true if the given node
has the given type
in node.nodes
.
Params
node
{Object}type
{String}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var node = new Node({
type: 'foo',
nodes: [
new Node({type: 'bar'}),
new Node({type: 'baz'})
]
});
console.log(utils.hasType(node, 'xyz'));
console.log(utils.hasType(node, 'baz'));
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
var node = new Node({
type: 'foo',
nodes: [
new Node({type: 'text', val: 'abc'}),
new Node({type: 'text', val: 'xyz'})
]
});
var textNode = utils.firstOfType(node.nodes, 'text');
console.log(textNode.val);
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
var node = new Node({
type: 'foo',
nodes: [
new Node({type: 'text', val: 'abc'}),
new Node({type: 'text', val: 'xyz'})
]
});
var nodeOne = utils.getNode(node.nodes, 'text');
console.log(nodeOne.val);
var nodeTwo = utils.getNode(node.nodes, 1);
console.log(nodeTwo.val);
Returns true if the given node is an "*.open" node.
Params
node
{Object}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var brace = new Node({type: 'brace'});
var open = new Node({type: 'brace.open'});
var close = new Node({type: 'brace.close'});
console.log(utils.isOpen(brace));
console.log(utils.isOpen(open));
console.log(utils.isOpen(close));
Returns true if the given node is a "*.close" node.
Params
node
{Object}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var brace = new Node({type: 'brace'});
var open = new Node({type: 'brace.open'});
var close = new Node({type: 'brace.close'});
console.log(utils.isClose(brace));
console.log(utils.isClose(open));
console.log(utils.isClose(close));
Returns true if node.nodes
has an .open
node
Params
node
{Object}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var brace = new Node({
type: 'brace',
nodes: []
});
var open = new Node({type: 'brace.open'});
console.log(utils.hasOpen(brace));
brace.addNode(open);
console.log(utils.hasOpen(brace));
Returns true if node.nodes
has a .close
node
Params
node
{Object}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var brace = new Node({
type: 'brace',
nodes: []
});
var close = new Node({type: 'brace.close'});
console.log(utils.hasClose(brace));
brace.addNode(close);
console.log(utils.hasClose(brace));
Returns true if node.nodes
has both .open
and .close
nodes
Params
node
{Object}returns
{Boolean}
Example
var Node = require('snapdragon-node');
var brace = new Node({
type: 'brace',
nodes: []
});
var open = new Node({type: 'brace.open'});
var close = new Node({type: 'brace.close'});
console.log(utils.hasOpen(brace));
console.log(utils.hasClose(brace));
brace.addNode(open);
brace.addNode(close);
console.log(utils.hasOpen(brace));
console.log(utils.hasClose(brace));
Push the given node
onto the state.inside
array for the
given type. This array is used as a "stack" for the given node.type
.
Params
state
{Object}: The compiler.state
object or custom state object.node
{Object}returns
{undefined}
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 val
to an array.
Params
Example
console.log(utils.arraify(''));
console.log(utils.arraify('foo'));
console.log(utils.arraify(['foo']));
Convert the given val
to a string by joining with ,
. Useful
for creating a cheerio/CSS/DOM-style selector from a list of strings.
Params
About
Contributing
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.
Building docs
(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:
$ npm install -g verbose/verb
Running tests
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:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.3, on March 04, 2017.