Skip to content

Commit

Permalink
Revert all symbol handlings to false by default. Closes #283
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Dec 1, 2018
1 parent 542939b commit 17fe9a1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
21 changes: 14 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Table of Contents

* [Object](#object "Object")
* [clone](#cloneobj "clone")
* [cloneWithShallow](#clonewithshallowobj-keys "cloneWithShallow")
* [clone](#cloneobj-options "clone")
* [cloneWithShallow](#clonewithshallowobj-keys-options "cloneWithShallow")
* [merge](#mergetarget-source-isnulloverride-ismergearrays "merge")
* [applyToDefaults](#applytodefaultsdefaults-options-isnulloverride "applyToDefaults")
* [applyToDefaultsWithShallow](#applytodefaultswithshallowdefaults-options-keys "applyToDefaultsWithShallow")
Expand Down Expand Up @@ -36,9 +36,13 @@

Hoek provides several helpful methods for objects and arrays.

### clone(obj)
### clone(obj, [options])

This method is used to clone an object or an array. A *deep copy* is made (duplicates everything, including values that are objects, as well as non-enumerable properties).
Clones an object or an array. A *deep copy* is made (duplicates everything, including values that are
objects, as well as non-enumerable properties) where:
- `obj` - the object to be cloned.
- `options` - optional settings:
- `symbols` - clone symbol properties. Defaults to `false`.

```javascript

Expand All @@ -62,10 +66,13 @@ console.log(nestedObj.x.b); // results in 123456
console.log(copy.x.b); // results in 100
```

### cloneWithShallow(obj, keys)
keys is an array of key names to shallow copy
### cloneWithShallow(obj, keys, [options])

This method is also used to clone an object or array, however any keys listed in the `keys` array are shallow copied while those not listed are deep copied.
Clones an object or array excluding some keys which are shallow copied where:
- `obj` - the object to be cloned.
- `keys` - an array of key names to shallow copy.
- `options` - optional settings:
- `symbols` - clone symbol properties. Defaults to `false`.

```javascript

Expand Down
45 changes: 24 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Assert = require('assert');
const Crypto = require('crypto');
const Path = require('path');

const DeepEqual = require('./deep-equal');
const Escape = require('./escape');


Expand All @@ -14,17 +15,22 @@ const Escape = require('./escape');
const internals = {};


// Deep object or array comparison

exports.deepEqual = DeepEqual;


// Clone object or array

exports.clone = function (obj, seen) {
exports.clone = function (obj, options = {}, _seen = null) {

if (typeof obj !== 'object' ||
obj === null) {

return obj;
}

seen = seen || new Map();
const seen = _seen || new Map();

const lookup = seen.get(obj);
if (lookup) {
Expand Down Expand Up @@ -66,7 +72,7 @@ exports.clone = function (obj, seen) {
seen.set(obj, newObj);

if (cloneDeep) {
const keys = Reflect.ownKeys(obj);
const keys = internals.keys(obj, options);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];

Expand All @@ -86,7 +92,7 @@ exports.clone = function (obj, seen) {
enumerable: descriptor ? descriptor.enumerable : true,
writable: true,
configurable: true,
value: exports.clone(obj[key], seen)
value: exports.clone(obj[key], options, seen)
});
}
}
Expand All @@ -100,11 +106,15 @@ exports.clone = function (obj, seen) {
};


internals.keys = function (obj, options = {}) {

return options.symbols ? Reflect.ownKeys(obj) : Object.getOwnPropertyNames(obj);
};


// Merge all the properties of source into target, source wins in conflict, and by default null and undefined from source are applied

/*eslint-disable */
exports.merge = function (target, source, isNullOverride /* = true */, isMergeArrays /* = true */) {
/*eslint-enable */

exports.assert(target && typeof target === 'object', 'Invalid target value: must be an object');
exports.assert(source === null || source === undefined || typeof source === 'object', 'Invalid source value: must be null, undefined, or an object');
Expand All @@ -126,7 +136,7 @@ exports.merge = function (target, source, isNullOverride /* = true */, isMergeAr
return target;
}

const keys = Reflect.ownKeys(source);
const keys = internals.keys(source);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (key === '__proto__' ||
Expand Down Expand Up @@ -191,7 +201,7 @@ exports.applyToDefaults = function (defaults, options, isNullOverride) {

// Clone an object except for the listed keys which are shallow copied

exports.cloneWithShallow = function (source, keys) {
exports.cloneWithShallow = function (source, keys, options) {

if (!source ||
typeof source !== 'object') {
Expand All @@ -200,7 +210,7 @@ exports.cloneWithShallow = function (source, keys) {
}

const storage = internals.store(source, keys); // Move shallow copy items to storage
const copy = exports.clone(source); // Deep copy the rest
const copy = exports.clone(source, options); // Deep copy the rest
internals.restore(copy, source, storage); // Shallow copy the stored items and restore
return copy;
};
Expand Down Expand Up @@ -266,18 +276,13 @@ exports.applyToDefaultsWithShallow = function (defaults, options, keys) {
return copy;
}

const storage = internals.store(options, keys); // Move shallow copy items to storage
exports.merge(copy, options, false, false); // Deep copy the rest
internals.restore(copy, options, storage); // Shallow copy the stored items and restore
const storage = internals.store(options, keys); // Move shallow copy items to storage
exports.merge(copy, options, false, false); // Deep copy the rest
internals.restore(copy, options, storage); // Shallow copy the stored items and restore
return copy;
};


// Deep object or array comparison

exports.deepEqual = require('./deep-equal');


// Find the common unique items in two arrays

exports.intersect = function (array1, array2, justFirst) {
Expand Down Expand Up @@ -320,7 +325,7 @@ internals.has = function (ref, key) {

// Test if the reference contains the values

exports.contain = function (ref, values, options) {
exports.contain = function (ref, values, options = {}) { // options: { deep, once, only, part, symbols }

/*
string -> string(s)
Expand All @@ -343,8 +348,6 @@ exports.contain = function (ref, values, options) {
values = [].concat(values);
}

options = options || {}; // deep, once, only, part

exports.assert(typeof ref === 'string' || typeof ref === 'object', 'Reference must be string or an object');
exports.assert(values.length, 'Values array cannot be empty');

Expand Down Expand Up @@ -412,7 +415,7 @@ exports.contain = function (ref, values, options) {
}
}
else {
const keys = Reflect.ownKeys(ref);
const keys = internals.keys(ref, options);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const pos = values.indexOf(key);
Expand Down
13 changes: 7 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ describe('clone()', () => {
const a = { [sym1]: 1 };
Object.defineProperty(a, sym2, { value: 2 });

const b = Hoek.clone(a);
const b = Hoek.clone(a, { symbols: true });

expect(a).to.equal(b);
expect(Hoek.deepEqual(a, b)).to.be.true();
expect(b[sym1]).to.be.equal(1);
expect(b[sym2]).to.be.equal(2);

expect(Hoek.deepEqual(a, b, { symbols: true })).to.be.true();
});

it('performs actual copy for shallow keys (no pass by reference)', () => {
Expand Down Expand Up @@ -806,7 +807,7 @@ describe('cloneWithShallow()', () => {
}
};

const copy = Hoek.cloneWithShallow(source, [[sym]]);
const copy = Hoek.cloneWithShallow(source, [[sym]], { symbols: true });
expect(copy).to.equal(source);
expect(copy).to.not.shallow.equal(source);
expect(copy.a).to.not.shallow.equal(source.a);
Expand Down Expand Up @@ -1785,11 +1786,11 @@ describe('contain()', () => {
const sym = Symbol();

expect(Hoek.contain([sym], sym)).to.be.true();
expect(Hoek.contain({ [sym]: 1 }, sym)).to.be.true();
expect(Hoek.contain({ [sym]: 1, a: 2 }, { [sym]: 1 })).to.be.true();
expect(Hoek.contain({ [sym]: 1 }, sym, { symbols: true })).to.be.true();
expect(Hoek.contain({ [sym]: 1, a: 2 }, { [sym]: 1 }, { symbols: true })).to.be.true();

expect(Hoek.contain([sym], Symbol())).to.be.false();
expect(Hoek.contain({ [sym]: 1 }, Symbol())).to.be.false();
expect(Hoek.contain({ [sym]: 1 }, Symbol(), { symbols: true })).to.be.false();
});
});

Expand Down

0 comments on commit 17fe9a1

Please sign in to comment.