sfcode: snapdragon-util <a href="https://www.npmjs.com/package/snapdragon-util"><img src="https://img.shields.io/npm/v/snapdragon-util.svg?style=flat" alt="NPM version"/></a> <a href="https://npmjs.org/package/snapdragon-util"><img src="https://img.shields.io/npm/dm/snapdragon-util.svg?style=flat" alt="NPM monthly downloads"/></a> <a href="https://npmjs.org/package/snapdragon-util"><img src="https://img.shields.io/npm/dt/snapdragon-util.svg?style=flat" alt="NPM total downloads"/></a> <a href="https://travis-ci.org/jonschlinkert/snapdragon-util"><img src="https://img.shields.io/travis/jonschlinkert/snapdragon-util.svg?style=flat&label=Travis" alt="Linux Build Status"/></a>

sfcode An Online Competing and Development Environment

snapdragon-util

Table of Contents

> Utilities for the snapdragon parser/compiler.

<details>

- Install
- Usage
- API
- Release history
  - [3.0.0] - 2017-05-01
  - [0.1.0]
- About

</details>

# Install

Install with npm:

$ npm install --save snapdragon-util

Install with yarn:

$ yarn add snapdragon-util

# Usage

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

# API

## <a href="index.js#L21">.isNode</a>

Returns true if the given value is a node.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{Boolean}**

Example

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

## <a href="index.js#L37">.noop</a>

Emit an empty string for the given `node`.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{undefined}**

Example

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

## <a href="index.js#L53">.identity</a>

Appdend `node.val` to `compiler.output`, exactly as it was created by the parser.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{undefined}**

Example

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

## <a href="index.js#L76">.append</a>

Previously named `.emit`, this method appends the given `val` to `compiler.output` for the given node. Useful when you know what value should be appended advance, regardless of the actual value of `node.val`.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{Function}**: Returns a compiler middleware function.

Example

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

## <a href="index.js#L99">.toNoop</a>

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-node
- `nodes` **{Array}**: Optionally pass a new `nodes` value, to replace the existing `node.nodes` array.

Example

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

## <a href="index.js#L128">.visit</a>

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-node
- `fn` **{Function}**
- `returns` **{Object}**: returns the node after recursively visiting all child nodes.

Example

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

## <a href="index.js#L155">.mapVisit</a>

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-node
- `options` **{Object}**
- `fn` **{Function}**
- `returns` **{Object}**: returns the node

Example

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

## <a href="index.js#L194">.addOpen</a>

Unshift an `*.open` node onto `node.nodes`.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `Node` **{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 = 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', val: '' };
// 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;
}
});

## <a href="index.js#L244">.addClose</a>

Push a `*.close` node onto `node.nodes`.

Params

- `node` **{Object}**: Instance of snapdragon-node
- `Node` **{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 = 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', val: '' };
// no need to return a node, since the parent
// was already added to the AST
return;
}
});

## <a href="index.js#L274">.wrapNodes</a>

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

Params

- `node` **{Object}**: Instance of snapdragon-node
- `Node` **{Function}**: (required) Node constructor function from snapdragon-node.
- `filter` **{Function}**: Optionaly specify a filter function to exclude the node.
- `returns` **{Object}**: Returns the node

## <a href="index.js#L299">.pushNode</a>

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

Params

- `parent` **{Object}**
- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{Object}**: Returns the child node

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'

## <a href="index.js#L325">.unshiftNode</a>

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

Params

- `parent` **{Object}**
- `node` **{Object}**: Instance of snapdragon-node
- `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) // 'bar'
console.log(node.parent.type) // 'foo'

## <a href="index.js#L354">.popNode</a>

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-node
- `returns` **{Number|Undefined}**: Returns the length of `node.nodes` or undefined.

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

## <a href="index.js#L382">.shiftNode</a>

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-node
- `returns` **{Number|Undefined}**: Returns the length of `node.nodes` or undefined.

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

## <a href="index.js#L409">.removeNode</a>

Remove the specified `node` from `parent.nodes`.

Params

- `parent` **{Object}**
- `node` **{Object}**: Instance of snapdragon-node
- `returns` **{Object|undefined}**: Returns the removed node, if successful, or undefined if it does not exist on `parent.nodes`.

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

## <a href="index.js#L443">.isType</a>

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-node
- `type` **{String}**
- `returns` **{Boolean}**

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

## <a href="index.js#L486">.hasType</a>

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

## <a href="index.js#L519">.firstOfType</a>

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);
//=> 'abc'

## <a href="index.js#L556">.findNode</a>

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.findNode(node.nodes, 'text');
console.log(nodeOne.val);
//=> 'abc'
var nodeTwo = utils.findNode(node.nodes, 1);
console.log(nodeTwo.val);
//=> 'xyz'

## <a href="index.js#L584">.isOpen</a>

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

Params

- `node` **{Object}**: Instance of snapdragon-node
- `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)); // false
console.log(utils.isOpen(open)); // true
console.log(utils.isOpen(close)); // false

## <a href="index.js#L607">.isClose</a>

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

Params

- `node` **{Object}**: Instance of snapdragon-node
- `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)); // false
console.log(utils.isClose(open)); // false
console.log(utils.isClose(close)); // true

## <a href="index.js#L633">.hasOpen</a>

Returns true if `node.nodes` has an `.open` node

Params

- `node` **{Object}**: Instance of snapdragon-node
- `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)); // false
brace.pushNode(open);
console.log(utils.hasOpen(brace)); // true

## <a href="index.js#L663">.hasClose</a>

Returns true if `node.nodes` has a `.close` node

Params

- `node` **{Object}**: Instance of snapdragon-node
- `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)); // false
brace.pushNode(close);
console.log(utils.hasClose(brace)); // true

## <a href="index.js#L697">.hasOpenAndClose</a>

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

Params

- `node` **{Object}**: Instance of snapdragon-node
- `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)); // 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

## <a href="index.js#L719">.addType</a>

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-node
- `returns` **{Array}**: Returns the `state.inside` stack for the given type.

Example

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

## <a href="index.js#L759">.removeType</a>

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-node
- `returns` **{Array}**: Returns the `state.inside` stack for the given type.

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: [] }

## <a href="index.js#L788">.isEmpty</a>

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

Params

- `node` **{Object}**: Instance of snapdragon-node
- `fn` **{Function}**
- `returns` **{Boolean}**

Example

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

## <a href="index.js#L833">.isInsideType</a>

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 = 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

## <a href="index.js#L867">.isInside</a>

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-node
- `type` **{String}**: The `node.type` to check for.
- `returns` **{Boolean}**

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

## <a href="index.js#L915">.last</a>

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

Params

- `array` **{Array}**
- `n` **{Number}**
- `returns` **{undefined}**

## <a href="index.js#L935">.arrayify</a>

Cast the given `val` to an array.

Params

- `val` **{any}**
- `returns` **{Array}**

Example

console.log(utils.arraify(''));
//=> []
console.log(utils.arraify('foo'));
//=> ['foo']
console.log(utils.arraify(['foo']));
//=> ['foo']

## <a href="index.js#L948">.stringify</a>

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

- `val` **{any}**
- `returns` **{Array}**

## <a href="index.js#L961">.trim</a>

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

Params

- `str` **{String}**
- `returns` **{String}**

# Release history

Changelog entries are classified using the following labels from keep-a-changelog:

- `added`: for new features
- `changed`: for changes in existing functionality
- `deprecated`: for once-stable features removed in upcoming releases
- `removed`: for deprecated features removed in this release
- `fixed`: for any bug fixes

Custom labels used in this changelog:

- `dependencies`: bumps dependencies
- `housekeeping`: code re-organization, minor edits, or other changes that don't fit in one of the other categories.

## [3.0.0] - 2017-05-01

Changed

- `.emit` was renamed to .append
- `.addNode` was renamed to .pushNode
- `.getNode` was renamed to .findNode
- `.isEmptyNodes` was renamed to .isEmpty: also now works with `node.nodes` and/or `node.val`

Added

- .identity
- .removeNode
- .shiftNode
- .popNode

## [0.1.0]

First release.

# 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#dev verb-generate-readme && 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

- github/jonschlinkert
- twitter/jonschlinkert

## License

Copyright © 2017, Jon Schlinkert. Released under the [MIT License](LICENSE).

This file was generated by verb-generate-readme, v0.6.0, on May 01, 2017.