# tree-transformer

> Transform nodes in the tree.

Latest version **1.0.0** (published 2013-08-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install tree-transformer
pnpm add tree-transformer
yarn add tree-transformer
bun add tree-transformer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2013-08-19 |
| First published | 2013-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Glen Huang |
| Maintainers | curvedmark |
| Keywords | ast, tree, visitor, transformer, compiler |

## Links

- npm: https://www.npmjs.com/package/tree-transformer
- Repository: https://github.com/curvedmark/tree-transformer
- Issues: https://github.com/curvedmark/tree-transformer/issues
- npm.io page: https://npm.io/package/tree-transformer

## Dependencies (1)

- [tree-visitor](https://npm.io/package/tree-visitor.md) *

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2013-08-19
- 0.7.0 — 2013-08-04
- 0.5.1 — 2013-08-01
- 0.5.0 — 2013-08-01
- 0.4.0 — 2013-07-28
- 0.3.0 — 2013-07-26

## README

# Tree Transformer

Transform nodes in the tree.

Like [Tree Visitor](https://github.com/curvedmark/tree-visitor), but methods can return a value to replace the node being processed.

## API

```javascript
var Transformer = require('tree-transformer');
var nodes = [
	{ type: 'number', value: 1 },
	{ type: 'string', value: 'abc', quote: '"' }
];

function MyTransformer() {}
MyTransformer.prototype = new Transformer();

MyTransformer.prototype.visit_node = function (node) {
	 return node.value;
};

new MyTransformer().visit(nodes); // nodes now equals to [1, 'abc']
```

Replacing only happens when visit an array of nodes.

Some returned values have special meanings:

* `null` - remove the node
* `undefined` - do nothing to the node
* an array of nodes - replace the node with the array of nodes. The result array is flattened.

	```javascript
	var Transformer = require('tree-transformer');
	var nodes = [
		{ type: 'number', value: 1 }
		{ type: 'number', value: 3 }
	];

	function MyTransformer() {}
	MyTransformer.prototype = new Transformer();

	MyTransformer.prototype.visit_number = function (number) {
	   return [number.value, number.value + 1];
	};

	new MyTransformer().visit(nodes); // nodes now equals to [1, 2, 3, 4]
	```
* others - replace the node with the value

When visiting a single node, `visit(node)` returns the returning value of the corresponding method, unless it's `undefined`, in which case the original node will be returned.

When visiting an array of nodes, the result array will be returned.

---
_Source: https://npm.io/package/tree-transformer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
