snapdragon-util
Advanced tools
Comparing version 2.0.0 to 2.1.0
330
index.js
@@ -7,10 +7,25 @@ 'use strict'; | ||
/** | ||
* Emit an empty string to effectively "skip" the string for the given `node`, | ||
* but still emit the position and node type. | ||
* Returns true if the given value is a node. | ||
* | ||
* ```js | ||
* var node = snapdragon.parser.node({type: 'foo'}); | ||
* console.log(utils.isNode(node)); //=> true | ||
* console.log(utils.isNode({})); //=> false | ||
* ``` | ||
* @param {Object} `node` | ||
* @api public | ||
*/ | ||
exports.isNode = function(node) { | ||
return typeOf(node) === 'object' && node.isNode; | ||
}; | ||
/** | ||
* Emit an empty string for the given `node`. | ||
* | ||
* ```js | ||
* // do nothing for beginning-of-string | ||
* snapdragon.compiler.set('bos', utils.noop); | ||
* ``` | ||
* @param {Object} node | ||
* @param {Object} `node` | ||
* @api public | ||
@@ -24,4 +39,4 @@ */ | ||
/** | ||
* Emit an empty string to effectively "skip" the string for the given `node`, | ||
* but still emit the position and node type. | ||
* 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. | ||
* | ||
@@ -36,3 +51,3 @@ * ```js | ||
* ``` | ||
* @param {Object} node | ||
* @param {Object} `node` | ||
* @api public | ||
@@ -48,7 +63,8 @@ */ | ||
/** | ||
* Converts an AST node into an empty `text` node and delete `node.nodes`. | ||
* Converts an AST node into an empty `text` node and deletes `node.nodes`. | ||
* | ||
* ```js | ||
* utils.toNoop(node); | ||
* utils.toNoop(node, true); // convert `node.nodes` to an empty array | ||
* // convert `node.nodes` to the given value instead of deleting it | ||
* utils.toNoop(node, []); | ||
* ``` | ||
@@ -59,5 +75,5 @@ * @param {Object} `node` | ||
exports.toNoop = function(node, keepNodes) { | ||
if (keepNodes === true) { | ||
node.nodes = []; | ||
exports.toNoop = function(node, nodes) { | ||
if (nodes) { | ||
node.nodes = nodes; | ||
} else { | ||
@@ -76,11 +92,11 @@ delete node.nodes; | ||
* ```js | ||
* snapdragon.compiler | ||
* .set('i', function(node) { | ||
* exports.visit(node, function(node2) { | ||
* // do stuff with "node2" | ||
* return node2; | ||
* }); | ||
* }) | ||
* snapdragon.compiler.set('i', function(node) { | ||
* utils.visit(node, function(node2) { | ||
* // do stuff with "node2" | ||
* return node2; | ||
* }); | ||
* }); | ||
* ``` | ||
* @param {Object} `node` | ||
* @param {Object} `options` Set `options.recurse` to true call recursively call `mapVisit` on `node.nodes`. | ||
* @param {Function} `fn` | ||
@@ -116,14 +132,17 @@ * @return {Object} returns the node | ||
* ```js | ||
* snapdragon.compiler | ||
* .set('i', function(node) { | ||
* exports.mapVisit(node, function(node2) { | ||
* // do stuff with "node2" | ||
* return node2; | ||
* }); | ||
* }) | ||
* snapdragon.compiler.set('i', function(node) { | ||
* utils.mapVisit(node, function(node2) { | ||
* // do stuff with "node2" | ||
* return node2; | ||
* }); | ||
* }); | ||
* ``` | ||
* @param {Object} `node` | ||
* @param {Object} `options` | ||
* @param {Function} `fn` | ||
* @return {Object} returns the node | ||
* @api public | ||
*/ | ||
exports.mapVisit = function(parent, options, fn) { | ||
exports.mapVisit = function(node, options, fn) { | ||
if (typeof options === 'function') { | ||
@@ -134,15 +153,10 @@ fn = options; | ||
var nodes = parent.nodes || parent.children; | ||
if (!Array.isArray(nodes)) { | ||
throw new TypeError('.mapVisit: exected parent.nodes to be an array'); | ||
if (!Array.isArray(node.nodes)) { | ||
throw new TypeError('.mapVisit: exected node.nodes to be an array'); | ||
} | ||
for (var i = 0; i < nodes.length; i++) { | ||
var node = nodes[i]; | ||
define(node, 'parent', parent); | ||
nodes[i] = exports.visit(node, options, fn) || node; | ||
// reset properties on `nodes[i]` in case the returned | ||
// node was user-defined and the properties were lost | ||
define(nodes[i], 'parent', parent); | ||
for (var i = 0; i < node.nodes.length; i++) { | ||
var childNode = node.nodes[i]; | ||
define(childNode, 'parent', node); | ||
exports.visit(childNode, options, fn) || childNode; | ||
} | ||
@@ -153,6 +167,6 @@ return node; | ||
/** | ||
* Wrap the given `node` with `*.open` and `*.close` tags. | ||
* Wraps the given `node` with `*.open` and `*.close` nodes. | ||
* | ||
* @param {Object} `node` (required) | ||
* @param {Function} `Node` (required) | ||
* @param {Function} `Node` (required) Node constructor function from [snapdragon-node][]. | ||
* @param {Function} `filter` Optionaly specify a filter function to exclude the node. | ||
@@ -172,2 +186,3 @@ * @return {undefined} | ||
* @param {Object} `node` | ||
* @param {Function} `Node` (required) Node constructor function from [snapdragon-node][]. | ||
* @param {Function} `filter` Optionaly specify a filter function to exclude the node. | ||
@@ -192,2 +207,3 @@ * @return {undefined} | ||
* @param {Object} `node` | ||
* @param {Function} `Node` (required) Node constructor function from [snapdragon-node][]. | ||
* @param {Function} `filter` Optionaly specify a filter function to exclude the node. | ||
@@ -209,3 +225,3 @@ * @return {undefined} | ||
/** | ||
* Push `node` onto `parent.nodes`. | ||
* Push the given `node` onto `parent.nodes`, and set `parent` as `node.parent. | ||
* | ||
@@ -217,5 +233,6 @@ * ```js | ||
* console.log(parent.nodes[0].type) // 'bar' | ||
* console.log(node.parent.type) // 'foo' | ||
* ``` | ||
* @param {Object} `parent` | ||
* @param {Object} `node` | ||
* @param {Function} `filter` Optionaly specify a filter function to exclude the node. | ||
* @return {undefined} | ||
@@ -232,3 +249,3 @@ * @api public | ||
/** | ||
* Unshift `node` onto `parent.nodes`. | ||
* Unshift `node` onto `parent.nodes`, and set `parent` as `node.parent. | ||
* | ||
@@ -240,3 +257,5 @@ * ```js | ||
* console.log(parent.nodes[0].type) // 'bar' | ||
* console.log(node.parent.type) // 'foo' | ||
* ``` | ||
* @param {Object} `parent` | ||
* @param {Object} `node` | ||
@@ -254,17 +273,17 @@ * @return {undefined} | ||
/** | ||
* Get the last `n` element from the given `array`. Used for getting | ||
* a node from `node.nodes.` | ||
* Returns true if `node` is a valid [Node][snapdragon-node] and | ||
* `node.type` matches the given `type`. | ||
* | ||
* @param {Array} `array` | ||
* @return {*} | ||
* ```js | ||
* 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 | ||
* ``` | ||
* @param {Object} `node` | ||
* @param {String} `type` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
exports.last = function(arr, n) { | ||
return arr[arr.length - (n || 1)]; | ||
}; | ||
/** | ||
* Return true if node is the given `type` | ||
*/ | ||
exports.isType = function(node, type) { | ||
@@ -294,3 +313,20 @@ if (typeOf(node) !== 'object' || !node.type) { | ||
/** | ||
* Return true if `nodes` has the given `type` | ||
* Returns true if the given `node` has the given `type` in `node.nodes`. | ||
* | ||
* ```js | ||
* 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 | ||
* ``` | ||
* @param {Object} `node` | ||
* @param {String} `type` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -309,15 +345,20 @@ | ||
/** | ||
* Return the first node from `nodes` of the given `type` | ||
* Returns the first node from `node.nodes` of the given `type` | ||
* | ||
* ```js | ||
* snapdragon.set('div', function(node) { | ||
* var textNode = exports.firstOfType(node.nodes, 'text'); | ||
* if (textNode) { | ||
* // do stuff with text node | ||
* } | ||
* 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' | ||
* ``` | ||
* @param {Array} `nodes` | ||
* @param {String} `type` | ||
* @return {Object} Returns a node, if found | ||
* @return {Object|undefined} Returns the first matching node or undefined. | ||
* @api public | ||
@@ -340,5 +381,27 @@ */ | ||
/** | ||
* Get the a node from `node.nodes`. If `type` is a number, the | ||
* node at that index is returned, otherwise [.firstOfType()](#firstOfType) | ||
* is called to get the first node that matches the given `type`. | ||
* Returns the node at the specified index, or the first node of the | ||
* given `type` from `node.nodes`. | ||
* | ||
* ```js | ||
* 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); | ||
* //=> 'abc' | ||
* | ||
* var nodeTwo = utils.getNode(node.nodes, 1); | ||
* console.log(nodeTwo.val); | ||
* //=> 'xyz' | ||
* ``` | ||
* | ||
* @param {Array} `nodes` | ||
* @param {String|Number} `type` Node type or index. | ||
* @return {Object} Returns a node or undefined. | ||
* @api public | ||
*/ | ||
@@ -355,3 +418,17 @@ | ||
/** | ||
* Return true if node is for an "open" tag | ||
* Returns true if the given node is an "*.open" node. | ||
* | ||
* ```js | ||
* 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 | ||
* ``` | ||
* @param {Object} `node` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -367,3 +444,17 @@ | ||
/** | ||
* Return true if node is for a "close" tag | ||
* Returns true if the given node is a "*.close" node. | ||
* | ||
* ```js | ||
* 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 | ||
* ``` | ||
* @param {Object} `node` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -379,3 +470,20 @@ | ||
/** | ||
* Return true if `node.nodes` has an `.open` node | ||
* Returns true if `node.nodes` **has** an `.open` node | ||
* | ||
* ```js | ||
* 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.addNode(open); | ||
* console.log(utils.hasOpen(brace)); // true | ||
* ``` | ||
* @param {Object} `node` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -391,3 +499,20 @@ | ||
/** | ||
* Return true if `node.nodes` has a `.close` node | ||
* Returns true if `node.nodes` **has** a `.close` node | ||
* | ||
* ```js | ||
* 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.addNode(close); | ||
* console.log(utils.hasClose(brace)); // true | ||
* ``` | ||
* @param {Object} `node` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -403,3 +528,24 @@ | ||
/** | ||
* Return true if `node.nodes` has both `.open` and `.close` nodes | ||
* Returns true if `node.nodes` has both `.open` and `.close` nodes | ||
* | ||
* ```js | ||
* 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.addNode(open); | ||
* brace.addNode(close); | ||
* console.log(utils.hasOpen(brace)); // true | ||
* console.log(utils.hasClose(brace)); // true | ||
* ``` | ||
* @param {Object} `node` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
@@ -412,3 +558,9 @@ | ||
/** | ||
* Add the given `node` to the `state.inside` stack for that type. | ||
* 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`. | ||
* | ||
* @param {Object} `state` The `compiler.state` object or custom state object. | ||
* @param {Object} `node` | ||
* @return {undefined} | ||
* @api public | ||
*/ | ||
@@ -423,6 +575,10 @@ | ||
} | ||
var type = node.type.replace(/\.open$/, ''); | ||
state.inside = state.inside || {}; | ||
if (!state.inside.hasOwnProperty(type)) { | ||
state.inside[type] = []; | ||
} | ||
state.inside[type].push(node); | ||
@@ -451,3 +607,3 @@ }; | ||
/** | ||
* Return true if `node.nodes` contains only open and close nodes, | ||
* Returns true if `node.nodes` contains only open and close nodes, | ||
* or open, close and an empty text node. | ||
@@ -475,3 +631,3 @@ */ | ||
/** | ||
* Return true if inside the current `type` | ||
* Returns true if inside the current `type` | ||
*/ | ||
@@ -483,2 +639,3 @@ | ||
} | ||
state.inside = state.inside || {}; | ||
return state.inside.hasOwnProperty(type) && state.inside[type].length > 0; | ||
@@ -488,3 +645,3 @@ }; | ||
/** | ||
* Return true if `node` is inside the current `type` | ||
* Returns true if `node` is inside the current `type` | ||
*/ | ||
@@ -531,3 +688,26 @@ | ||
/** | ||
* Get the last `n` element from the given `array`. Used for getting | ||
* a node from `node.nodes.` | ||
* | ||
* @param {Array} `array` | ||
* @param {Number} `n` | ||
* @return {undefined} | ||
* @api public | ||
*/ | ||
exports.last = function(arr, n) { | ||
return arr[arr.length - (n || 1)]; | ||
}; | ||
/** | ||
* Cast the given `val` to an array. | ||
* | ||
* ```js | ||
* console.log(utils.arraify('')); | ||
* //=> [] | ||
* console.log(utils.arraify('foo')); | ||
* //=> ['foo'] | ||
* console.log(utils.arraify(['foo'])); | ||
* //=> ['foo'] | ||
* ``` | ||
* @param {any} `val` | ||
@@ -544,3 +724,3 @@ * @return {Array} | ||
* Convert the given `val` to a string by joining with `,`. Useful | ||
* for creating a selector from a list of strings. | ||
* for creating a cheerio/CSS/DOM-style selector from a list of strings. | ||
* | ||
@@ -547,0 +727,0 @@ * @param {any} `val` |
{ | ||
"name": "snapdragon-util", | ||
"description": "Utilities for the snapdragon parser/compiler.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/jonschlinkert/snapdragon-util", | ||
@@ -13,4 +13,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js", | ||
"utils.js" | ||
"index.js" | ||
], | ||
@@ -24,2 +23,13 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"define-property": "^0.2.5", | ||
"kind-of": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.11", | ||
"mocha": "^3.2.0", | ||
"snapdragon": "^0.10.1", | ||
"snapdragon-capture-set": "^1.0.1", | ||
"snapdragon-node": "^1.0.6" | ||
}, | ||
"keywords": [ | ||
@@ -40,13 +50,2 @@ "capture", | ||
], | ||
"dependencies": { | ||
"define-property": "^0.2.5", | ||
"kind-of": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.11", | ||
"mocha": "^3.2.0", | ||
"snapdragon": "^0.10.1", | ||
"snapdragon-capture-set": "^1.0.1", | ||
"snapdragon-node": "^1.0.6" | ||
}, | ||
"verb": { | ||
@@ -53,0 +52,0 @@ "toc": "collapsible", |
359
README.md
@@ -29,9 +29,9 @@ # 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) | ||
### [.noop](index.js#L18) | ||
### [.isNode](index.js#L18) | ||
Emit an empty string to effectively "skip" the string for the given `node`, but still emit the position and node type. | ||
Returns true if the given value is a node. | ||
**Params** | ||
* **{Object}**: node | ||
* `node` **{Object}** | ||
@@ -41,2 +41,18 @@ **Example** | ||
```js | ||
var node = snapdragon.parser.node({type: 'foo'}); | ||
console.log(utils.isNode(node)); //=> true | ||
console.log(utils.isNode({})); //=> false | ||
``` | ||
### [.noop](index.js#L33) | ||
Emit an empty string for the given `node`. | ||
**Params** | ||
* `node` **{Object}** | ||
**Example** | ||
```js | ||
// do nothing for beginning-of-string | ||
@@ -46,9 +62,9 @@ snapdragon.compiler.set('bos', utils.noop); | ||
### [.emit](index.js#L38) | ||
### [.emit](index.js#L53) | ||
Emit an empty string to effectively "skip" the string for the given `node`, but still emit the position and node type. | ||
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** | ||
* **{Object}**: node | ||
* `node` **{Object}** | ||
@@ -66,5 +82,5 @@ **Example** | ||
### [.toNoop](index.js#L55) | ||
### [.toNoop](index.js#L71) | ||
Converts an AST node into an empty `text` node and delete `node.nodes`. | ||
Converts an AST node into an empty `text` node and deletes `node.nodes`. | ||
@@ -79,6 +95,7 @@ **Params** | ||
utils.toNoop(node); | ||
utils.toNoop(node, true); // convert `node.nodes` to an empty array | ||
// convert `node.nodes` to the given value instead of deleting it | ||
utils.toNoop(node, []); | ||
``` | ||
### [.visit](index.js#L85) | ||
### [.visit](index.js#L101) | ||
@@ -90,2 +107,3 @@ 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. | ||
* `node` **{Object}** | ||
* `options` **{Object}**: Set `options.recurse` to true call recursively call `mapVisit` on `node.nodes`. | ||
* `fn` **{Function}** | ||
@@ -97,30 +115,35 @@ * `returns` **{Object}**: returns the node | ||
```js | ||
snapdragon.compiler | ||
.set('i', function(node) { | ||
exports.visit(node, function(node2) { | ||
// do stuff with "node2" | ||
return node2; | ||
}); | ||
}) | ||
snapdragon.compiler.set('i', function(node) { | ||
utils.visit(node, function(node2) { | ||
// do stuff with "node2" | ||
return node2; | ||
}); | ||
}); | ||
``` | ||
### [.mapVisit](index.js#L121) | ||
### [.mapVisit](index.js#L140) | ||
Map [visit](#visit) with the given `fn` over an array of AST `nodes`. | ||
**Params** | ||
* `node` **{Object}** | ||
* `options` **{Object}** | ||
* `fn` **{Function}** | ||
* `returns` **{Object}**: returns the node | ||
**Example** | ||
```js | ||
snapdragon.compiler | ||
.set('i', function(node) { | ||
exports.mapVisit(node, function(node2) { | ||
// do stuff with "node2" | ||
return node2; | ||
}); | ||
}) | ||
snapdragon.compiler.set('i', function(node) { | ||
utils.mapVisit(node, function(node2) { | ||
// do stuff with "node2" | ||
return node2; | ||
}); | ||
}); | ||
``` | ||
### [.wrapNodes](index.js#L154) | ||
### [.wrapNodes](index.js#L168) | ||
Wrap the given `node` with `*.open` and `*.close` tags. | ||
Wraps the given `node` with `*.open` and `*.close` nodes. | ||
@@ -130,7 +153,7 @@ **Params** | ||
* `node` **{Object}**: (required) | ||
* `Node` **{Function}**: (required) | ||
* `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node). | ||
* `filter` **{Function}**: Optionaly specify a filter function to exclude the node. | ||
* `returns` **{undefined}** | ||
### [.addOpen](index.js#L168) | ||
### [.addOpen](index.js#L183) | ||
@@ -142,6 +165,7 @@ Unshift an `*.open` node onto `node.nodes`. | ||
* `node` **{Object}** | ||
* `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node). | ||
* `filter` **{Function}**: Optionaly specify a filter function to exclude the node. | ||
* `returns` **{undefined}** | ||
### [.addClose](index.js#L187) | ||
### [.addClose](index.js#L203) | ||
@@ -153,13 +177,14 @@ Push a `*.close` node onto `node.nodes`. | ||
* `node` **{Object}** | ||
* `Node` **{Function}**: (required) Node constructor function from [snapdragon-node](https://github.com/jonschlinkert/snapdragon-node). | ||
* `filter` **{Function}**: Optionaly specify a filter function to exclude the node. | ||
* `returns` **{undefined}** | ||
### [.pushNode](index.js#L212) | ||
### [.pushNode](index.js#L229) | ||
Push `node` onto `parent.nodes`. | ||
Push the given `node` onto `parent.nodes`, and set `parent` as `node.parent. | ||
**Params** | ||
* `parent` **{Object}** | ||
* `node` **{Object}** | ||
* `filter` **{Function}**: Optionaly specify a filter function to exclude the node. | ||
* `returns` **{undefined}** | ||
@@ -174,10 +199,12 @@ | ||
console.log(parent.nodes[0].type) // 'bar' | ||
console.log(node.parent.type) // 'foo' | ||
``` | ||
### [.unshiftNode](index.js#L232) | ||
### [.unshiftNode](index.js#L251) | ||
Unshift `node` onto `parent.nodes`. | ||
Unshift `node` onto `parent.nodes`, and set `parent` as `node.parent. | ||
**Params** | ||
* `parent` **{Object}** | ||
* `node` **{Object}** | ||
@@ -193,13 +220,58 @@ * `returns` **{undefined}** | ||
console.log(parent.nodes[0].type) // 'bar' | ||
console.log(node.parent.type) // 'foo' | ||
``` | ||
### [.firstOfType](index.js#L308) | ||
### [.isType](index.js#L273) | ||
Return the first node from `nodes` of the given `type` | ||
Returns true if `node` is a valid [Node](https://github.com/jonschlinkert/snapdragon-node) and `node.type` matches the given `type`. | ||
**Params** | ||
* `node` **{Object}** | ||
* `type` **{String}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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](index.js#L317) | ||
Returns true if the given `node` has the given `type` in `node.nodes`. | ||
**Params** | ||
* `node` **{Object}** | ||
* `type` **{String}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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](index.js#L349) | ||
Returns the first node from `node.nodes` of the given `type` | ||
**Params** | ||
* `nodes` **{Array}** | ||
* `type` **{String}** | ||
* `returns` **{Object}**: Returns a node, if found | ||
* `returns` **{Object|undefined}**: Returns the first matching node or undefined. | ||
@@ -209,12 +281,192 @@ **Example** | ||
```js | ||
snapdragon.set('div', function(node) { | ||
var textNode = exports.firstOfType(node.nodes, 'text'); | ||
if (textNode) { | ||
// do stuff with text node | ||
} | ||
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' | ||
``` | ||
### [.arrayify](index.js#L507) | ||
### [.getNode](index.js#L390) | ||
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** | ||
```js | ||
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); | ||
//=> 'abc' | ||
var nodeTwo = utils.getNode(node.nodes, 1); | ||
console.log(nodeTwo.val); | ||
//=> 'xyz' | ||
``` | ||
### [.isOpen](index.js#L416) | ||
Returns true if the given node is an "*.open" node. | ||
**Params** | ||
* `node` **{Object}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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](index.js#L441) | ||
Returns true if the given node is a "*.close" node. | ||
**Params** | ||
* `node` **{Object}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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 | ||
``` | ||
### [.hasOpen](index.js#L469) | ||
Returns true if `node.nodes` **has** an `.open` node | ||
**Params** | ||
* `node` **{Object}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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.addNode(open); | ||
console.log(utils.hasOpen(brace)); // true | ||
``` | ||
### [.hasClose](index.js#L497) | ||
Returns true if `node.nodes` **has** a `.close` node | ||
**Params** | ||
* `node` **{Object}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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.addNode(close); | ||
console.log(utils.hasClose(brace)); // true | ||
``` | ||
### [.hasOpenAndClose](index.js#L529) | ||
Returns true if `node.nodes` has both `.open` and `.close` nodes | ||
**Params** | ||
* `node` **{Object}** | ||
* `returns` **{Boolean}** | ||
**Example** | ||
```js | ||
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.addNode(open); | ||
brace.addNode(close); | ||
console.log(utils.hasOpen(brace)); // true | ||
console.log(utils.hasClose(brace)); // true | ||
``` | ||
### [.addType](index.js#L543) | ||
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}** | ||
### [.last](index.js#L667) | ||
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](index.js#L687) | ||
Cast the given `val` to an array. | ||
@@ -227,6 +479,17 @@ | ||
### [.stringify](index.js#L520) | ||
**Example** | ||
```js | ||
console.log(utils.arraify('')); | ||
//=> [] | ||
console.log(utils.arraify('foo')); | ||
//=> ['foo'] | ||
console.log(utils.arraify(['foo'])); | ||
//=> ['foo'] | ||
``` | ||
### [.stringify](index.js#L700) | ||
Convert the given `val` to a string by joining with `,`. Useful | ||
for creating a selector from a list of strings. | ||
for creating a cheerio/CSS/DOM-style selector from a list of strings. | ||
@@ -274,6 +537,6 @@ **Params** | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
MIT | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 15, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 26, 2017._ |
31005
632
527