Skip to content

Commit

Permalink
pkg 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sepiariver committed Feb 15, 2022
1 parent 6230db8 commit 169595f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 83 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# @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

```cli
npm install @sepiariver/unique-set
```

### Usage
## Usage

```js
const UniqueSet = require('@sepiariver/unique-set');
Expand All @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
module.exports = UniqueSet;
114 changes: 41 additions & 73 deletions test/basic.spec.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
});
});

0 comments on commit 169595f

Please sign in to comment.