Socket
Socket
Sign inDemoInstall

snapdragon-util

Package Overview
Dependencies
2
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    snapdragon-util

Utilities for the snapdragon parser/compiler.


Version published
Weekly downloads
10M
decreased by-10.43%
Maintainers
1
Install size
51.4 kB
Created
Weekly downloads
 

Readme

Source

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

Utilities for the snapdragon parser/compiler.

Table of Contents - [Install](#install) - [Usage](#usage) - [API](#api) - [About](#about)

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

  • node {Object}

Example

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

.noop

Emit an empty string for the given node.

Params

  • node {Object}

Example

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

.emit

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

  • node {Object}

Example

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

.toNoop

Converts an AST node into an empty text node and deletes node.nodes.

Params

  • node {Object}

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

  • node {Object}
  • options {Object}: Set options.recurse to true call recursively call mapVisit on node.nodes.
  • fn {Function}
  • returns {Object}: returns the node

Example

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

.mapVisit

Map visit with the given fn over an array of AST nodes.

Params

  • node {Object}
  • options {Object}
  • fn {Function}
  • returns {Object}: returns the node

Example

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

.wrapNodes

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

Params

  • node {Object}: (required)
  • Node {Function}: (required) Node constructor function from snapdragon-node.
  • filter {Function}: Optionaly specify a filter function to exclude the node.
  • returns {undefined}

.addOpen

Unshift an *.open node onto node.nodes.

Params

  • node {Object}
  • Node {Function}: (required) Node constructor function from snapdragon-node.
  • filter {Function}: Optionaly specify a filter function to exclude the node.
  • returns {undefined}

.addClose

Push a *.close node onto node.nodes.

Params

  • node {Object}
  • Node {Function}: (required) Node constructor function from snapdragon-node.
  • filter {Function}: Optionaly specify a filter function to exclude the node.
  • returns {undefined}

.pushNode

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

Params

  • parent {Object}
  • node {Object}
  • returns {undefined}

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

  • parent {Object}
  • node {Object}
  • 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'

.isType

Returns true if node is a valid Node and node.type matches the given type.

Params

  • node {Object}
  • 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

.hasType

Returns true if the given node has the given type in node.nodes.

Params

  • node {Object}
  • 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

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

.getNode

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.getNode(node.nodes, 'text');
console.log(nodeOne.val);
//=> 'abc'

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

.isOpen

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

Params

  • node {Object}
  • 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

.isClose

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

Params

  • node {Object}
  • 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

.hasOpen

Returns true if node.nodes has an .open node

Params

  • node {Object}
  • 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.addNode(open);
console.log(utils.hasOpen(brace)); // true

.hasClose

Returns true if node.nodes has a .close node

Params

  • node {Object}
  • 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.addNode(close);
console.log(utils.hasClose(brace)); // true

.hasOpenAndClose

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

Params

  • node {Object}
  • 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.addNode(open);
brace.addNode(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 "stack" for the given node.type.

Params

  • state {Object}: The compiler.state object or custom state object.
  • node {Object}
  • returns {undefined}

.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 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']

.stringify

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}

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

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.4.3, on March 04, 2017.

Keywords

FAQs

Last updated on 04 Mar 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc