Skip to content

Commit 17fe9a1

Browse files
committedDec 1, 2018
Revert all symbol handlings to false by default. Closes #283
1 parent 542939b commit 17fe9a1

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed
 

‎API.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Table of Contents
22

33
* [Object](#object "Object")
4-
* [clone](#cloneobj "clone")
5-
* [cloneWithShallow](#clonewithshallowobj-keys "cloneWithShallow")
4+
* [clone](#cloneobj-options "clone")
5+
* [cloneWithShallow](#clonewithshallowobj-keys-options "cloneWithShallow")
66
* [merge](#mergetarget-source-isnulloverride-ismergearrays "merge")
77
* [applyToDefaults](#applytodefaultsdefaults-options-isnulloverride "applyToDefaults")
88
* [applyToDefaultsWithShallow](#applytodefaultswithshallowdefaults-options-keys "applyToDefaultsWithShallow")
@@ -36,9 +36,13 @@
3636

3737
Hoek provides several helpful methods for objects and arrays.
3838

39-
### clone(obj)
39+
### clone(obj, [options])
4040

41-
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).
41+
Clones an object or an array. A *deep copy* is made (duplicates everything, including values that are
42+
objects, as well as non-enumerable properties) where:
43+
- `obj` - the object to be cloned.
44+
- `options` - optional settings:
45+
- `symbols` - clone symbol properties. Defaults to `false`.
4246

4347
```javascript
4448

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

65-
### cloneWithShallow(obj, keys)
66-
keys is an array of key names to shallow copy
69+
### cloneWithShallow(obj, keys, [options])
6770

68-
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.
71+
Clones an object or array excluding some keys which are shallow copied where:
72+
- `obj` - the object to be cloned.
73+
- `keys` - an array of key names to shallow copy.
74+
- `options` - optional settings:
75+
- `symbols` - clone symbol properties. Defaults to `false`.
6976

7077
```javascript
7178

‎lib/index.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Assert = require('assert');
66
const Crypto = require('crypto');
77
const Path = require('path');
88

9+
const DeepEqual = require('./deep-equal');
910
const Escape = require('./escape');
1011

1112

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

1617

18+
// Deep object or array comparison
19+
20+
exports.deepEqual = DeepEqual;
21+
22+
1723
// Clone object or array
1824

19-
exports.clone = function (obj, seen) {
25+
exports.clone = function (obj, options = {}, _seen = null) {
2026

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

2430
return obj;
2531
}
2632

27-
seen = seen || new Map();
33+
const seen = _seen || new Map();
2834

2935
const lookup = seen.get(obj);
3036
if (lookup) {
@@ -66,7 +72,7 @@ exports.clone = function (obj, seen) {
6672
seen.set(obj, newObj);
6773

6874
if (cloneDeep) {
69-
const keys = Reflect.ownKeys(obj);
75+
const keys = internals.keys(obj, options);
7076
for (let i = 0; i < keys.length; ++i) {
7177
const key = keys[i];
7278

@@ -86,7 +92,7 @@ exports.clone = function (obj, seen) {
8692
enumerable: descriptor ? descriptor.enumerable : true,
8793
writable: true,
8894
configurable: true,
89-
value: exports.clone(obj[key], seen)
95+
value: exports.clone(obj[key], options, seen)
9096
});
9197
}
9298
}
@@ -100,11 +106,15 @@ exports.clone = function (obj, seen) {
100106
};
101107

102108

109+
internals.keys = function (obj, options = {}) {
110+
111+
return options.symbols ? Reflect.ownKeys(obj) : Object.getOwnPropertyNames(obj);
112+
};
113+
114+
103115
// Merge all the properties of source into target, source wins in conflict, and by default null and undefined from source are applied
104116

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

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

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

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

194-
exports.cloneWithShallow = function (source, keys) {
204+
exports.cloneWithShallow = function (source, keys, options) {
195205

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

202212
const storage = internals.store(source, keys); // Move shallow copy items to storage
203-
const copy = exports.clone(source); // Deep copy the rest
213+
const copy = exports.clone(source, options); // Deep copy the rest
204214
internals.restore(copy, source, storage); // Shallow copy the stored items and restore
205215
return copy;
206216
};
@@ -266,18 +276,13 @@ exports.applyToDefaultsWithShallow = function (defaults, options, keys) {
266276
return copy;
267277
}
268278

269-
const storage = internals.store(options, keys); // Move shallow copy items to storage
270-
exports.merge(copy, options, false, false); // Deep copy the rest
271-
internals.restore(copy, options, storage); // Shallow copy the stored items and restore
279+
const storage = internals.store(options, keys); // Move shallow copy items to storage
280+
exports.merge(copy, options, false, false); // Deep copy the rest
281+
internals.restore(copy, options, storage); // Shallow copy the stored items and restore
272282
return copy;
273283
};
274284

275285

276-
// Deep object or array comparison
277-
278-
exports.deepEqual = require('./deep-equal');
279-
280-
281286
// Find the common unique items in two arrays
282287

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

321326
// Test if the reference contains the values
322327

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

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

346-
options = options || {}; // deep, once, only, part
347-
348351
exports.assert(typeof ref === 'string' || typeof ref === 'object', 'Reference must be string or an object');
349352
exports.assert(values.length, 'Values array cannot be empty');
350353

@@ -412,7 +415,7 @@ exports.contain = function (ref, values, options) {
412415
}
413416
}
414417
else {
415-
const keys = Reflect.ownKeys(ref);
418+
const keys = internals.keys(ref, options);
416419
for (let i = 0; i < keys.length; ++i) {
417420
const key = keys[i];
418421
const pos = values.indexOf(key);

‎test/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,13 @@ describe('clone()', () => {
142142
const a = { [sym1]: 1 };
143143
Object.defineProperty(a, sym2, { value: 2 });
144144

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

147147
expect(a).to.equal(b);
148-
expect(Hoek.deepEqual(a, b)).to.be.true();
149148
expect(b[sym1]).to.be.equal(1);
150149
expect(b[sym2]).to.be.equal(2);
150+
151+
expect(Hoek.deepEqual(a, b, { symbols: true })).to.be.true();
151152
});
152153

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

809-
const copy = Hoek.cloneWithShallow(source, [[sym]]);
810+
const copy = Hoek.cloneWithShallow(source, [[sym]], { symbols: true });
810811
expect(copy).to.equal(source);
811812
expect(copy).to.not.shallow.equal(source);
812813
expect(copy.a).to.not.shallow.equal(source.a);
@@ -1785,11 +1786,11 @@ describe('contain()', () => {
17851786
const sym = Symbol();
17861787

17871788
expect(Hoek.contain([sym], sym)).to.be.true();
1788-
expect(Hoek.contain({ [sym]: 1 }, sym)).to.be.true();
1789-
expect(Hoek.contain({ [sym]: 1, a: 2 }, { [sym]: 1 })).to.be.true();
1789+
expect(Hoek.contain({ [sym]: 1 }, sym, { symbols: true })).to.be.true();
1790+
expect(Hoek.contain({ [sym]: 1, a: 2 }, { [sym]: 1 }, { symbols: true })).to.be.true();
17901791

17911792
expect(Hoek.contain([sym], Symbol())).to.be.false();
1792-
expect(Hoek.contain({ [sym]: 1 }, Symbol())).to.be.false();
1793+
expect(Hoek.contain({ [sym]: 1 }, Symbol(), { symbols: true })).to.be.false();
17931794
});
17941795
});
17951796

0 commit comments

Comments
 (0)
Please sign in to comment.