unist-util-visit
Advanced tools
Comparing version 1.1.0 to 1.1.1
108
index.js
@@ -1,89 +0,55 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2015 Titus Wormer | ||
* @license MIT | ||
* @module unist:util:visit | ||
* @fileoverview Recursively walk over unist nodes. | ||
*/ | ||
'use strict'; | ||
/* eslint-env commonjs */ | ||
/* Expose. */ | ||
module.exports = visit; | ||
/** | ||
* Visit. | ||
* | ||
* @param {Node} tree - Root node | ||
* @param {string} [type] - Node type. | ||
* @param {function(node): boolean?} visitor - Invoked | ||
* with each found node. Can return `false` to stop. | ||
* @param {boolean} [reverse] - By default, `visit` will | ||
* walk forwards, when `reverse` is `true`, `visit` | ||
* walks backwards. | ||
*/ | ||
/* Visit. */ | ||
function visit(tree, type, visitor, reverse) { | ||
if (typeof type === 'function') { | ||
reverse = visitor; | ||
visitor = type; | ||
type = null; | ||
} | ||
if (typeof type === 'function') { | ||
reverse = visitor; | ||
visitor = type; | ||
type = null; | ||
} | ||
/** | ||
* Visit children in `parent`. | ||
* | ||
* @param {Array.<Node>} children - Children of `node`. | ||
* @param {Node?} parent - Parent of `node`. | ||
* @return {boolean?} - `false` if the visiting stopped. | ||
*/ | ||
function all(children, parent) { | ||
var step = reverse ? -1 : 1; | ||
var max = children.length; | ||
var min = -1; | ||
var index = (reverse ? max : min) + step; | ||
var child; | ||
one(tree); | ||
while (index > min && index < max) { | ||
child = children[index]; | ||
return; | ||
if (child && one(child, index, parent) === false) { | ||
return false; | ||
} | ||
/* Visit a single node. */ | ||
function one(node, index, parent) { | ||
var result; | ||
index += step; | ||
} | ||
index = index || (parent ? 0 : null); | ||
return true; | ||
if (!type || node.type === type) { | ||
result = visitor(node, index, parent || null); | ||
} | ||
/** | ||
* Visit a single node. | ||
* | ||
* @param {Node} node - Node to visit. | ||
* @param {number?} [index] - Position of `node` in `parent`. | ||
* @param {Node?} [parent] - Parent of `node`. | ||
* @return {boolean?} - A result of invoking `visitor`. | ||
*/ | ||
function one(node, index, parent) { | ||
var result; | ||
if (node.children && result !== false) { | ||
return all(node.children, node); | ||
} | ||
index = index || (parent ? 0 : null); | ||
return result; | ||
} | ||
if (!type || node.type === type) { | ||
result = visitor(node, index, parent || null); | ||
} | ||
/* Visit children in `parent`. */ | ||
function all(children, parent) { | ||
var step = reverse ? -1 : 1; | ||
var max = children.length; | ||
var min = -1; | ||
var index = (reverse ? max : min) + step; | ||
var child; | ||
if (node.children && result !== false) { | ||
return all(node.children, node); | ||
} | ||
while (index > min && index < max) { | ||
child = children[index]; | ||
return result; | ||
if (child && one(child, index, parent) === false) { | ||
return false; | ||
} | ||
index += step; | ||
} | ||
one(tree); | ||
return true; | ||
} | ||
} | ||
/* | ||
* Expose. | ||
*/ | ||
module.exports = visit; |
{ | ||
"name": "unist-util-visit", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Recursively walk over unist nodes", | ||
@@ -19,6 +19,3 @@ "license": "MIT", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/unist-util-visit.git" | ||
}, | ||
"repository": "https://github.com/wooorm/unist-util-visit", | ||
"bugs": "https://github.com/wooorm/unist-util-visit/issues", | ||
@@ -34,25 +31,35 @@ "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"browserify": "^13.0.0", | ||
"eslint": "^2.0.0", | ||
"esmangle": "^1.0.0", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^2.0.0", | ||
"jscs-jsdoc": "^1.0.0", | ||
"remark": "^4.0.0", | ||
"remark-comment-config": "^3.0.0", | ||
"remark-github": "^4.0.0", | ||
"remark-lint": "^3.0.0", | ||
"tape": "^4.5.1" | ||
"nyc": "^9.0.1", | ||
"remark": "^6.0.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.5.1", | ||
"xo": "^0.17.1" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . --quiet --frail", | ||
"build-md": "remark . --quiet --frail --output", | ||
"build-bundle": "browserify index.js --no-builtins -s unistUtilVisit > unist-util-visit.js", | ||
"build-mangle": "esmangle unist-util-visit.js > unist-util-visit.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint-api": "eslint .", | ||
"lint-style": "jscs --reporter inline .", | ||
"lint": "npm run lint-api && npm run lint-style", | ||
"test-api": "node test.js", | ||
"test-coverage": "istanbul cover test.js", | ||
"lint": "xo", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"ignores": [ | ||
"unist-util-visit.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"presets": "wooorm" | ||
} | ||
} |
# unist-util-visit [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page] | ||
[Unist][] node visitor. Useful with working with [**remark**][remark] | ||
or [**retext**][retext]. | ||
[Unist][] node visitor. Useful with working with [**remark**][remark], | ||
[**retext**][retext], or [**rehype**][rehype]. | ||
@@ -14,9 +14,4 @@ ## Installation | ||
**unist-util-visit** is also available as an AMD, CommonJS, and | ||
globals module, [uncompressed and compressed][releases]. | ||
## Usage | ||
Dependencies: | ||
```javascript | ||
@@ -26,9 +21,13 @@ var remark = require('remark'); | ||
remark().use(function () { | ||
return function (ast) { | ||
visit(ast, 'text', function (node) { | ||
console.log(node) | ||
}); | ||
}; | ||
}).process('Some _emphasis_, **strongness**, and `code`.'); | ||
remark().use(plugin).process('Some _emphasis_, **importance**, and `code`.'); | ||
function plugin() { | ||
return transformer; | ||
function transformer(tree) { | ||
visit(tree, 'text', visitor); | ||
} | ||
function visitor(node) { | ||
console.log(node); | ||
} | ||
} | ||
``` | ||
@@ -39,8 +38,8 @@ | ||
```js | ||
{'type': 'text', 'value': 'Some '} | ||
{'type': 'text', 'value': 'emphasis'} | ||
{'type': 'text', 'value': ', '} | ||
{'type': 'text', 'value': 'strongness'} | ||
{'type': 'text', 'value': ', and '} | ||
{'type': 'text', 'value': '.'} | ||
{type: 'text', value: 'Some '} | ||
{type: 'text', value: 'emphasis'} | ||
{type: 'text', value: ', '} | ||
{type: 'text', value: 'strongness'} | ||
{type: 'text', value: ', and '} | ||
{type: 'text', value: '.'} | ||
``` | ||
@@ -52,17 +51,15 @@ | ||
Visit nodes. Optionally by node type. Optionally in reverse. | ||
Visit nodes. Optionally by node type. Optionally in reverse. | ||
###### Parameters | ||
* `node` ([`Node`][node]) | ||
— Node to search; | ||
* `type` (`string`, optional) | ||
— Node type; | ||
* `visitor` ([Function][visitor]) | ||
— Visitor invoked when a node is found; | ||
* `reverse` (`boolean`, default: `false`) | ||
— When falsey, checking starts at the first child and continues | ||
through to later children. When truthy, this is reversed. | ||
This **does not** mean checking starts at the deepest node and | ||
@@ -75,3 +72,3 @@ continues on to the highest node. | ||
**Parameters**: | ||
###### Parameters | ||
@@ -82,4 +79,6 @@ * `node` (`Node`) — Found node; | ||
**Returns**: `boolean?` - When `false`, visiting is immediately stopped. | ||
###### Returns | ||
`boolean?` - When `false`, visiting is immediately stopped. | ||
## License | ||
@@ -101,4 +100,2 @@ | ||
[releases]: https://github.com/wooorm/unist-util-visit/releases | ||
[license]: LICENSE | ||
@@ -114,4 +111,6 @@ | ||
[rehype]: https://github.com/wooorm/rehype | ||
[node]: https://github.com/wooorm/unist#node | ||
[visitor]: #stop--visitornode-index-parent |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
6257
4
41
109
1