Socket
Socket
Sign inDemoInstall

traverse

Package Overview
Dependencies
66
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.6.8 to 0.6.9

test/typed-array.js

13

CHANGELOG.md

@@ -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

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc