snapdragon-util
Utilities for the snapdragon parser/compiler.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
$ npm install --save snapdragon-util
Usage
var util = ;
API
.isNode
Returns true if the given value is a node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var node = type: 'foo';console; //=> trueconsole; //=> false
.noop
Emit an empty string for the given node
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{undefined}
Example
// do nothing for beginning-of-stringsnapdragoncompiler;
.value
Returns node.value
or node.val
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{String}: returns
Example
const star = type: 'star' value: '*';const slash = type: 'slash' val: '/';console //=> '*'console //=> '/'
.identity
Append node.value
to compiler.output
.
Params
node
{Object}: Instance of snapdragon-nodereturns
{undefined}
Example
snapdragoncompiler;
.append
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
snapdragoncompiler
.toNoop
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 newnodes
value, to replace the existingnode.nodes
array.
Example
utils;// convert `node.nodes` to the given value instead of deleting itutils;
.visit
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
snapdragoncompiler;
.mapVisit
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 node
Example
snapdragoncompiler;
.addOpen
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
var Node = ;snapdragonparser;
.addClose
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
var Node = ;snapdragonparser;
.wrapNodes
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 node
.pushNode
Push 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 node
Example
var parent = type: 'foo';var node = type: 'bar';utils;console // 'bar'console // 'foo'
.unshiftNode
Unshift node
onto parent.nodes
, and set parent
as `node.parent.
Params
parent
{Object}node
{Object}: Instance of snapdragon-nodereturns
{undefined}
Example
var parent = type: 'foo';var node = type: 'bar';utils;console // 'bar'console // 'foo'
.popNode
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 ofnode.nodes
or undefined.
Example
var parent = type: 'foo';utils;utils;utils;console; //=> 3utils;console; //=> 2
.shiftNode
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 ofnode.nodes
or undefined.
Example
var parent = type: 'foo';utils;utils;utils;console; //=> 3utils;console; //=> 2
.removeNode
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 onparent.nodes
.
Example
var parent = type: 'abc';var foo = type: 'foo';utils;utils;utils;console; //=> 3utils;console; //=> 2
.isType
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
var Node = ;var node = type: 'foo';console; // falseconsole; // true
.hasType
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
var Node = ;var node = type: 'foo' nodes: type: 'bar' type: 'baz' ;console; // falseconsole; // true
.firstOfType
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 = type: 'foo' nodes: type: 'text' value: 'abc' type: 'text' value: 'xyz' ; var textNode = utils;console;//=> 'abc'
.findNode
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 = type: 'foo' nodes: type: 'text' value: 'abc' type: 'text' value: 'xyz' ; var nodeOne = utils;console;//=> 'abc' var nodeTwo = utils;console;//=> 'xyz'
.isOpen
Returns true if the given node is an "*.open" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace';var open = type: 'brace.open';var close = type: 'brace.close'; console; // falseconsole; // trueconsole; // false
.isClose
Returns true if the given node is a "*.close" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace';var open = type: 'brace.open';var close = type: 'brace.close'; console; // falseconsole; // falseconsole; // true
.isBlock
Returns true if the given node is an "*.open" node.
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace';var open = type: 'brace.open' value: '{';var inner = type: 'text' value: 'a,b,c';var close = type: 'brace.close' value: '}';brace;brace;brace; console; // true
.hasNode
Returns true if parent.nodes
has the given node
.
Params
type
{String}returns
{Boolean}
Example
const foo = type: 'foo';const bar = type: 'bar';cosole; // falsefoo;cosole; // true
.hasOpen
Returns true if node.nodes
has an .open
node
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace' nodes: ; var open = type: 'brace.open';console; // false brace;console; // true
.hasClose
Returns true if node.nodes
has a .close
node
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace' nodes: ; var close = type: 'brace.close';console; // false brace;console; // true
.hasOpenAndClose
Returns true if node.nodes
has both .open
and .close
nodes
Params
node
{Object}: Instance of snapdragon-nodereturns
{Boolean}
Example
var Node = ;var brace = type: 'brace' nodes: ; var open = type: 'brace.open';var close = type: 'brace.close';console; // falseconsole; // false brace;brace;console; // trueconsole; // true
.addType
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}: Thecompiler.state
object or custom state object.node
{Object}: Instance of snapdragon-nodereturns
{Array}: Returns thestate.inside
stack for the given type.
Example
var state = inside: {};var node = type: 'brace';utils;console;//=> { brace: [{type: 'brace'}] }
.removeType
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}: Thecompiler.state
object or custom state object.node
{Object}: Instance of snapdragon-nodereturns
{Array}: Returns thestate.inside
stack for the given type.
Example
var state = inside: {};var node = type: 'brace';utils;console;//=> { brace: [{type: 'brace'}] }utils;//=> { brace: [] }
.isEmpty
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
var node = type: 'text';utils; //=> truenodevalue = 'foo';utils; //=> false
.isInsideType
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
var state = inside: {};var node = type: 'brace';console; //=> falseutils;console; //=> trueutils;console; //=> false
.isInside
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 thecompiler.state
object, if it exists, or a user-supplied state object.node
{Object}: Instance of snapdragon-nodetype
{String}: Thenode.type
to check for.returns
{Boolean}
Example
var state = inside: {};var node = type: 'brace';var open = type: 'brace.open';console; //=> falseutils;console; //=> true
.last
Get the last n
element from the given array
. Used for getting
a node from node.nodes.
Params
array
{Array}n
{Number}returns
{undefined}
.arrayify
Cast the given value
to an array.
Params
value
{any}returns
{Array}
Example
console;//=> []console;//=> ['foo']console;//=> ['foo']
.stringify
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}
.trim
Ensure that the given value is a string and call .trim()
on it,
or return an empty string.
Params
str
{String}returns
{String}
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.
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
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#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- snapdragon-node: Snapdragon utility for creating a new AST node in custom code, such as plugins. | homepage
- snapdragon-position: Snapdragon util and plugin for patching the position on an AST node. | homepage
- snapdragon-token: Create a snapdragon token. Used by the snapdragon lexer, but can also be used by… more | homepage
Contributors
Commits | Contributor |
---|---|
43 | jonschlinkert |
2 | realityking |
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on January 11, 2018.