1 # snapdragon-util [![NPM version](https://img.shields.io/npm/v/snapdragon-util.svg?style=flat)](https://www.npmjs.com/package/snapdragon-util) [![NPM monthly downloads](https://img.shields.io/npm/dm/snapdragon-util.svg?style=flat)](https://npmjs.org/package/snapdragon-util) [![NPM total downloads](https://img.shields.io/npm/dt/snapdragon-util.svg?style=flat)](https://npmjs.org/package/snapdragon-util) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/snapdragon-util.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/snapdragon-util)
3 > Utilities for the snapdragon parser/compiler.
6 <summary><strong>Table of Contents</strong></summary>
11 - [Release history](#release-history)
12 * [[3.0.0] - 2017-05-01](#300---2017-05-01)
20 Install with [npm](https://www.npmjs.com/):
23 $ npm install --save snapdragon-util
26 Install with [yarn](https://yarnpkg.com):
29 $ yarn add snapdragon-util
35 var util = require('snapdragon-util');
40 ### [.isNode](index.js#L21)
42 Returns true if the given value is a node.
46 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
47 * `returns` **{Boolean}**
52 var Node = require('snapdragon-node');
53 var node = new Node({type: 'foo'});
54 console.log(utils.isNode(node)); //=> true
55 console.log(utils.isNode({})); //=> false
58 ### [.noop](index.js#L37)
60 Emit an empty string for the given `node`.
64 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
65 * `returns` **{undefined}**
70 // do nothing for beginning-of-string
71 snapdragon.compiler.set('bos', utils.noop);
74 ### [.identity](index.js#L53)
76 Appdend `node.val` to `compiler.output`, exactly as it was created by the parser.
80 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
81 * `returns` **{undefined}**
86 snapdragon.compiler.set('text', utils.identity);
89 ### [.append](index.js#L76)
91 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`.
95 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
96 * `returns` **{Function}**: Returns a compiler middleware function.
102 .set('i', function(node) {
105 .set('i.open', utils.append('<i>'))
106 .set('i.close', utils.append('</i>'))
109 ### [.toNoop](index.js#L99)
111 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.
115 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
116 * `nodes` **{Array}**: Optionally pass a new `nodes` value, to replace the existing `node.nodes` array.
122 // convert `node.nodes` to the given value instead of deleting it
123 utils.toNoop(node, []);
126 ### [.visit](index.js#L128)
128 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.
132 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
133 * `fn` **{Function}**
134 * `returns` **{Object}**: returns the node after recursively visiting all child nodes.
139 snapdragon.compiler.set('i', function(node) {
140 utils.visit(node, function(childNode) {
141 // do stuff with "childNode"
147 ### [.mapVisit](index.js#L155)
149 Map [visit](#visit) the given `fn` over `node.nodes`. This is called by [visit](#visit), use this method if you do not want `fn` to be called on the first node.
153 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
154 * `options` **{Object}**
155 * `fn` **{Function}**
156 * `returns` **{Object}**: returns the node
161 snapdragon.compiler.set('i', function(node) {
162 utils.mapVisit(node, function(childNode) {
163 // do stuff with "childNode"
169 ### [.addOpen](index.js#L194)
171 Unshift an `*.open` node onto `node.nodes`.
175 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
176 * `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node).
177 * `filter` **{Function}**: Optionaly specify a filter function to exclude the node.
178 * `returns` **{Object}**: Returns the created opening node.
183 var Node = require('snapdragon-node');
184 snapdragon.parser.set('brace', function(node) {
185 var match = this.match(/^{/);
187 var parent = new Node({type: 'brace'});
188 utils.addOpen(parent, Node);
189 console.log(parent.nodes[0]):
190 // { type: 'brace.open', val: '' };
192 // push the parent "brace" node onto the stack
195 // return the parent node, so it's also added to the AST
201 ### [.addClose](index.js#L244)
203 Push a `*.close` node onto `node.nodes`.
207 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
208 * `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node).
209 * `filter` **{Function}**: Optionaly specify a filter function to exclude the node.
210 * `returns` **{Object}**: Returns the created closing node.
215 var Node = require('snapdragon-node');
216 snapdragon.parser.set('brace', function(node) {
217 var match = this.match(/^}/);
219 var parent = this.parent();
220 if (parent.type !== 'brace') {
221 throw new Error('missing opening: ' + '}');
224 utils.addClose(parent, Node);
225 console.log(parent.nodes[parent.nodes.length - 1]):
226 // { type: 'brace.close', val: '' };
228 // no need to return a node, since the parent
229 // was already added to the AST
235 ### [.wrapNodes](index.js#L274)
237 Wraps the given `node` with `*.open` and `*.close` nodes.
241 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
242 * `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node).
243 * `filter` **{Function}**: Optionaly specify a filter function to exclude the node.
244 * `returns` **{Object}**: Returns the node
246 ### [.pushNode](index.js#L299)
248 Push the given `node` onto `parent.nodes`, and set `parent` as `node.parent.
252 * `parent` **{Object}**
253 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
254 * `returns` **{Object}**: Returns the child node
259 var parent = new Node({type: 'foo'});
260 var node = new Node({type: 'bar'});
261 utils.pushNode(parent, node);
262 console.log(parent.nodes[0].type) // 'bar'
263 console.log(node.parent.type) // 'foo'
266 ### [.unshiftNode](index.js#L325)
268 Unshift `node` onto `parent.nodes`, and set `parent` as `node.parent.
272 * `parent` **{Object}**
273 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
274 * `returns` **{undefined}**
279 var parent = new Node({type: 'foo'});
280 var node = new Node({type: 'bar'});
281 utils.unshiftNode(parent, node);
282 console.log(parent.nodes[0].type) // 'bar'
283 console.log(node.parent.type) // 'foo'
286 ### [.popNode](index.js#L354)
288 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`.
292 * `parent` **{Object}**
293 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
294 * `returns` **{Number|Undefined}**: Returns the length of `node.nodes` or undefined.
299 var parent = new Node({type: 'foo'});
300 utils.pushNode(parent, new Node({type: 'foo'}));
301 utils.pushNode(parent, new Node({type: 'bar'}));
302 utils.pushNode(parent, new Node({type: 'baz'}));
303 console.log(parent.nodes.length); //=> 3
304 utils.popNode(parent);
305 console.log(parent.nodes.length); //=> 2
308 ### [.shiftNode](index.js#L382)
310 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`.
314 * `parent` **{Object}**
315 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
316 * `returns` **{Number|Undefined}**: Returns the length of `node.nodes` or undefined.
321 var parent = new Node({type: 'foo'});
322 utils.pushNode(parent, new Node({type: 'foo'}));
323 utils.pushNode(parent, new Node({type: 'bar'}));
324 utils.pushNode(parent, new Node({type: 'baz'}));
325 console.log(parent.nodes.length); //=> 3
326 utils.shiftNode(parent);
327 console.log(parent.nodes.length); //=> 2
330 ### [.removeNode](index.js#L409)
332 Remove the specified `node` from `parent.nodes`.
336 * `parent` **{Object}**
337 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
338 * `returns` **{Object|undefined}**: Returns the removed node, if successful, or undefined if it does not exist on `parent.nodes`.
343 var parent = new Node({type: 'abc'});
344 var foo = new Node({type: 'foo'});
345 utils.pushNode(parent, foo);
346 utils.pushNode(parent, new Node({type: 'bar'}));
347 utils.pushNode(parent, new Node({type: 'baz'}));
348 console.log(parent.nodes.length); //=> 3
349 utils.removeNode(parent, foo);
350 console.log(parent.nodes.length); //=> 2
353 ### [.isType](index.js#L443)
355 Returns true if `node.type` matches the given `type`. Throws a `TypeError` if `node` is not an instance of `Node`.
359 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
360 * `type` **{String}**
361 * `returns` **{Boolean}**
366 var Node = require('snapdragon-node');
367 var node = new Node({type: 'foo'});
368 console.log(utils.isType(node, 'foo')); // false
369 console.log(utils.isType(node, 'bar')); // true
372 ### [.hasType](index.js#L486)
374 Returns true if the given `node` has the given `type` in `node.nodes`. Throws a `TypeError` if `node` is not an instance of `Node`.
378 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
379 * `type` **{String}**
380 * `returns` **{Boolean}**
385 var Node = require('snapdragon-node');
386 var node = new Node({
389 new Node({type: 'bar'}),
390 new Node({type: 'baz'})
393 console.log(utils.hasType(node, 'xyz')); // false
394 console.log(utils.hasType(node, 'baz')); // true
397 ### [.firstOfType](index.js#L519)
399 Returns the first node from `node.nodes` of the given `type`
403 * `nodes` **{Array}**
404 * `type` **{String}**
405 * `returns` **{Object|undefined}**: Returns the first matching node or undefined.
410 var node = new Node({
413 new Node({type: 'text', val: 'abc'}),
414 new Node({type: 'text', val: 'xyz'})
418 var textNode = utils.firstOfType(node.nodes, 'text');
419 console.log(textNode.val);
423 ### [.findNode](index.js#L556)
425 Returns the node at the specified index, or the first node of the given `type` from `node.nodes`.
429 * `nodes` **{Array}**
430 * `type` **{String|Number}**: Node type or index.
431 * `returns` **{Object}**: Returns a node or undefined.
436 var node = new Node({
439 new Node({type: 'text', val: 'abc'}),
440 new Node({type: 'text', val: 'xyz'})
444 var nodeOne = utils.findNode(node.nodes, 'text');
445 console.log(nodeOne.val);
448 var nodeTwo = utils.findNode(node.nodes, 1);
449 console.log(nodeTwo.val);
453 ### [.isOpen](index.js#L584)
455 Returns true if the given node is an "*.open" node.
459 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
460 * `returns` **{Boolean}**
465 var Node = require('snapdragon-node');
466 var brace = new Node({type: 'brace'});
467 var open = new Node({type: 'brace.open'});
468 var close = new Node({type: 'brace.close'});
470 console.log(utils.isOpen(brace)); // false
471 console.log(utils.isOpen(open)); // true
472 console.log(utils.isOpen(close)); // false
475 ### [.isClose](index.js#L607)
477 Returns true if the given node is a "*.close" node.
481 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
482 * `returns` **{Boolean}**
487 var Node = require('snapdragon-node');
488 var brace = new Node({type: 'brace'});
489 var open = new Node({type: 'brace.open'});
490 var close = new Node({type: 'brace.close'});
492 console.log(utils.isClose(brace)); // false
493 console.log(utils.isClose(open)); // false
494 console.log(utils.isClose(close)); // true
497 ### [.hasOpen](index.js#L633)
499 Returns true if `node.nodes` **has** an `.open` node
503 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
504 * `returns` **{Boolean}**
509 var Node = require('snapdragon-node');
510 var brace = new Node({
515 var open = new Node({type: 'brace.open'});
516 console.log(utils.hasOpen(brace)); // false
518 brace.pushNode(open);
519 console.log(utils.hasOpen(brace)); // true
522 ### [.hasClose](index.js#L663)
524 Returns true if `node.nodes` **has** a `.close` node
528 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
529 * `returns` **{Boolean}**
534 var Node = require('snapdragon-node');
535 var brace = new Node({
540 var close = new Node({type: 'brace.close'});
541 console.log(utils.hasClose(brace)); // false
543 brace.pushNode(close);
544 console.log(utils.hasClose(brace)); // true
547 ### [.hasOpenAndClose](index.js#L697)
549 Returns true if `node.nodes` has both `.open` and `.close` nodes
553 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
554 * `returns` **{Boolean}**
559 var Node = require('snapdragon-node');
560 var brace = new Node({
565 var open = new Node({type: 'brace.open'});
566 var close = new Node({type: 'brace.close'});
567 console.log(utils.hasOpen(brace)); // false
568 console.log(utils.hasClose(brace)); // false
570 brace.pushNode(open);
571 brace.pushNode(close);
572 console.log(utils.hasOpen(brace)); // true
573 console.log(utils.hasClose(brace)); // true
576 ### [.addType](index.js#L719)
578 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`.
582 * `state` **{Object}**: The `compiler.state` object or custom state object.
583 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
584 * `returns` **{Array}**: Returns the `state.inside` stack for the given type.
589 var state = { inside: {}};
590 var node = new Node({type: 'brace'});
591 utils.addType(state, node);
592 console.log(state.inside);
593 //=> { brace: [{type: 'brace'}] }
596 ### [.removeType](index.js#L759)
598 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`.
602 * `state` **{Object}**: The `compiler.state` object or custom state object.
603 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
604 * `returns` **{Array}**: Returns the `state.inside` stack for the given type.
609 var state = { inside: {}};
610 var node = new Node({type: 'brace'});
611 utils.addType(state, node);
612 console.log(state.inside);
613 //=> { brace: [{type: 'brace'}] }
614 utils.removeType(state, node);
618 ### [.isEmpty](index.js#L788)
620 Returns true if `node.val` is an empty string, or `node.nodes` does not contain any non-empty text nodes.
624 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
625 * `fn` **{Function}**
626 * `returns` **{Boolean}**
631 var node = new Node({type: 'text'});
632 utils.isEmpty(node); //=> true
634 utils.isEmpty(node); //=> false
637 ### [.isInsideType](index.js#L833)
639 Returns true if the `state.inside` stack for the given type exists and has one or more nodes on it.
643 * `state` **{Object}**
644 * `type` **{String}**
645 * `returns` **{Boolean}**
650 var state = { inside: {}};
651 var node = new Node({type: 'brace'});
652 console.log(utils.isInsideType(state, 'brace')); //=> false
653 utils.addType(state, node);
654 console.log(utils.isInsideType(state, 'brace')); //=> true
655 utils.removeType(state, node);
656 console.log(utils.isInsideType(state, 'brace')); //=> false
659 ### [.isInside](index.js#L867)
661 Returns true if `node` is either a child or grand-child of the given `type`, or `state.inside[type]` is a non-empty array.
665 * `state` **{Object}**: Either the `compiler.state` object, if it exists, or a user-supplied state object.
666 * `node` **{Object}**: Instance of [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node)
667 * `type` **{String}**: The `node.type` to check for.
668 * `returns` **{Boolean}**
673 var state = { inside: {}};
674 var node = new Node({type: 'brace'});
675 var open = new Node({type: 'brace.open'});
676 console.log(utils.isInside(state, open, 'brace')); //=> false
677 utils.pushNode(node, open);
678 console.log(utils.isInside(state, open, 'brace')); //=> true
681 ### [.last](index.js#L915)
683 Get the last `n` element from the given `array`. Used for getting
684 a node from `node.nodes.`
688 * `array` **{Array}**
690 * `returns` **{undefined}**
692 ### [.arrayify](index.js#L935)
694 Cast the given `val` to an array.
699 * `returns` **{Array}**
704 console.log(utils.arraify(''));
706 console.log(utils.arraify('foo'));
708 console.log(utils.arraify(['foo']));
712 ### [.stringify](index.js#L948)
714 Convert the given `val` to a string by joining with `,`. Useful
715 for creating a cheerio/CSS/DOM-style selector from a list of strings.
720 * `returns` **{Array}**
722 ### [.trim](index.js#L961)
724 Ensure that the given value is a string and call `.trim()` on it,
725 or return an empty string.
730 * `returns` **{String}**
734 Changelog entries are classified using the following labels from [keep-a-changelog](https://github.com/olivierlacan/keep-a-changelog):
736 * `added`: for new features
737 * `changed`: for changes in existing functionality
738 * `deprecated`: for once-stable features removed in upcoming releases
739 * `removed`: for deprecated features removed in this release
740 * `fixed`: for any bug fixes
742 Custom labels used in this changelog:
744 * `dependencies`: bumps dependencies
745 * `housekeeping`: code re-organization, minor edits, or other changes that don't fit in one of the other categories.
747 ### [3.0.0] - 2017-05-01
751 * `.emit` was renamed to [.append](#append)
752 * `.addNode` was renamed to [.pushNode](#pushNode)
753 * `.getNode` was renamed to [.findNode](#findNode)
754 * `.isEmptyNodes` was renamed to [.isEmpty](#isEmpty): also now works with `node.nodes` and/or `node.val`
758 * [.identity](#identity)
759 * [.removeNode](#removeNode)
760 * [.shiftNode](#shiftNode)
761 * [.popNode](#popNode)
771 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
773 Please read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.
777 _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
779 To generate the readme, run the following command:
782 $ npm install -g verbose/verb#dev verb-generate-readme && verb
787 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:
790 $ npm install && npm test
797 * [github/jonschlinkert](https://github.com/jonschlinkert)
798 * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
802 Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
803 Released under the [MIT License](LICENSE).
807 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 01, 2017._