Skip to content

Commit

Permalink
[New] support cloning Typed Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood authored and ljharb committed Aug 22, 2023
1 parent b5a9d3a commit aa6c277
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"func-style": [2, "declaration"],
"global-require": 1,
"max-lines-per-function": 0,
"max-statements-per-line": 1,
"max-statements-per-line": [1, {"max": 2}],
"multiline-comment-style": 0,
"no-proto": 0,
"no-sparse-arrays": 1,
Expand All @@ -27,6 +27,7 @@
"rules": {
"no-console": 0,
"no-plusplus": 0,
"no-magic-numbers": 0,
},
},
],
Expand Down
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var whichTypedArray = require('which-typed-array');

// TODO: use call-bind, is-date, is-regex, is-string, is-boolean-object, is-number-object
function toS(obj) { return Object.prototype.toString.call(obj); }
function isDate(obj) { return toS(obj) === '[object Date]'; }
Expand Down Expand Up @@ -49,17 +51,22 @@ function copy(src) {
dst = { message: src.message };
} else if (isBoolean(src) || isNumber(src) || isString(src)) {
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) {
dst = global[ta].from(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(objectKeys(src), function (key) {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"in-publish": "^2.0.1",
"npmignore": "^0.3.0",
"safe-publish-latest": "^2.0.0",
"semver": "^6.3.1",
"tape": "^5.6.1"
},
"scripts": {
Expand Down Expand Up @@ -89,5 +90,8 @@
"ignore": [
".github/workflows"
]
},
"dependencies": {
"which-typed-array": "^1.1.11"
}
}
20 changes: 19 additions & 1 deletion test/mutability.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var test = require('tape');
var semver = require('semver');
var assert = require('assert');
var traverse = require('../');
var deepEqual = require('./lib/deep_equal');
Expand Down Expand Up @@ -77,6 +78,20 @@ test('cloneT', function (t) {
t.end();
});

/* global Uint8Array */
test('cloneTypedArray', { skip: semver.satisfies(process.version, '< 4') }, function (t) {
var obj = Uint8Array.from([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, Uint8Array.from([2]));
t.same(res, Uint8Array.from([3]));
t.end();
});

test('reduce', function (t) {
var obj = { a: 1, b: 2, c: [3, 4] };
var res = traverse(obj).reduce(function (acc, x) {
Expand Down Expand Up @@ -194,6 +209,7 @@ test('deleteRedux', function (t) {

t.ok(!deepEqual(obj, { a: 1, c: [3, undefined, 5] }));

// eslint-disable-next-line no-sparse-arrays
t.ok(deepEqual(obj, { a: 1, c: [3,, 5] }));

t.ok(!deepEqual(obj, { a: 1, c: [3, null, 5] }));
Expand All @@ -219,7 +235,8 @@ test('deleteMap', function (t) {

t.ok(deepEqual(res, { a: 1, c: xs }));

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,,] }));

t.ok(deepEqual(res, { a: 1, c: [3] }));

Expand All @@ -244,6 +261,7 @@ test('deleteMapRedux', function (t) {

t.ok(!deepEqual(res, { a: 1, c: [3, 5] }));

// eslint-disable-next-line no-sparse-arrays
t.ok(deepEqual(res, { a: 1, c: [3,, 5] }));

t.end();
Expand Down
14 changes: 14 additions & 0 deletions test/typed-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var test = require('tape');
var semver = require('semver');
var traverse = require('../');

/* global Uint8Array */
test('traverse an UInt8Array', { skip: semver.satisfies(process.version, '< 4') }, function (t) {
var obj = Uint8Array.from('test');
var results = traverse(obj).map(function () {});
t.same(results, obj);
t.end();
});

0 comments on commit aa6c277

Please sign in to comment.