@sepiariver/unique-set 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # @sepiariver/unique-set
2
2
 
3
- Extends the `add` method on the native JavaScript Set object to compare using [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal)
3
+ 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.
4
4
 
5
- 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.
5
+ 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.
6
+
7
+ Requires @babel/core 7+
6
8
 
7
9
  ## Installation
8
10
 
@@ -10,7 +12,7 @@ The extended method iterates through the elements of the Set until equality is f
10
12
  npm install @sepiariver/unique-set
11
13
  ```
12
14
 
13
- ### Usage
15
+ ## Usage
14
16
 
15
17
  ```js
16
18
  const UniqueSet = require('@sepiariver/unique-set');
@@ -36,9 +38,21 @@ const data = [
36
38
  [1, 2, 3],
37
39
  ];
38
40
 
39
- let unique = new UniqueSet();
41
+ let unique1 = new UniqueSet();
40
42
  data.forEach((el) => {
41
- unique.add(el);
43
+ unique1.add(el);
42
44
  });
43
- console.log(unique.size); // 6 instead of 8 with Set
45
+ let unique2 = new UniqueSet(data);
46
+ console.log(unique1.size); // 6 instead of 8 with Set
47
+ console.log(unique2.size); // 6
44
48
  ```
49
+
50
+ ## Testing
51
+
52
+ 1. Clone this repo
53
+ 2. `npm install`
54
+ 3. `npm run test`
55
+
56
+ ## Contributing
57
+
58
+ Submit pull requests to [https://github.com/sepiariver/unique-set/pulls]
package/dist/index.js CHANGED
@@ -46,14 +46,16 @@ var UniqueSet = /*#__PURE__*/function (_Set) {
46
46
  function UniqueSet() {
47
47
  _classCallCheck(this, UniqueSet);
48
48
 
49
- return _super.call(this);
49
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
50
+ args[_key] = arguments[_key];
51
+ }
52
+
53
+ return _super.call.apply(_super, [this].concat(args));
50
54
  }
51
55
 
52
56
  _createClass(UniqueSet, [{
53
- key: "add",
54
- value: function add(o) {
55
- var isUnique = true;
56
-
57
+ key: "has",
58
+ value: function has(o) {
57
59
  var _iterator = _createForOfIteratorHelper(this),
58
60
  _step;
59
61
 
@@ -62,8 +64,7 @@ var UniqueSet = /*#__PURE__*/function (_Set) {
62
64
  var i = _step.value;
63
65
 
64
66
  if ((0, _fastDeepEqual["default"])(o, i)) {
65
- isUnique = false;
66
- break;
67
+ return true;
67
68
  }
68
69
  }
69
70
  } catch (err) {
@@ -72,7 +73,12 @@ var UniqueSet = /*#__PURE__*/function (_Set) {
72
73
  _iterator.f();
73
74
  }
74
75
 
75
- if (isUnique) {
76
+ return false;
77
+ }
78
+ }, {
79
+ key: "add",
80
+ value: function add(o) {
81
+ if (!this.has(o)) {
76
82
  Set.prototype.add.call(this, o);
77
83
  }
78
84
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sepiariver/unique-set",
3
- "version": "1.0.2",
4
- "description": "Extends the add method on the native JavaScript Set object to compare using fast-deep-equal",
3
+ "version": "1.1.1",
4
+ "description": "Extends the has and add methods on the native JavaScript Set object to deeply compare using fast-deep-equal",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "jest",
@@ -29,8 +29,8 @@
29
29
  "fast-deep-equal": "^3.1.3"
30
30
  },
31
31
  "devDependencies": {
32
- "@babel/cli": "^7.17.0",
33
- "@babel/core": "^7.17.2",
32
+ "@babel/cli": "^7.17.6",
33
+ "@babel/core": "^7.17.8",
34
34
  "@babel/preset-env": "^7.16.11",
35
35
  "babel-jest": "^27.5.1",
36
36
  "jest": "^27.5.1"
package/src/index.js CHANGED
@@ -1,21 +1,22 @@
1
1
  import equal from "fast-deep-equal";
2
2
 
3
3
  class UniqueSet extends Set {
4
- constructor() {
5
- super();
4
+ constructor(...args) {
5
+ super(...args);
6
6
  }
7
- add(o) {
8
- let isUnique = true;
9
- for (let i of this) {
7
+ has(o) {
8
+ for (const i of this) {
10
9
  if (equal(o, i)) {
11
- isUnique = false;
12
- break;
10
+ return true;
13
11
  }
14
12
  }
15
- if (isUnique) {
13
+ return false;
14
+ }
15
+ add(o) {
16
+ if (!this.has(o)) {
16
17
  Set.prototype.add.call(this, o);
17
18
  }
18
19
  }
19
20
  }
20
21
 
21
- module.exports = UniqueSet;
22
+ module.exports = UniqueSet;
package/.babelrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "presets": [
3
- "@babel/preset-env"
4
- ]
5
- }
package/babel.config.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- presets: [["@babel/preset-env", { targets: { node: "current" } }]],
3
- };