Skip to content

Commit

Permalink
Should not ignore non-cyclic duplicate references.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickd- committed May 19, 2017
1 parent 4ae4c03 commit 0958c8f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
22 changes: 7 additions & 15 deletions lib/NestedObjectMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ module.exports = class NestedObjectMap extends Map {
*/
constructor(object) {
super();
this.knownObjects = new Set();
this.addObject(object);
}

/**
* Deep iterates the passed object and adds it's fields to the map.
*
* @param {object} object - a nested object
* @param {object} [objectPath] for recursive calls
* @param {array} [objectPath] for recursive calls
* @param {array} [knownObjects] within current path for recursive calls
*/
mapObject(object, objectPath = []) {
if (object && !this.knownObjects.has(object) && typeof object === 'object' && object.constructor && object.constructor.name === 'Object') {
this.knownObjects.add(object);
mapObject(object, objectPath = [], knownObjects = []) {
if (object && !knownObjects.includes(object) && typeof object === 'object' && object.constructor && object.constructor.name === 'Object') {
const currentknownObjects = knownObjects.concat(object);
Object.keys(object).forEach((key) => {
const currentPath = objectPath.concat(key);
const value = object[key];
const currentPath = objectPath.concat(key);
this.set(currentPath.join('.'), value);
this.addObject(value, currentPath);
this.mapObject(value, currentPath, currentknownObjects);
});
}
}
Expand All @@ -54,12 +54,4 @@ module.exports = class NestedObjectMap extends Map {
this.mapObject(object, objectPath);
}

/**
* Overrides Maps clear method.
*/
clear() {
this.knownObjects.clear();
super.clear();
}

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nested-object-map",
"version": "1.1.0",
"version": "1.2.0",
"description": "Deep convert a nested Object into a ES6 Map.",
"homepage": "https://github.com/patrickd-/nestedobjectmap.node",
"license": "MIT",
Expand Down
34 changes: 33 additions & 1 deletion test/NestedObjectMap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ describe('new NestedObjectMap()', () => {
const objectMap = new NestedObjectMap(object);
objectMap.has('ref').should.equal(true);
objectMap.get('ref').should.deepEqual(object);
objectMap.has('ref.ref').should.equal(false);
});

it('should not ignore non-cyclic duplicate references', () => {
const ref = { a: 1 };
const object = {
a: { ref },
b: { ref },
};
const objectMap = new NestedObjectMap(object);
objectMap.has('a.ref.a').should.equal(true);
objectMap.has('b.ref.a').should.equal(true);
objectMap.get('a.ref.a').should.deepEqual(ref.a);
objectMap.get('b.ref.a').should.deepEqual(ref.a);
});

it('should not ignore duplicate values', () => {
const object = {
a: null,
b: null,
c: 1,
d: 1,
e: 'a',
f: 'a',
};
const objectMap = new NestedObjectMap(object);
objectMap.has('a').should.equal(true);
objectMap.has('b').should.equal(true);
objectMap.has('c').should.equal(true);
objectMap.has('d').should.equal(true);
objectMap.has('e').should.equal(true);
objectMap.has('f').should.equal(true);
});

it('should ignore non-object values', () => {
Expand Down Expand Up @@ -119,7 +151,7 @@ describe('new NestedObjectMap()', () => {
});

describe('.clear()', () => {
it('should clear the cyclic reference Set', () => {
it('should properly clear the map', () => {
const object = {
a: { b: [1, 2, 3], c: false },
};
Expand Down

0 comments on commit 0958c8f

Please sign in to comment.