traverse
Advanced tools
Comparing version 0.6.8 to 0.6.9
@@ -8,2 +8,15 @@ # Changelog | ||
## [v0.6.9](https://github.com/ljharb/js-traverse/compare/v0.6.8...v0.6.9) - 2024-04-08 | ||
### Commits | ||
- [New] support cloning Typed Arrays [`18c32c5`](https://github.com/ljharb/js-traverse/commit/18c32c5ba8ebc84344925198bb29b6def97471fd) | ||
- [New] [Fix] add `includeSymbols` option; partial revert of "[New] support enumerable Symbol properties" [`aab373f`](https://github.com/ljharb/js-traverse/commit/aab373f503f80f62ea958124c0cc9321f9fe0b78) | ||
- [Fix] Add "isWritable" before writing to properties [`595d64e`](https://github.com/ljharb/js-traverse/commit/595d64e307452b805c4d209a6f77916e54c031ab) | ||
- [actions] remove redundant finisher [`7539473`](https://github.com/ljharb/js-traverse/commit/7539473f969589ca19eee197d02b568b299cfebc) | ||
- [Refactor] use an internal null options object instead of an `immutable` boolean [`0f1e6f1`](https://github.com/ljharb/js-traverse/commit/0f1e6f126a3d847864d3a80fc8227a2bb1f97c78) | ||
- [Deps] update `typedarray.prototype.slice`, `which-typed-array` [`165f954`](https://github.com/ljharb/js-traverse/commit/165f954e540975b4a5db7f4b7134de2c0b48ee29) | ||
- [Deps] update `typedarray.prototype.slice` [`ed483ed`](https://github.com/ljharb/js-traverse/commit/ed483ed7aa1cc85e8d7e25d2b2cd1e0881eb6522) | ||
- [Dev Deps] update `tape` [`5ee670c`](https://github.com/ljharb/js-traverse/commit/5ee670cdc074026f087f18860d80a30c86921e46) | ||
## [v0.6.8](https://github.com/ljharb/js-traverse/compare/v0.6.7...v0.6.8) - 2023-12-20 | ||
@@ -10,0 +23,0 @@ |
101
index.js
'use strict'; | ||
var whichTypedArray = require('which-typed-array'); | ||
var taSlice = require('typedarray.prototype.slice'); | ||
var gopd = require('gopd'); | ||
// TODO: use call-bind, is-date, is-regex, is-string, is-boolean-object, is-number-object | ||
@@ -57,3 +61,11 @@ function toS(obj) { return Object.prototype.toString.call(obj); } | ||
function copy(src) { | ||
function isWritable(object, key) { | ||
if (typeof gopd !== 'function') { | ||
return true; | ||
} | ||
return !gopd(object, key).writable; | ||
} | ||
function copy(src, options) { | ||
if (typeof src === 'object' && src !== null) { | ||
@@ -72,16 +84,22 @@ var dst; | ||
dst = Object(src); | ||
} else if (Object.create && Object.getPrototypeOf) { | ||
dst = Object.create(Object.getPrototypeOf(src)); | ||
} else if (src.constructor === Object) { | ||
dst = {}; | ||
} else { | ||
var proto = (src.constructor && src.constructor.prototype) | ||
|| src.__proto__ | ||
|| {}; | ||
var T = function T() {}; // eslint-disable-line func-style, func-name-matching | ||
T.prototype = proto; | ||
dst = new T(); | ||
var ta = whichTypedArray(src); | ||
if (ta) { | ||
return taSlice(src); | ||
} else if (Object.create && Object.getPrototypeOf) { | ||
dst = Object.create(Object.getPrototypeOf(src)); | ||
} else if (src.constructor === Object) { | ||
dst = {}; | ||
} else { | ||
var proto = (src.constructor && src.constructor.prototype) | ||
|| src.__proto__ | ||
|| {}; | ||
var T = function T() {}; // eslint-disable-line func-style, func-name-matching | ||
T.prototype = proto; | ||
dst = new T(); | ||
} | ||
} | ||
forEach(ownEnumerableKeys(src), function (key) { | ||
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys; | ||
forEach(iteratorFunction(src), function (key) { | ||
dst[key] = src[key]; | ||
@@ -94,9 +112,15 @@ }); | ||
function walk(root, cb, immutable) { | ||
/** @type {TraverseOptions} */ | ||
var emptyNull = { __proto__: null }; | ||
function walk(root, cb) { | ||
var path = []; | ||
var parents = []; | ||
var alive = true; | ||
var options = arguments.length > 2 ? arguments[2] : emptyNull; | ||
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys; | ||
var immutable = !!options.immutable; | ||
return (function walker(node_) { | ||
var node = immutable ? copy(node_) : node_; | ||
var node = immutable ? copy(node_, options) : node_; | ||
var modifiers = {}; | ||
@@ -149,3 +173,3 @@ | ||
if (!state.keys || state.node_ !== state.node) { | ||
state.keys = ownEnumerableKeys(state.node); | ||
state.keys = iteratorFunction(state.node); | ||
} | ||
@@ -195,3 +219,7 @@ | ||
var child = walker(state.node[key]); | ||
if (immutable && hasOwnProperty.call(state.node, key)) { | ||
if ( | ||
immutable | ||
&& hasOwnProperty.call(state.node, key) | ||
&& !isWritable(state.node, key) | ||
) { | ||
state.node[key] = child.node; | ||
@@ -216,11 +244,25 @@ } | ||
/** @typedef {{ immutable?: boolean, includeSymbols?: boolean }} TraverseOptions */ | ||
/** | ||
* A traverse constructor | ||
* @param {object} obj - the object to traverse | ||
* @param {TraverseOptions | undefined} [options] - options for the traverse | ||
* @constructor | ||
*/ | ||
function Traverse(obj) { | ||
/** @type {TraverseOptions} */ | ||
this.options = arguments.length > 1 ? arguments[1] : emptyNull; | ||
this.value = obj; | ||
} | ||
/** @type {(ps: PropertyKey[]) => Traverse['value']} */ | ||
Traverse.prototype.get = function (ps) { | ||
var node = this.value; | ||
for (var i = 0; i < ps.length; i++) { | ||
for (var i = 0; node && i < ps.length; i++) { | ||
var key = ps[i]; | ||
if (!node || !hasOwnProperty.call(node, key)) { | ||
if ( | ||
!hasOwnProperty.call(node, key) | ||
|| (!this.options.includeSymbols && typeof key === 'symbol') | ||
) { | ||
return void undefined; | ||
@@ -233,7 +275,8 @@ } | ||
/** @type {(ps: PropertyKey[]) => boolean} */ | ||
Traverse.prototype.has = function (ps) { | ||
var node = this.value; | ||
for (var i = 0; i < ps.length; i++) { | ||
for (var i = 0; node && i < ps.length; i++) { | ||
var key = ps[i]; | ||
if (!node || !hasOwnProperty.call(node, key)) { | ||
if (!hasOwnProperty.call(node, key) || (!this.options.includeSymbols && typeof key === 'symbol')) { | ||
return false; | ||
@@ -258,7 +301,7 @@ } | ||
Traverse.prototype.map = function (cb) { | ||
return walk(this.value, cb, true); | ||
return walk(this.value, cb, { __proto__: null, immutable: true, includeSymbols: !!this.options.includeSymbols }); | ||
}; | ||
Traverse.prototype.forEach = function (cb) { | ||
this.value = walk(this.value, cb, false); | ||
this.value = walk(this.value, cb, this.options); | ||
return this.value; | ||
@@ -297,3 +340,8 @@ }; | ||
var nodes = []; | ||
var options = this.options; | ||
if (whichTypedArray(this.value)) { | ||
return taSlice(this.value); | ||
} | ||
return (function clone(src) { | ||
@@ -307,3 +355,3 @@ for (var i = 0; i < parents.length; i++) { | ||
if (typeof src === 'object' && src !== null) { | ||
var dst = copy(src); | ||
var dst = copy(src, options); | ||
@@ -313,3 +361,4 @@ parents.push(src); | ||
forEach(ownEnumerableKeys(src), function (key) { | ||
var iteratorFunction = options.includeSymbols ? ownEnumerableKeys : objectKeys; | ||
forEach(iteratorFunction(src), function (key) { | ||
dst[key] = clone(src[key]); | ||
@@ -328,4 +377,6 @@ }); | ||
/** @type {(obj: object, options?: TraverseOptions) => Traverse} */ | ||
function traverse(obj) { | ||
return new Traverse(obj); | ||
var options = arguments.length > 1 ? arguments[1] : emptyNull; | ||
return new Traverse(obj, options); | ||
} | ||
@@ -332,0 +383,0 @@ |
{ | ||
"name": "traverse", | ||
"version": "0.6.8", | ||
"version": "0.6.9", | ||
"description": "traverse and transform objects by visiting every node on a recursive walk", | ||
@@ -19,3 +19,3 @@ "main": "index.js", | ||
"safe-publish-latest": "^2.0.0", | ||
"tape": "^5.7.2" | ||
"tape": "^5.7.5" | ||
}, | ||
@@ -95,2 +95,7 @@ "scripts": { | ||
}, | ||
"dependencies": { | ||
"gopd": "^1.0.1", | ||
"typedarray.prototype.slice": "^1.0.3", | ||
"which-typed-array": "^1.1.15" | ||
}, | ||
"engines": { | ||
@@ -97,0 +102,0 @@ "node": ">= 0.4" |
@@ -26,10 +26,25 @@ 'use strict'; | ||
st.equal(traverse(obj).has([globalSymbol]), true); | ||
st.equal(traverse(obj).has([globalSymbol]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([globalSymbol]), true); | ||
st.equal(traverse(obj).has([globalSymbol, globalSymbol]), false); | ||
st.equal(traverse(obj).has([globalSymbol, localSymbol]), true); | ||
st.equal(traverse(obj).has([localSymbol]), true); | ||
st.equal(traverse(obj).has([localSymbol]), true); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([globalSymbol, globalSymbol]), false); | ||
st.equal(traverse(obj).has([globalSymbol, localSymbol]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([globalSymbol, localSymbol]), true); | ||
st.equal(traverse(obj).has([localSymbol]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([localSymbol]), true); | ||
st.equal(traverse(obj).has([Symbol('d')]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([Symbol('d')]), false); | ||
st.equal(traverse(obj).has([Symbol('e')]), false); | ||
st.equal(traverse(obj).has([Symbol.for('d')]), true); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([Symbol('e')]), false); | ||
st.equal(traverse(obj).has([Symbol.for('d')]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([Symbol.for('d')]), true); | ||
st.equal(traverse(obj).has([Symbol.for('e')]), false); | ||
st.equal(traverse(obj, { includeSymbols: true }).has([Symbol.for('e')]), false); | ||
@@ -36,0 +51,0 @@ st.end(); |
@@ -80,2 +80,15 @@ 'use strict'; | ||
test('cloneTypedArray', { skip: typeof Uint8Array !== 'function' }, function (t) { | ||
var obj = new Uint8Array([1]); | ||
var res = traverse.clone(obj); | ||
t.same(obj, res); | ||
t.ok(obj !== res); | ||
obj.set([2], 0); | ||
res.set([3], 0); | ||
t.same(obj, new Uint8Array([2])); | ||
t.same(res, new Uint8Array([3])); | ||
t.end(); | ||
}); | ||
test('reduce', function (t) { | ||
@@ -198,2 +211,3 @@ var obj = { a: 1, b: 2, c: [3, 4] }; | ||
// eslint-disable-next-line no-sparse-arrays | ||
t.ok(deepEqual(obj, { a: 1, c: [3,, 5] })); | ||
@@ -224,3 +238,4 @@ | ||
t.ok(deepEqual(res, { a: 1, c: [3,,] })); // eslint-disable-line comma-spacing | ||
// eslint-disable-next-line comma-spacing, no-sparse-arrays | ||
t.ok(deepEqual(res, { a: 1, c: [3,,] })); | ||
@@ -250,2 +265,3 @@ t.ok(deepEqual(res, { a: 1, c: [3] })); | ||
// eslint-disable-next-line no-sparse-arrays | ||
t.ok(deepEqual(res, { a: 1, c: [3,, 5] })); | ||
@@ -252,0 +268,0 @@ |
Sorry, the diff of this file is not supported yet
83378
32
1447
3
+ Addedgopd@^1.0.1
+ Addedwhich-typed-array@^1.1.15
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-abstract@1.23.3(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-to-primitive@1.2.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedobject-inspect@1.13.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedregexp.prototype.flags@1.5.2(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.2(transitive)
+ Addedtyped-array-length@1.0.6(transitive)
+ Addedtypedarray.prototype.slice@1.0.3(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)