{"name":"neotraverse","version":"0.6.12","keywords":["traverse","walk","recursive","map","forEach","deep","clone"],"author":{"name":"Puru Vijay, James Halliday"},"license":"MIT","_id":"neotraverse@0.6.12","homepage":"https://github.com/PuruVJ/neotraverse","bugs":{"url":"https://github.com/PuruVJ/neotraverse/issues"},"dist":{"shasum":"eca4bca88a735e94150285524f93fd3834363026","tarball":"http://registry-npm.ywwl.com/neotraverse/download/neotraverse-0.6.12.tgz","fileCount":14,"integrity":"sha512-2+SB0CsPOjEs6f6PZ7ysDKANjn/jLPfJq65QWMQbEDPQWj/S/BzPkePhCDQRU5mmVquIl7FUt88DaCKGjfbZ3g==","signatures":[{"sig":"MEUCIHzmQQPJSX4uDTH/a16v/14alILz3P7myoKsIgw4Bf4aAiEAhuK0ou7gfSPcV29f3703Ywtu+iAsv23ya1RPPA0DFdQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":128443,"size":22252,"noattachment":false,"key":"/neotraverse/-/neotraverse-0.6.12.tgz"},"main":"dist/legacy/legacy.cjs","type":"module","types":"dist/index.d.ts","engines":{"node":">= 18"},"exports":{".":{"types":"./dist/index.d.ts","import":{"default":"./dist/index.js","production":"./dist/min/index.js","development":"./dist/index.js"},"default":"./dist/min/index.js"},"./legacy":{"import":{"types":"./dist/legacy/legacy.d.ts","default":"./dist/legacy/legacy.mjs"},"default":"./dist/legacy/legacy.cjs","require":{"types":"./dist/legacy/legacy.d.cts","default":"./dist/legacy/legacy.cjs"}},"./modern":{"types":"./dist/modern/modern.d.ts","import":{"default":"./dist/modern/modern.js","production":"./dist/modern/min/modern.js","development":"./dist/modern/modern.js"},"default":"./dist/modern/modern.js"},"./package.json":"./package.json"},"gitHead":"105c1add121063030ddbb618f0944122b2120cec","scripts":{"test":"vitest run","compile":"tsup && cp dist/legacy/legacy.cjs legacy.js"},"_npmUser":{"name":"puruvj","email":"awesomepuruvj@gmail.com"},"repository":{"url":"git+https://github.com/PuruVJ/neotraverse.git","type":"git"},"_npmVersion":"10.8.1","description":"traverse and transform objects by visiting every node on a recursive walk","directories":{},"_nodeVersion":"22.3.0","_hasShrinkwrap":false,"devDependencies":{"tsup":"^8.1.0","terser":"^5.31.2","vitest":"^2.0.1","@swc/core":"^1.6.13","typescript":"^5.5.3","@types/node":"^20.14.10"},"_npmOperationalInternal":{"tmp":"tmp/neotraverse_0.6.12_1721455027562_0.256773364931004","host":"s3://npm-registry-packages"},"readme":"# neotraverse\n\nTraverse 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:\n\n- 🤌 1.38KB min+brotli\n- 🚥 Zero dependencies\n- 🎹 TypeScript. Throw away the `@types/traverse` package\n- ❎ No polyfills\n- 🛸 ESM-first\n- 📜 Legacy mode supporting ES5\n\n# Principles\n\nRules this package aims to follow for an indefinite period of time:\n\n- No dependencies.\n- No polyfills.\n- ESM-first.\n- Pushing to be modern\n- Always provide a legacy mode\n- Always follow `traverse` API. There already are many packages that do this. `neotraverse` intends to be a drop-in replacement for `traverse` and provide the same API with 0 dependencies and enhanced Developer Experience.\n- All deviating changes happen in `neotraverse/modern` build.\n\n# Modern build\n\n`neotraverse/modern` provides a new class `new Traverse()`, and all methods and state is provided as first argument `ctx` (`this.update -> ctx.update`, `this.isLeaf -> ctx.isLeaf`, etc.)\n\nBefore:\n\n```js\nimport traverse from 'neotraverse';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\nAfter:\n\n```js\nimport { Traverse } from 'neotraverse/modern';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\nnew Traverse(obj).forEach((ctx, x) => {\n  if (x < 0) ctx.update(x + 128);\n});\n```\n\n# Which build to use?\n\n`neotraverse` provides 3 builds:\n\n- default: Backwards compatible with `traverse` and provides the same API, but ESM only and compiled to ES2022 with Node 18+\n- modern: Modern build with ESM only and compiled to ES2022 with Node 18+. Provides a new class `new Traverse()`, and all methods and state is provided as first argument `ctx` (`this.update -> ctx.update`, `this.isLeaf -> ctx.isLeaf`, etc.)\n- legacy: Legacy build with ES5 and CJS, compatible with `traverse` and provides the same API.\n\nHere's a matrix of the different builds:\n\n| Build   | ESM       | CJS | Browser | Node | Polyfills | Size              |\n| ------- | --------- | --- | ------- | ---- | --------- | ----------------- |\n| default | ✅ ES2022 |     | ✅      | ✅   | ❌        | 1.54KB min+brotli |\n| modern  | ✅ ES2022 |     | ✅      | ✅   | ❌        | 1.38KB min+brotli |\n| legacy  | ✅ ES5    | ✅  | ✅      | ✅   | ❌        | 2.73KB min+brotli |\n\nIf you are:\n\n## starting from scratch\n\n```ts\nimport { Traverse } from 'neotraverse/modern';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\nnew Traverse(obj).forEach((ctx, x) => {\n  if (x < 0) ctx.update(x + 128); // `this` is same as `ctx` when using regular function\n});\n```\n\n## migrating from `traverse`\n\n### and you don't care about old browsers or Node versions:\n\nUse default build for no breaking changes, and a modern build for better developer experience.\n\n```ts\nimport traverse from 'neotraverse';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\n### and you care about old browsers or Node versions:\n\nUse legacy build for compatibility with old browsers and Node versions.\n\n```js\nconst traverse = require('neotraverse/legacy');\n```\n\nESM:\n\n```js\nimport traverse from 'neotraverse/legacy';\n```\n\n# examples\n\n## transform negative numbers in-place\n\nnegative.js\n\n```js\nimport { Traverse } from 'neotraverse/modern';\nconst obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];\n\nnew Traverse(obj).forEach(function (ctx, x) {\n  if (x < 0) ctx.update(x + 128);\n});\n\nconsole.dir(obj);\n```\n\nor in legacy mode:\n\n```js\nimport traverse from 'neotraverse';\n// OR import traverse from 'neotraverse/legacy';\n\nconst obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n\n// This is identical to the above\ntraverse.forEach(obj, function (x) {\n  if (x < 0) this.update(x + 128);\n});\n\nconsole.dir(obj);\n```\n\nOutput:\n\n    [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]\n\n## collect leaf nodes\n\nleaves.js\n\n```js\nimport { Traverse } from 'neotraverse/modern';\n\nconst obj = {\n  a: [1, 2, 3],\n  b: 4,\n  c: [5, 6],\n  d: { e: [7, 8], f: 9 }\n};\n\nconst leaves = new Traverse(obj).reduce((ctx, acc, x) => {\n  if (ctx.isLeaf) acc.push(x);\n  return acc;\n}, []);\n\nconsole.dir(leaves);\n```\n\nor in legacy mode:\n\n```js\nimport traverse from 'neotraverse';\n// OR import traverse from 'neotraverse/legacy';\n\nconst obj = {\n  a: [1, 2, 3],\n  b: 4,\n  c: [5, 6],\n  d: { e: [7, 8], f: 9 }\n};\n\nconst leaves = traverse(obj).reduce(function (acc, x) {\n  if (this.isLeaf) acc.push(x);\n  return acc;\n}, []);\n\n// Equivalent to the above\nconst leavesLegacy = traverse.reduce(\n  obj,\n  function (acc, x) {\n    if (this.isLeaf) acc.push(x);\n    return acc;\n  },\n  []\n);\n\nconsole.dir(leaves);\nconsole.dir(leavesLegacy);\n```\n\nOutput:\n\n    [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]\n\n## scrub circular references\n\nscrub.js:\n\n```js\nimport { Traverse } from 'neotraverse/modern';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\nobj.c.push(obj);\n\nconst scrubbed = new Traverse(obj).map(function (ctx, x) {\n  if (ctx.circular) ctx.remove();\n});\n\nconsole.dir(scrubbed);\n```\n\nor in legacy mode:\n\n```js\nimport traverse from 'neotraverse';\n// OR import traverse from 'neotraverse/legacy';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\nobj.c.push(obj);\n\nconst scrubbed = traverse(obj).map(function (x) {\n  if (this.circular) this.remove();\n});\n\n// Equivalent to the above\nconst scrubbedLegacy = traverse.map(obj, function (x) {\n  if (this.circular) this.remove();\n});\n\nconsole.dir(scrubbed);\nconsole.dir(scrubbedLegacy);\n```\n\noutput:\n\n    { a: 1, b: 2, c: [ 3, 4 ] }\n\n## commonjs\n\nneotraverse/legacy is compatible with commonjs and provides the same API as `traverse`, acting as a drop-in replacement:\n\n```js\nconst traverse = require('neotraverse/legacy');\n```\n\n## esm\n\n```js\nimport { Traverse } from 'neotraverse/modern';\n```\n\n```js\nimport traverse from 'neotraverse';\n```\n\n# Differences from `traverse`\n\n- ESM-first\n- ES2022, Node 18+\n- Types included by default. No need to install `@types/traverse`\n- Works as-is in all major browsers and Deno\n- No polyfills\n- `new Traverse()` class instead of regular old `traverse()`\n- Legacy mode supporting `ES5` and `CJS`\n\nThere is a legacy mode that provides the same API as `traverse`, acting as a drop-in replacement:\n\n```js\nimport traverse from 'neotraverse';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\nIf 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:\n\n```js\nimport traverse from 'neotraverse/legacy';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\n# Migrating from `traverse`\n\n### Step 1: Install `neotraverse`\n\n```sh\nnpm install neotraverse\nnpm uninstall traverse @types/traverse # Remove the old dependencies\n```\n\n### Step 2: Replace `traverse` with `neotraverse`\n\n```diff\n-import traverse from 'traverse';\n+import traverse from 'neotraverse';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\n-traverse(obj).forEach(function (x) {\n+traverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\nOptionally, there's also a legacy mode that provides the same API as `traverse`, acting as a drop-in replacement:\n\n```js\nimport traverse from 'neotraverse/legacy';\n\nconst obj = { a: 1, b: 2, c: [3, 4] };\n\ntraverse(obj).forEach(function (x) {\n  if (x < 0) this.update(x + 128);\n});\n```\n\n### Step 3(Optional): Bundle time aliasing\n\nIf you use Vite, you can aliss `traverse` to `neotravers/legacy` in your `vite.config.js`:\n\n```js\nimport { defineConfig } from 'vite';\n\nexport default defineConfig({\n  resolve: {\n    alias: {\n      traverse: 'neotraverse' // or 'neotraverse/legacy'\n    }\n  }\n});\n```\n\n# methods\n\nEach method that takes an `fn` uses the context documented below in the context section.\n\n## .map(fn)\n\nExecute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `ctx.update(value)`(modern) or `this.update(value)`(legacy).\n\n## .forEach(fn)\n\nExecute `fn` for each node in the object but unlike `.map()`, when `ctx.update()`(modern) or `this.update()`(legacy) is called it updates the object in-place.\n\n## .reduce(fn, acc)\n\nFor each node in the object, perform a [left-fold](<http://en.wikipedia.org/wiki/Fold_(higher-order_function)>) with the return value of `fn(acc, node)`.\n\nIf `acc` isn't specified, `acc` is set to the root object for the first step and the root element is skipped.\n\n## .paths()\n\nReturn an `Array` of every possible non-cyclic path in the object. Paths are `Array`s of string keys.\n\n## .nodes()\n\nReturn an `Array` of every node in the object.\n\n## .clone()\n\nCreate a deep clone of the object.\n\n## .get(path)\n\nGet the element at the array `path`.\n\n## .set(path, value)\n\nSet the element at the array `path` to `value`.\n\n## .has(path)\n\nReturn whether the element at the array `path` exists.\n\n# context\n\nEach method that takes a callback has a context (its `ctx` object, or `this` object in legacy mode) with these attributes:\n\n## this.node\n\nThe present node on the recursive walk\n\n## this.path\n\nAn array of string keys from the root to the present node\n\n## this.parent\n\nThe context of the node's parent. This is `undefined` for the root node.\n\n## this.key\n\nThe name of the key of the present node in its parent. This is `undefined` for the root node.\n\n## this.isRoot, this.notRoot\n\nWhether the present node is the root node\n\n## this.isLeaf, this.notLeaf\n\nWhether or not the present node is a leaf node (has no children)\n\n## this.level\n\nDepth of the node within the traversal\n\n## this.circular\n\nIf the node equals one of its parents, the `circular` attribute is set to the context of that parent and the traversal progresses no deeper.\n\n## this.update(value, stopHere=false)\n\nSet a new value for the present node.\n\nAll the elements in `value` will be recursively traversed unless `stopHere` is true.\n\n## this.remove(stopHere=false)\n\nRemove the current element from the output. If the node is in an Array it will be spliced off. Otherwise it will be deleted from its parent.\n\n## this.delete(stopHere=false)\n\nDelete the current element from its parent in the output. Calls `delete` even on Arrays.\n\n## this.before(fn)\n\nCall this function before any of the children are traversed.\n\nYou can assign into `ctx.keys`(modern) or `this.keys`(legacy) here to traverse in a custom order.\n\n## this.after(fn)\n\nCall this function after any of the children are traversed.\n\n## this.pre(fn)\n\nCall this function before each of the children are traversed.\n\n## this.post(fn)\n\nCall this function after each of the children are traversed.\n\n# license\n\nMIT\n","publish_time":1721455027732,"maintainers":[{"name":"puruvj","email":"awesomepuruvj@gmail.com"}],"_cnpm_publish_time":1721455027732,"dist-tags":{"latest":"1.0.2","next":"0.6.9-next.0"}}