Skip to content

Commit

Permalink
Merge pull request #22 from YellowKirby/symbol-support
Browse files Browse the repository at this point in the history
Support Symbol properties in objects
  • Loading branch information
guigrpa committed Mar 29, 2018
2 parents e2ad74d + 1c12fec commit 6d696fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/timm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ function throwStr(msg: string) {
throw new Error(msg);
}

function getKeysAndSymbols(obj: Object): Array {
const keys = Object.keys(obj);
if (Object.getOwnPropertySymbols) {
return keys.concat(Object.getOwnPropertySymbols(obj));
}

return keys;
}

const hasOwnProperty = {}.hasOwnProperty;

export function clone<T: ArrayOrObject>(obj: T): T {
if (Array.isArray(obj)) return obj.slice();
const keys = Object.keys(obj);
const keys = getKeysAndSymbols(obj);
const out: any = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand All @@ -51,7 +60,7 @@ function doMerge(
for (let idx = 0; idx < rest.length; idx++) {
const obj = rest[idx];
if (obj == null) continue;
const keys = Object.keys(obj);
const keys = getKeysAndSymbols(obj);
if (!keys.length) continue;
for (let j = 0; j <= keys.length; j++) {
const key = keys[j];
Expand Down Expand Up @@ -614,7 +623,7 @@ export function omit(obj: Object, attrs: Array<string> | string): Object {
}
if (!fDoSomething) return obj;
const out = {};
const keys = Object.keys(obj);
const keys = getKeysAndSymbols(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (omitList.indexOf(key) >= 0) continue;
Expand Down
28 changes: 28 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ if (process.env.TEST_MINIFIED_LIB) {
timm = require('../lib/timm');
}

const SYMBOL = Symbol('some symbol');

const ARR = [{ a: 1 }, { a: 2 }, { a: 3, d: { d1: 4, d2: 5, d3: null } }];
const OBJ = {
a: 1,
b: 2,
d: { d1: 3, d2: 4, b: { b: { b: 4 } } },
e: { e1: 'foo', e2: 'bar' },
arr: ['c', 'd'],
[SYMBOL]: 'hello world',
};

//------------------------------------------------
Expand All @@ -29,6 +32,10 @@ test('getIn: object root: deep', t => {
t.is(timm.getIn(OBJ, ['d', 'b', 'b', 'b']), 4);
});

test('getIn: object root: with Symbols', t => {
t.is(timm.getIn(OBJ, [SYMBOL]), 'hello world');
});

test('getIn: array root: shallow', t => {
t.deepEqual(timm.getIn(ARR, [1]), { a: 2 });
});
Expand Down Expand Up @@ -70,6 +77,11 @@ test('set: changing', t => {
t.is(obj2.b, 5);
});

test('set: should support modifing Symbols', t => {
const obj2 = timm.set(OBJ, SYMBOL, 'new value');
t.is(obj2[SYMBOL], 'new value');
});

test("set: should return the same object when it hasn't changed", t => {
const obj2 = timm.set(OBJ, 'b', 2);
t.is(obj2, OBJ);
Expand Down Expand Up @@ -373,6 +385,16 @@ test('mergeDeep: with more than 6 args', t => {
t.deepEqual(obj2, { a: 1, b: { a: 1, b: 2 }, c: 3, d: 4, e: 5, f: 6 });
});

test('merge: should preserve unmodified Symbols', t => {
const obj2 = timm.merge(OBJ, { foo: 'bar' });
t.is(obj2[SYMBOL], OBJ[SYMBOL]);
});

test('merge: should allow updating Symbol properties', t => {
const obj2 = timm.merge(OBJ, { [SYMBOL]: 'bar' });
t.is(obj2[SYMBOL], 'bar');
});

//------------------------------------------------
// mergeIn()
//------------------------------------------------
Expand Down Expand Up @@ -410,6 +432,7 @@ test('omit: with changes (single attribute)', t => {
const obj2 = timm.omit(OBJ, 'a');
t.is(obj2.a, undefined);
t.is(obj2.b, 2);
t.is(obj2[SYMBOL], 'hello world'),
t.not(obj2, OBJ);
t.deepEqual(obj2.d, OBJ.d);
});
Expand All @@ -427,6 +450,11 @@ test("omit: should return the same object when it hasn't changed", t => {
t.deepEqual(obj2, OBJ);
});

test('omit: should support omitting Symbols', t => {
const obj2 = timm.omit(OBJ, SYMBOL);
t.is(obj2[SYMBOL], undefined);
});

//------------------------------------------------
// addDefaults()
//------------------------------------------------
Expand Down

0 comments on commit 6d696fc

Please sign in to comment.