Socket
Socket
Sign inDemoInstall

unist-util-map

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unist-util-map - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

37

lib/unist-util-map.js
// LICENSE : MIT
"use strict";
var ObjectAssign = require("object-assign");
module.exports = function map(ast, mapFn) {
return (function preorder(node, index, parent) {
const newNode = ObjectAssign({}, mapFn(node, index, parent));
if (node.children) {
newNode.children = node.children.map(function (child, index) {
return preorder(child, index, node);
});
}
return newNode;
}(ast, null, null));
};
'use strict'
var assign = require('object-assign')
module.exports = map
function map(ast, iteratee) {
return preorder(ast, null, null)
function preorder(node, index, parent) {
var children = node.children
var newNode = assign({}, iteratee(node, index, parent))
if (children) {
newNode.children = children.map(bound)
}
return newNode
function bound(child, index) {
return preorder(child, index, node)
}
}
}
{
"name": "unist-util-map",
"repository": {
"type": "git",
"url": "git+https://github.com/syntax-tree/unist-util-map.git"
},
"author": "azu",
"email": "azuciao@gmail.com",
"homepage": "https://github.com/syntax-tree/unist-util-map",
"version": "1.0.4",
"description": "Create a new Unist tree with all nodes that mapped by the provided function",
"license": "MIT",
"keywords": [
"unist",
"textlint",
"util"
],
"repository": "syntax-tree/unist-util-map",
"bugs": "https://github.com/syntax-tree/unist-util-map/issues",
"author": "azu <azuciao@gmail.com>",
"contributors": [
"azu <azuciao@gmail.com>",
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)"
],
"files": [
"lib/"
],
"bugs": {
"url": "https://github.com/syntax-tree/unist-util-map/issues"
},
"version": "1.0.3",
"description": "Create a new Unist tree with all nodes that mapped by the provided function",
"main": "lib/unist-util-map.js",
"directories": {
"test": "test"
"dependencies": {
"object-assign": "^4.0.1"
},
"devDependencies": {
"mocha": "^5.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"xo": "^0.20.3"
},
"scripts": {
"test": "mocha"
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"test-api": "mocha",
"test": "npm run format && npm run test-api"
},
"keywords": [
"unist",
"textlint",
"util"
],
"devDependencies": {
"mocha": "^3.2.0",
"txt-to-ast": "^1.1.0",
"unist-builder": "^1.0.1"
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"dependencies": {
"object-assign": "^4.0.1"
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}

@@ -1,12 +0,14 @@

# unist-util-map [![Build Status](https://travis-ci.org/syntax-tree/unist-util-map.svg?branch=master)](https://travis-ci.org/syntax-tree/unist-util-map)
# unist-util-map [![Build Status][build-badge]][build-page]
Create a new Unist tree with all nodes that mapped by the provided function.
Helper for creating [unist: Universal Syntax Tree](https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree").
Helper for creating [unist: Universal Syntax Tree][unist].
- retext, remark, hast, textlint
* [retext][], [remark][], [rehype][], [textlint][]
## Installation
npm install unist-util-map
```sh
npm install unist-util-map
```

@@ -20,41 +22,40 @@ ## Usage

```js
const map = require("unist-util-map");
const ObjectAssign = require("object-assign");
// input
const ast = {
"type": "root",
"children": [
{
"type": "node",
"children": [
{"type": "leaf", "value": "1"}
]
},
{"type": "leaf", "value": "2"}
]
};
// tranformed
const actual = map(ast, function (node) {
if (node.type === "leaf") {
return ObjectAssign({}, node, {
value: "CHANGED"
});
}
// no change
return node;
});
// output
const assert = require('assert')
const assign = require('object-assign')
const map = require('unist-util-map')
// Input
const tree = {
type: 'root',
children: [
{
type: 'node',
children: [{type: 'leaf', value: '1'}]
},
{type: 'leaf', value: '2'}
]
}
// Transform:
const actual = map(tree, function(node) {
if (node.type === 'leaf') {
return assign({}, node, {value: 'CHANGED'})
}
// No change
return node
})
// Expected output:
const expected = {
"type": "root",
"children": [
{
"type": "node",
"children": [
{"type": "leaf", "value": "CHANGED"}
]
},
{"type": "leaf", "value": "CHANGED"}
]
};
assert.deepEqual(actual, expected);
type: 'root',
children: [
{
type: 'node',
children: [{type: 'leaf', value: 'CHANGED'}]
},
{type: 'leaf', value: 'CHANGED'}
]
}
assert.deepEqual(actual, expected)
```

@@ -64,14 +65,42 @@

npm test
```sh
npm test
```
## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D
See [`contribute.md` in `syntax-tree/unist`][contributing] for ways to get
started.
This organisation has a [Code of Conduct][coc]. By interacting with this
repository, organisation, or community you agree to abide by its terms.
## License
MIT
[MIT][]
[build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-map.svg
[build-page]: https://travis-ci.org/syntax-tree/unist-util-map
[unist]: https://github.com/wooorm/unist "wooorm/unist: Universal Syntax Tree"
[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md
[remark]: https://github.com/remarkjs/remark
[retext]: https://github.com/retextjs/retext
[rehype]: https://github.com/rehypejs/rehype
[textlint]: https://github.com/textlint/textlint
[mit]: LICENSE
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc