diff --git a/src/data-structures/disjoint-set/DisjointSet.js b/src/data-structures/disjoint-set/DisjointSet.js index 2fe3c25c3c..ad99527c06 100644 --- a/src/data-structures/disjoint-set/DisjointSet.js +++ b/src/data-structures/disjoint-set/DisjointSet.js @@ -6,7 +6,8 @@ export default class DisjointSet { */ constructor(keyCallback) { this.keyCallback = keyCallback; - this.items = {}; + // Item keys must not collide with inherited Object properties. + this.items = Object.create(null); } /** diff --git a/src/data-structures/disjoint-set/DisjointSetItem.js b/src/data-structures/disjoint-set/DisjointSetItem.js index 037d0e0ae3..0b47aa0c5c 100644 --- a/src/data-structures/disjoint-set/DisjointSetItem.js +++ b/src/data-structures/disjoint-set/DisjointSetItem.js @@ -8,7 +8,8 @@ export default class DisjointSetItem { this.keyCallback = keyCallback; /** @var {DisjointSetItem} this.parent */ this.parent = null; - this.children = {}; + // Store __proto__ as a child key instead of changing the registry's prototype. + this.children = Object.create(null); } /** diff --git a/src/data-structures/disjoint-set/__test__/DisjointSet.test.js b/src/data-structures/disjoint-set/__test__/DisjointSet.test.js index db0d019b0a..33611d972b 100644 --- a/src/data-structures/disjoint-set/__test__/DisjointSet.test.js +++ b/src/data-structures/disjoint-set/__test__/DisjointSet.test.js @@ -1,6 +1,51 @@ import DisjointSet from '../DisjointSet'; describe('DisjointSet', () => { + const prototypeKeys = ['__proto__', 'constructor', 'toString', 'hasOwnProperty', 'valueOf']; + + it.each(prototypeKeys)('should treat an absent prototype-named key %s as missing', (key) => { + const disjointSet = new DisjointSet(); + + expect(disjointSet.find(key)).toBeNull(); + expect(() => disjointSet.union(key, 'other')).toThrow('One or two values are not in sets'); + expect(() => disjointSet.inSameSet(key, 'other')).toThrow('One or two values are not in sets'); + }); + + it.each(prototypeKeys)('should register and merge a prototype-named root %s', (key) => { + const disjointSet = new DisjointSet(); + disjointSet.makeSet(key).makeSet('other'); + + expect(disjointSet.find(key)).toBe(key); + expect(disjointSet.inSameSet(key, 'other')).toBe(false); + + disjointSet.union(key, 'other'); + + expect(disjointSet.find('other')).toBe(key); + expect(disjointSet.inSameSet(key, 'other')).toBe(true); + }); + + it.each(prototypeKeys)('should preserve membership when re-adding merged key %s', (key) => { + const disjointSet = new DisjointSet(); + disjointSet.makeSet('root').makeSet(key).union('root', key); + disjointSet.makeSet(key); + + expect(disjointSet.find(key)).toBe('root'); + expect(disjointSet.inSameSet('root', key)).toBe(true); + }); + + it.each(prototypeKeys)('should support custom key extractors returning %s', (key) => { + const keyCallback = (value) => value.key; + const disjointSet = new DisjointSet(keyCallback); + const item = { key }; + const other = { key: 'other' }; + + expect(disjointSet.find(item)).toBeNull(); + disjointSet.makeSet(item).makeSet(other).union(other, item); + + expect(disjointSet.find({ key })).toBe('other'); + expect(disjointSet.inSameSet(item, other)).toBe(true); + }); + it('should throw error when trying to union and check not existing sets', () => { function mergeNotExistingSets() { const disjointSet = new DisjointSet(); diff --git a/src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js b/src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js index 8383093084..072a089979 100644 --- a/src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js +++ b/src/data-structures/disjoint-set/__test__/DisjointSetItem.test.js @@ -1,6 +1,34 @@ import DisjointSetItem from '../DisjointSetItem'; describe('DisjointSetItem', () => { + it.each(['__proto__', 'constructor', 'toString', 'hasOwnProperty', 'valueOf'])( + 'should include a prototype-named child %s in children and rank', + (key) => { + const root = new DisjointSetItem('root'); + const child = new DisjointSetItem(key); + const grandchild = new DisjointSetItem('grandchild'); + child.addChild(grandchild); + root.addChild(child); + + expect(root.getChildren()).toEqual([child]); + expect(root.getRank()).toBe(2); + expect(child.getRoot()).toBe(root); + expect(grandchild.getRoot()).toBe(root); + }, + ); + + it('should retain children whose custom key is __proto__', () => { + const keyCallback = (value) => value.key; + const root = new DisjointSetItem({ key: 'root' }, keyCallback); + const child = new DisjointSetItem({ key: '__proto__' }, keyCallback); + + child.setParent(root); + + expect(root.getChildren()).toEqual([child]); + expect(root.getRank()).toBe(1); + expect(child.getRoot()).toBe(root); + }); + it('should do basic manipulation with disjoint set item', () => { const itemA = new DisjointSetItem('A'); const itemB = new DisjointSetItem('B');