@sepiariver/unique-set 1.0.0 → 1.1.0
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 +22 -10
- package/dist/index.js +90 -0
- package/package.json +2 -2
- package/src/index.js +10 -9
- package/.babelrc +0 -5
- package/babel.config.js +0 -3
- package/test/basic.spec.js +0 -71
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @sepiariver/unique-set
|
|
2
|
-
|
|
2
|
+
|
|
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
|
+
|
|
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+
|
|
3
8
|
|
|
4
9
|
## Installation
|
|
5
10
|
|
|
@@ -7,7 +12,7 @@ Extends the add method on the native JavaScript Set object to compare using fast
|
|
|
7
12
|
npm install @sepiariver/unique-set
|
|
8
13
|
```
|
|
9
14
|
|
|
10
|
-
|
|
15
|
+
## Usage
|
|
11
16
|
|
|
12
17
|
```js
|
|
13
18
|
const UniqueSet = require('@sepiariver/unique-set');
|
|
@@ -33,14 +38,21 @@ const data = [
|
|
|
33
38
|
[1, 2, 3],
|
|
34
39
|
];
|
|
35
40
|
|
|
36
|
-
let
|
|
37
|
-
data.forEach((el) => {
|
|
38
|
-
common.add(el);
|
|
39
|
-
});
|
|
40
|
-
console.log(common);
|
|
41
|
-
|
|
42
|
-
let unique = new UniqueSet();
|
|
41
|
+
let unique1 = new UniqueSet();
|
|
43
42
|
data.forEach((el) => {
|
|
44
|
-
|
|
43
|
+
unique1.add(el);
|
|
45
44
|
});
|
|
45
|
+
let unique2 = new UniqueSet(data);
|
|
46
|
+
console.log(unique1.size); // 6 instead of 8 with Set
|
|
47
|
+
console.log(unique2.size); // 6
|
|
46
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
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
+
|
|
5
|
+
var _fastDeepEqual = _interopRequireDefault(require("fast-deep-equal"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
8
|
+
|
|
9
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
10
|
+
|
|
11
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
12
|
+
|
|
13
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
14
|
+
|
|
15
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
16
|
+
|
|
17
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
18
|
+
|
|
19
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
20
|
+
|
|
21
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
22
|
+
|
|
23
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
24
|
+
|
|
25
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
26
|
+
|
|
27
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
28
|
+
|
|
29
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
30
|
+
|
|
31
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
32
|
+
|
|
33
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
34
|
+
|
|
35
|
+
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
|
|
36
|
+
|
|
37
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
38
|
+
|
|
39
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
40
|
+
|
|
41
|
+
var UniqueSet = /*#__PURE__*/function (_Set) {
|
|
42
|
+
_inherits(UniqueSet, _Set);
|
|
43
|
+
|
|
44
|
+
var _super = _createSuper(UniqueSet);
|
|
45
|
+
|
|
46
|
+
function UniqueSet() {
|
|
47
|
+
_classCallCheck(this, UniqueSet);
|
|
48
|
+
|
|
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));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
_createClass(UniqueSet, [{
|
|
57
|
+
key: "has",
|
|
58
|
+
value: function has(o) {
|
|
59
|
+
var _iterator = _createForOfIteratorHelper(this),
|
|
60
|
+
_step;
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
64
|
+
var i = _step.value;
|
|
65
|
+
|
|
66
|
+
if ((0, _fastDeepEqual["default"])(o, i)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} catch (err) {
|
|
71
|
+
_iterator.e(err);
|
|
72
|
+
} finally {
|
|
73
|
+
_iterator.f();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}, {
|
|
79
|
+
key: "add",
|
|
80
|
+
value: function add(o) {
|
|
81
|
+
if (!this.has(o)) {
|
|
82
|
+
Set.prototype.add.call(this, o);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}]);
|
|
86
|
+
|
|
87
|
+
return UniqueSet;
|
|
88
|
+
}( /*#__PURE__*/_wrapNativeSuper(Set));
|
|
89
|
+
|
|
90
|
+
module.exports = UniqueSet;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sepiariver/unique-set",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Extends the add
|
|
3
|
+
"version": "1.1.0",
|
|
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",
|
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
|
-
|
|
8
|
-
|
|
9
|
-
for (let i of this) {
|
|
7
|
+
has(o) {
|
|
8
|
+
for (const i of this) {
|
|
10
9
|
if (equal(o, i)) {
|
|
11
|
-
|
|
12
|
-
break;
|
|
10
|
+
return true;
|
|
13
11
|
}
|
|
14
12
|
}
|
|
15
|
-
|
|
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/babel.config.js
DELETED
package/test/basic.spec.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import UniqueSet from "../dist";
|
|
2
|
-
|
|
3
|
-
describe("UniqueSet", () => {
|
|
4
|
-
it("adds unique objects", () => {
|
|
5
|
-
const data = [
|
|
6
|
-
"string",
|
|
7
|
-
"another string",
|
|
8
|
-
"string",
|
|
9
|
-
1,
|
|
10
|
-
2,
|
|
11
|
-
1,
|
|
12
|
-
{
|
|
13
|
-
foo: "bar",
|
|
14
|
-
bar: "baz",
|
|
15
|
-
baz: "lurman",
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
bar: "baz",
|
|
19
|
-
baz: "lurman",
|
|
20
|
-
foo: "bar",
|
|
21
|
-
},
|
|
22
|
-
[1, 2, 3],
|
|
23
|
-
[1, 2, 3],
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
const expected = [
|
|
27
|
-
"string",
|
|
28
|
-
"another string",
|
|
29
|
-
1,
|
|
30
|
-
2,
|
|
31
|
-
{
|
|
32
|
-
foo: "bar",
|
|
33
|
-
bar: "baz",
|
|
34
|
-
baz: "lurman",
|
|
35
|
-
},
|
|
36
|
-
[1, 2, 3],
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
let unique = new UniqueSet();
|
|
40
|
-
data.forEach((el) => {
|
|
41
|
-
unique.add(el);
|
|
42
|
-
});
|
|
43
|
-
expect(expected).toEqual(Array.from(unique));
|
|
44
|
-
expect(unique.size).toBe(6);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("complies with MDN reference", () => {
|
|
48
|
-
const mySet1 = new UniqueSet();
|
|
49
|
-
|
|
50
|
-
mySet1.add(1); // Set [ 1 ]
|
|
51
|
-
mySet1.add(5); // Set [ 1, 5 ]
|
|
52
|
-
mySet1.add(5); // Set [ 1, 5 ]
|
|
53
|
-
mySet1.add("some text"); // Set [ 1, 5, 'some text' ]
|
|
54
|
-
const o = { a: 1, b: 2 };
|
|
55
|
-
mySet1.add(o);
|
|
56
|
-
|
|
57
|
-
mySet1.add({ a: 1, b: 2 }); // o is referencing a different object, we treat this differently
|
|
58
|
-
|
|
59
|
-
expect(mySet1.has(1)).toBeTruthy();
|
|
60
|
-
expect(mySet1.has(3)).toBeFalsy();
|
|
61
|
-
expect(mySet1.has(5)).toBeTruthy();
|
|
62
|
-
expect(mySet1.has(Math.sqrt(25))).toBeTruthy();
|
|
63
|
-
expect(mySet1.has("Some Text".toLowerCase())).toBeTruthy();
|
|
64
|
-
expect(mySet1.has(o)).toBeTruthy();
|
|
65
|
-
expect(mySet1.size).toBe(4); // unique
|
|
66
|
-
|
|
67
|
-
mySet1.delete(5); // removes 5 from the set
|
|
68
|
-
expect(mySet1.has(5)).toBeFalsy();
|
|
69
|
-
expect(mySet1.size).toBe(3);
|
|
70
|
-
});
|
|
71
|
-
});
|