diff --git a/README.md b/README.md index 1f65698..a1f1121 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # @sepiariver/unique-set -Extends the `add` method on the native JavaScript Set object to compare using [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal) +Extends the `has` and `add` methods on the native JavaScript `Set` object to use [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal) as the equality algorithm. -The extended method iterates through the elements of the Set until equality is found. If no elements match, the entire Set would have been iterated to determine so. For a very large Set, there is probably a better way to achieve what you're trying to do, otherwise UniqueSet can be very convenient. +The extended methods iterate through the elements of the `UniqueSet` until equality is found. If no elements match, the entire `UniqueSet` would have been iterated to determine so. However fast `fast-deep-equal` is, calling it in a loop like this makes performance many times poorer than the native `Set`. For datasets greater than a thousand elements, there is probably a better way to achieve what you're trying to do. Otherwise, `UniqueSet` is convenient. + +Requires @babel/core 7+ ## Installation @@ -10,7 +12,7 @@ The extended method iterates through the elements of the Set until equality is f npm install @sepiariver/unique-set ``` -### Usage +## Usage ```js const UniqueSet = require('@sepiariver/unique-set'); @@ -36,9 +38,21 @@ const data = [ [1, 2, 3], ]; -let unique = new UniqueSet(); +let unique1 = new UniqueSet(); data.forEach((el) => { - unique.add(el); + unique1.add(el); }); -console.log(unique.size); // 6 instead of 8 with Set +let unique2 = new UniqueSet(data); +console.log(unique1.size); // 6 instead of 8 with Set +console.log(unique2.size); // 6 ``` + +## Testing + +1. Clone this repo +2. `npm install` +3. `npm run test` + +## Contributing + +Submit pull requests to https://github.com/sepiariver/unique-set/pulls \ No newline at end of file diff --git a/package.json b/package.json index 54df63f..7fa1d57 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@sepiariver/unique-set", "version": "1.1.0", - "description": "Extends the add method on the native JavaScript Set object to compare using fast-deep-equal", + "description": "Extends the has and add methods on the native JavaScript Set object to deeply compare using fast-deep-equal", "main": "index.js", "scripts": { "test": "jest", diff --git a/src/index.js b/src/index.js index bae1abe..644244d 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ class UniqueSet extends Set { super(...args); } has(o) { - for (let i of this) { + for (const i of this) { if (equal(o, i)) { return true; } @@ -14,9 +14,9 @@ class UniqueSet extends Set { } add(o) { if (!this.has(o)) { - Set.prototype.add.call(this, o); + Set.prototype.add.call(this, o); } } } -module.exports = UniqueSet; \ No newline at end of file +module.exports = UniqueSet; diff --git a/test/basic.spec.js b/test/basic.spec.js index d71088a..ae6ddbd 100644 --- a/test/basic.spec.js +++ b/test/basic.spec.js @@ -1,41 +1,40 @@ import UniqueSet from "../dist"; describe("UniqueSet", () => { - it("adds unique objects", () => { - const data = [ - "string", - "another string", - "string", - 1, - 2, - 1, - { - foo: "bar", - bar: "baz", - baz: "lurman", - }, - { - bar: "baz", - baz: "lurman", - foo: "bar", - }, - [1, 2, 3], - [1, 2, 3], - ]; - - const expected = [ - "string", - "another string", - 1, - 2, - { - foo: "bar", - bar: "baz", - baz: "lurman", - }, - [1, 2, 3], - ]; + const data = [ + "string", + "another string", + "string", + 1, + 2, + 1, + { + foo: "bar", + bar: "baz", + baz: "lurman", + }, + { + bar: "baz", + baz: "lurman", + foo: "bar", + }, + [1, 2, 3], + [1, 2, 3], + ]; + const expected = [ + "string", + "another string", + 1, + 2, + { + foo: "bar", + bar: "baz", + baz: "lurman", + }, + [1, 2, 3], + ]; + it("adds unique objects", () => { let unique = new UniqueSet(); data.forEach((el) => { unique.add(el); @@ -53,7 +52,6 @@ describe("UniqueSet", () => { mySet1.add("some text"); // Set [ 1, 5, 'some text' ] const o = { a: 1, b: 2 }; mySet1.add(o); - mySet1.add({ a: 1, b: 2 }); // o is referencing a different object, we treat this differently expect(mySet1.has(1)).toBeTruthy(); @@ -62,7 +60,8 @@ describe("UniqueSet", () => { expect(mySet1.has(Math.sqrt(25))).toBeTruthy(); expect(mySet1.has("Some Text".toLowerCase())).toBeTruthy(); expect(mySet1.has(o)).toBeTruthy(); - expect(mySet1.size).toBe(4); // unique + expect(mySet1.has({ a: 1, b: 2 })).toBeTruthy(); + expect(mySet1.size).toBe(4); // unique objects mySet1.delete(5); // removes 5 from the set expect(mySet1.has(5)).toBeFalsy(); @@ -71,42 +70,11 @@ describe("UniqueSet", () => { expect(mySet1.size).toBe(0); }); - it('works with the contructor', () => { - const data = [ - "string", - "another string", - "string", - 1, - 2, - 1, - { - foo: "bar", - bar: "baz", - baz: "lurman", - }, - { - bar: "baz", - baz: "lurman", - foo: "bar", - }, - [1, 2, 3], - [1, 2, 3], - ]; - - const expected = [ - "string", - "another string", - 1, - 2, - { - foo: "bar", - bar: "baz", - baz: "lurman", - }, - [1, 2, 3], - ]; - let unique = new UniqueSet(data); - expect(Array.from(unique)).toEqual(expected); - expect(unique.size).toBe(6); + it("works with the contructor", () => { + let unique = new UniqueSet(data); + expect(Array.from(unique)).toEqual(expected); + expect(unique.size).toBe(6); + let standard = new Set(data); + expect(standard.size).toBe(8); }); });