Skip to content

Commit

Permalink
Legecy mode is now just compiled, og file has all the modesa
Browse files Browse the repository at this point in the history
  • Loading branch information
PuruVJ committed Jul 11, 2024
1 parent 70fc8d4 commit 521cf72
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 35 deletions.
90 changes: 86 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

Traverse and transform objects by visiting every node on a recursive walk. This is a fork and TypeScript rewrite of [traverse](https://github.com/ljharb/js-traverse) with 0 dependencies and major improvements:

- 🤌 1.45KB min+brotli
- 🤌 1.54KB min+brotli
- 🚥 Zero dependencies
- 🎹 TypeScript. Throw away the `@types/traverse` package
- ❎ No polyfills
- 🛸 ESM-first
- Legacy mode supporting ES5

# Principles

Expand Down Expand Up @@ -35,6 +36,25 @@ new Traverse(obj).forEach(function (x) {
console.dir(obj);
```

or in legacy mode:

```js
import traverse from 'neotraverse';

const obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];

traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});

// This is identical to the above
traverse.forEach(obj, function (x) {
if (x < 0) this.update(x + 128);
});

console.dir(obj);
```

Output:

[ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
Expand All @@ -61,6 +81,37 @@ const leaves = new Traverse(obj).reduce(function (acc, x) {
console.dir(leaves);
```

or in legacy mode:

```js
import traverse from 'neotraverse';

const obj = {
a: [1, 2, 3],
b: 4,
c: [5, 6],
d: { e: [7, 8], f: 9 }
};

const leaves = traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) acc.push(x);
return acc;
}, []);

// Equivalent to the above
const leavesLegacy = traverse.reduce(
obj,
function (acc, x) {
if (this.isLeaf) acc.push(x);
return acc;
},
[]
);

console.dir(leaves);
console.dir(leavesLegacy);
```

Output:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Expand All @@ -82,6 +133,27 @@ const scrubbed = new Traverse(obj).map(function (x) {
console.dir(scrubbed);
```

or in legacy mode:

```js
import traverse from 'neotraverse';

const obj = { a: 1, b: 2, c: [3, 4] };
obj.c.push(obj);

const scrubbed = traverse(obj).map(function (x) {
if (this.circular) this.remove();
});

// Equivalent to the above
const scrubbedLegacy = traverse.map(obj, function (x) {
if (this.circular) this.remove();
});

console.dir(scrubbed);
console.dir(scrubbedLegacy);
```

output:

{ a: 1, b: 2, c: [ 3, 4 ] }
Expand All @@ -94,12 +166,12 @@ output:
- Works as-is in all major browsers and Deno
- No polyfills
- `new Traverse()` class instead of regular old `traverse()`
- Legacy mode supporting ES2015 minimum
- Legacy mode supporting ES5 and CJS

There is a legacy mode that provides the same API as `traverse`, acting as a drop-in replacement:

```js
import traverse from 'neotraverse/legacy';
import traverse from 'neotraverse';

const obj = { a: 1, b: 2, c: [3, 4] };

Expand All @@ -108,7 +180,17 @@ traverse(obj).forEach(function (x) {
});
```

> NOTE: legacy mode doesn't have methods attached to the function constructor. As in, traverse.map(obj, fn) won't work. Use `new Traverse(obj).map(fn)` or `traverse(obj).map(fn)` instead.
If you want to support really old browsers or NodeJS, supporting ES5, there's `neotraverse/legacy` which is compatible with ES5 and provides the same API as `traverse`, acting as a drop-in replacement for older browsers:

```js
import traverse from 'neotraverse/legacy';

const obj = { a: 1, b: 2, c: [3, 4] };

traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});
```

# Migrating from `traverse`

Expand Down
2 changes: 1 addition & 1 deletion examples/json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import traverse from '../dist/legacy/legacy.js';
import traverse from '../dist/legacy/index.js';

var id = 54;
var callbacks = {};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"node": ">= 18"
},
"devDependencies": {
"@swc/core": "^1.6.13",
"@types/node": "^20.14.10",
"terser": "^5.31.2",
"tsup": "^8.1.0",
Expand Down
135 changes: 133 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 521cf72

Please sign in to comment.