Home

Awesome

snapdragon-util NPM version NPM monthly downloads NPM total downloads Linux Build Status

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

Install with npm:

$ npm install --save snapdragon-util

Usage

var util = require('snapdragon-util');

API

.isNode

Returns true if the given value is a node.

Params

Example

var Node = require('snapdragon-node');
var node = new Node({type: 'foo'});
console.log(utils.isNode(node)); //=> true
console.log(utils.isNode({})); //=> false

.noop

Emit an empty string for the given node.

Params

Example

// do nothing for beginning-of-string
snapdragon.compiler.set('bos', utils.noop);

.value

Returns node.value or node.val.

Params

Example

const star = new Node({type: 'star', value: '*'});
const slash = new Node({type: 'slash', val: '/'});
console.log(utils.value(star)) //=> '*'
console.log(utils.value(slash)) //=> '/'

.identity

Append node.value to compiler.output.

Params

Example

snapdragon.compiler.set('text', utils.identity);

.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

Example

snapdragon.compiler
  .set('i', function(node) {
    this.mapVisit(node);
  })
  .set('i.open', utils.append('<i>'))
  .set('i.close', utils.append('</i>'))

.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

Example

utils.toNoop(node);
// convert `node.nodes` to the given value instead of deleting it
utils.toNoop(node, []);

.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

Example

snapdragon.compiler.set('i', function(node) {
  utils.visit(node, function(childNode) {
    // do stuff with "childNode"
    return childNode;
  });
});

.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

Example

snapdragon.compiler.set('i', function(node) {
  utils.mapVisit(node, function(childNode) {
    // do stuff with "childNode"
    return childNode;
  });
});

.addOpen

Unshift an *.open node onto node.nodes.

Params

Example

var Node = require('snapdragon-node');
snapdragon.parser.set('brace', function(node) {
  var match = this.match(/^{/);
  if (match) {
    var parent = new Node({type: 'brace'});
    utils.addOpen(parent, Node);
    console.log(parent.nodes[0]):
    // { type: 'brace.open', value: '' };

    // push the parent "brace" node onto the stack
    this.push(parent);

    // return the parent node, so it's also added to the AST
    return brace;
  }
});

.addClose

Push a *.close node onto node.nodes.

Params

Example

var Node = require('snapdragon-node');
snapdragon.parser.set('brace', function(node) {
  var match = this.match(/^}/);
  if (match) {
    var parent = this.parent();
    if (parent.type !== 'brace') {
      throw new Error('missing opening: ' + '}');
    }

    utils.addClose(parent, Node);
    console.log(parent.nodes[parent.nodes.length - 1]):
    // { type: 'brace.close', value: '' };

    // no need to return a node, since the parent
    // was already added to the AST
    return;
  }
});

.wrapNodes

Wraps the given node with *.open and *.close nodes.

Params

.pushNode

Push the given node onto parent.nodes, and set parent as `node.parent.

Params

Example

var parent = new Node({type: 'foo'});
var node = new Node({type: 'bar'});
utils.pushNode(parent, node);
console.log(parent.nodes[0].type) // 'bar'
console.log(node.parent.type) // 'foo'

.unshiftNode

Unshift node onto parent.nodes, and set parent as `node.parent.

Params

Example

var parent = new Node({type: 'foo'});
var node = new Node({type: 'bar'});
utils.unshiftNode(parent, node);
console.log(parent.nodes[0].type) // 'bar'
console.log(node.parent.type) // '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

Example

var parent = new Node({type: 'foo'});
utils.pushNode(parent, new Node({type: 'foo'}));
utils.pushNode(parent, new Node({type: 'bar'}));
utils.pushNode(parent, new Node({type: 'baz'}));
console.log(parent.nodes.length); //=> 3
utils.popNode(parent);
console.log(parent.nodes.length); //=> 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

Example

var parent = new Node({type: 'foo'});
utils.pushNode(parent, new Node({type: 'foo'}));
utils.pushNode(parent, new Node({type: 'bar'}));
utils.pushNode(parent, new Node({type: 'baz'}));
console.log(parent.nodes.length); //=> 3
utils.shiftNode(parent);
console.log(parent.nodes.length); //=> 2

.removeNode

Remove the specified node from parent.nodes.

Params

Example

var parent = new Node({type: 'abc'});
var foo = new Node({type: 'foo'});
utils.pushNode(parent, foo);
utils.pushNode(parent, new Node({type: 'bar'}));
utils.pushNode(parent, new Node({type: 'baz'}));
console.log(parent.nodes.length); //=> 3
utils.removeNode(parent, foo);
console.log(parent.nodes.length); //=> 2

.isType

Returns true if node.type matches the given type. Throws a TypeError if node is not an instance of Node.

Params

Example

var Node = require('snapdragon-node');
var node = new Node({type: 'foo'});
console.log(utils.isType(node, 'foo')); // false
console.log(utils.isType(node, 'bar')); // 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

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')); // false
console.log(utils.hasType(node, 'baz')); // true

.firstOfType

Returns the first node from node.nodes of the given type

Params

Example

var node = new Node({
  type: 'foo',
  nodes: [
    new Node({type: 'text', value: 'abc'}),
    new Node({type: 'text', value: 'xyz'})
  ]
});

var textNode = utils.firstOfType(node.nodes, 'text');
console.log(textNode.value);
//=> 'abc'

.findNode

Returns the node at the specified index, or the first node of the given type from node.nodes.

Params

Example

var node = new Node({
  type: 'foo',
  nodes: [
    new Node({type: 'text', value: 'abc'}),
    new Node({type: 'text', value: 'xyz'})
  ]
});

var nodeOne = utils.findNode(node.nodes, 'text');
console.log(nodeOne.value);
//=> 'abc'

var nodeTwo = utils.findNode(node.nodes, 1);
console.log(nodeTwo.value);
//=> 'xyz'

.isOpen

Returns true if the given node is an "*.open" node.

Params

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)); // false
console.log(utils.isOpen(open)); // true
console.log(utils.isOpen(close)); // false

.isClose

Returns true if the given node is a "*.close" node.

Params

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)); // false
console.log(utils.isClose(open)); // false
console.log(utils.isClose(close)); // true

.isBlock

Returns true if the given node is an "*.open" node.

Params

Example

var Node = require('snapdragon-node');
var brace = new Node({type: 'brace'});
var open = new Node({type: 'brace.open', value: '{'});
var inner = new Node({type: 'text', value: 'a,b,c'});
var close = new Node({type: 'brace.close', value: '}'});
brace.push(open);
brace.push(inner);
brace.push(close);

console.log(utils.isBlock(brace)); // true

.hasNode

Returns true if parent.nodes has the given node.

Params

Example

const foo = new Node({type: 'foo'});
const bar = new Node({type: 'bar'});
cosole.log(util.hasNode(foo, bar)); // false
foo.push(bar);
cosole.log(util.hasNode(foo, bar)); // true

.hasOpen

Returns true if node.nodes has an .open node

Params

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)); // false

brace.pushNode(open);
console.log(utils.hasOpen(brace)); // true

.hasClose

Returns true if node.nodes has a .close node

Params

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)); // false

brace.pushNode(close);
console.log(utils.hasClose(brace)); // true

.hasOpenAndClose

Returns true if node.nodes has both .open and .close nodes

Params

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)); // false
console.log(utils.hasClose(brace)); // false

brace.pushNode(open);
brace.pushNode(close);
console.log(utils.hasOpen(brace)); // true
console.log(utils.hasClose(brace)); // 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

Example

var state = { inside: {}};
var node = new Node({type: 'brace'});
utils.addType(state, node);
console.log(state.inside);
//=> { 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

Example

var state = { inside: {}};
var node = new Node({type: 'brace'});
utils.addType(state, node);
console.log(state.inside);
//=> { brace: [{type: 'brace'}] }
utils.removeType(state, node);
//=> { brace: [] }

.isEmpty

Returns true if node.value is an empty string, or node.nodes does not contain any non-empty text nodes.

Params

Example

var node = new Node({type: 'text'});
utils.isEmpty(node); //=> true
node.value = 'foo';
utils.isEmpty(node); //=> false

.isInsideType

Returns true if the state.inside stack for the given type exists and has one or more nodes on it.

Params

Example

var state = { inside: {}};
var node = new Node({type: 'brace'});
console.log(utils.isInsideType(state, 'brace')); //=> false
utils.addType(state, node);
console.log(utils.isInsideType(state, 'brace')); //=> true
utils.removeType(state, node);
console.log(utils.isInsideType(state, 'brace')); //=> 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

Example

var state = { inside: {}};
var node = new Node({type: 'brace'});
var open = new Node({type: 'brace.open'});
console.log(utils.isInside(state, open, 'brace')); //=> false
utils.pushNode(node, open);
console.log(utils.isInside(state, open, 'brace')); //=> true

.last

Get the last n element from the given array. Used for getting a node from node.nodes.

Params

.arrayify

Cast the given value to an array.

Params

Example

console.log(utils.arrayify(''));
//=> []
console.log(utils.arrayify('foo'));
//=> ['foo']
console.log(utils.arrayify(['foo']));
//=> ['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

.trim

Ensure that the given value is a string and call .trim() on it, or return an empty string.

Params

About

<details> <summary><strong>Contributing</strong></summary>

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.

</details> <details> <summary><strong>Running Tests</strong></summary>

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
</details> <details> <summary><strong>Building docs</strong></summary>

(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
</details>

Related projects

You might also be interested in these projects:

Contributors

CommitsContributor
43jonschlinkert
2realityking

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.