unist-util-visit-parents
Advanced tools
Comparing version 1.0.0 to 1.1.0
102
index.js
@@ -1,84 +0,54 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2016 Titus Wormer | ||
* @license MIT | ||
* @module unist:util:visit-parents | ||
* @fileoverview Recursively walk over unist nodes, | ||
* with ancestral information. | ||
*/ | ||
'use strict'; | ||
/* eslint-env commonjs */ | ||
/* Expose. */ | ||
module.exports = visitParents; | ||
/** | ||
* 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. | ||
*/ | ||
/* Visit. */ | ||
function visitParents(tree, type, visitor) { | ||
var stack = []; | ||
var stack = []; | ||
if (typeof type === 'function') { | ||
visitor = type; | ||
type = null; | ||
} | ||
if (typeof type === 'function') { | ||
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 length = children.length; | ||
var index = -1; | ||
var child; | ||
one(tree); | ||
stack.push(parent); | ||
return; | ||
while (++index < length) { | ||
child = children[index]; | ||
/* Visit a single node. */ | ||
function one(node) { | ||
var result; | ||
if (child && one(child) === false) { | ||
return false; | ||
} | ||
} | ||
if (!type || node.type === type) { | ||
result = visitor(node, stack.concat()); | ||
} | ||
stack.pop(); | ||
return true; | ||
if (node.children && result !== false) { | ||
return all(node.children, node); | ||
} | ||
/** | ||
* Visit a single node. | ||
* | ||
* @param {Node} node - Node to visit. | ||
* @return {boolean?} - Result of invoking `visitor`. | ||
*/ | ||
function one(node) { | ||
var result; | ||
return result; | ||
} | ||
if (!type || node.type === type) { | ||
result = visitor(node, stack.concat()); | ||
} | ||
/* Visit children in `parent`. */ | ||
function all(children, parent) { | ||
var length = children.length; | ||
var index = -1; | ||
var child; | ||
if (node.children && result !== false) { | ||
return all(node.children, node); | ||
} | ||
stack.push(parent); | ||
return result; | ||
while (++index < length) { | ||
child = children[index]; | ||
if (child && one(child) === false) { | ||
return false; | ||
} | ||
} | ||
one(tree); | ||
stack.pop(); | ||
return true; | ||
} | ||
} | ||
/* | ||
* Expose. | ||
*/ | ||
module.exports = visitParents; |
{ | ||
"name": "unist-util-visit-parents", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Recursively walk over unist nodes, with ancestral information", | ||
@@ -8,11 +8,2 @@ "license": "MIT", | ||
"unist", | ||
"remark", | ||
"markdown", | ||
"retext", | ||
"natural", | ||
"language", | ||
"node", | ||
"visit", | ||
"parent", | ||
"ancestor", | ||
"walk", | ||
@@ -22,6 +13,3 @@ "util", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/unist-util-visit-parents.git" | ||
}, | ||
"repository": "https://github.com/wooorm/unist-util-visit-parents", | ||
"bugs": "https://github.com/wooorm/unist-util-visit-parents/issues", | ||
@@ -35,27 +23,38 @@ "author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"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 unistUtilVisitParents > unist-util-visit-parents.js", | ||
"build-mangle": "esmangle unist-util-visit-parents.js > unist-util-visit-parents.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-parents.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"presets": "wooorm" | ||
} | ||
} |
# unist-util-visit-parents [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page] | ||
[Unist][] node visitor, with ancestral information. Useful when | ||
working with [**remark**][remark] or [**retext**][retext]. | ||
[Unist][] node visitor, with ancestral information. | ||
Similar to [`unist-util-visit`][visit], which you should probably be | ||
using. | ||
## Installation | ||
@@ -17,20 +13,19 @@ | ||
**unist-util-visit-parents** is also available as an AMD, CommonJS, and | ||
globals module, [uncompressed and compressed][releases]. | ||
## Usage | ||
Dependencies: | ||
```javascript | ||
var remark = require('remark'); | ||
var visitParents = require('unist-util-visit-parents'); | ||
var visit = require('unist-util-visit-parents'); | ||
remark().use(function () { | ||
return function (ast) { | ||
visitParents(ast, 'strong', function (node, parents) { | ||
console.log(parents); | ||
}); | ||
}; | ||
}).process('# Some **strongness** in a heading'); | ||
remark().use(plugin).process('Some _emphasis_, **importance**, and `code`.'); | ||
function plugin() { | ||
return transformer; | ||
function transformer(tree) { | ||
visit(tree, 'strong', visitor); | ||
} | ||
function visitor(node, parents) { | ||
console.log(parents); | ||
} | ||
} | ||
``` | ||
@@ -43,5 +38,11 @@ | ||
children: [ [Object] ] }, | ||
{ type: 'heading', | ||
depth: 1, | ||
children: [ [Object], [Object], [Object] ] } } ] | ||
{ type: 'paragraph', | ||
children: | ||
[ [Object], | ||
[Object], | ||
[Object], | ||
[Object], | ||
[Object], | ||
[Object], | ||
[Object] ] } ] | ||
``` | ||
@@ -51,9 +52,11 @@ | ||
### `visitParents(node[, type], visitor)` | ||
### `visit(node[, type], visitor)` | ||
Visit nodes, with ancestral information. Optionally by node type. | ||
###### Parameters | ||
* `node` ([`Node`][node]) — Node to search; | ||
* `type` (`string`, optional) — Node type; | ||
* `visitor` (`Function`) — See [`visitor`][visitor]. | ||
* `visitor` ([Function][visitor]) — Visitor invoked when a node is found. | ||
@@ -64,3 +67,3 @@ #### `stop? = visitor(node, parents)` | ||
**Parameters**: | ||
###### Parameters | ||
@@ -70,4 +73,6 @@ * `node` ([`Node`][node]) — Found node; | ||
**Returns**: `boolean?` - When `false`, visiting is immediately stopped. | ||
###### Returns | ||
`boolean?` - When `false`, visiting is immediately stopped. | ||
## License | ||
@@ -89,4 +94,2 @@ | ||
[releases]: https://github.com/wooorm/unist-util-visit-parents/releases | ||
[license]: LICENSE | ||
@@ -98,10 +101,4 @@ | ||
[retext]: https://github.com/wooorm/retext | ||
[remark]: https://github.com/wooorm/remark | ||
[node]: https://github.com/wooorm/unist#node | ||
[visitor]: #stop--visitornode-index-parent-parents | ||
[visit]: https://github.com/wooorm/unist-util-visit | ||
[visitor]: #stop--visitornode-parents |
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
5664
4
39
98
1