File: polyfill.js

package info (click to toggle)
node-es6-set 0.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 260 kB
  • sloc: javascript: 701; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 3,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";

var isValue        = require("type/value/is")
  , clear          = require("es5-ext/array/#/clear")
  , eIndexOf       = require("es5-ext/array/#/e-index-of")
  , setPrototypeOf = require("es5-ext/object/set-prototype-of")
  , callable       = require("es5-ext/object/valid-callable")
  , d              = require("d")
  , ee             = require("event-emitter")
  , Symbol         = require("es6-symbol")
  , iterator       = require("es6-iterator/valid-iterable")
  , forOf          = require("es6-iterator/for-of")
  , Iterator       = require("./lib/iterator")
  , isNative       = require("./is-native-implemented")
  , call           = Function.prototype.call
  , defineProperty = Object.defineProperty
  , getPrototypeOf = Object.getPrototypeOf
  , SetPoly
  , getValues
  , NativeSet;

if (isNative) NativeSet = Set;

module.exports = SetPoly = function Set(/* iterable*/) {
	var iterable = arguments[0], self;
	if (!(this instanceof SetPoly)) throw new TypeError("Constructor requires 'new'");
	if (isNative && setPrototypeOf) self = setPrototypeOf(new NativeSet(), getPrototypeOf(this));
	else self = this;
	if (isValue(iterable)) iterator(iterable);
	defineProperty(self, "__setData__", d("c", []));
	if (!iterable) return self;
	forOf(
		iterable,
		function (value) {
			if (eIndexOf.call(this, value) !== -1) return;
			this.push(value);
		},
		self.__setData__
	);
	return self;
};

if (isNative) {
	if (setPrototypeOf) setPrototypeOf(SetPoly, NativeSet);
	SetPoly.prototype = Object.create(NativeSet.prototype, { constructor: d(SetPoly) });
}

ee(
	Object.defineProperties(SetPoly.prototype, {
		add: d(function (value) {
			if (this.has(value)) return this;
			this.emit("_add", this.__setData__.push(value) - 1, value);
			return this;
		}),
		clear: d(function () {
			if (!this.__setData__.length) return;
			clear.call(this.__setData__);
			this.emit("_clear");
		}),
		delete: d(function (value) {
			var index = eIndexOf.call(this.__setData__, value);
			if (index === -1) return false;
			this.__setData__.splice(index, 1);
			this.emit("_delete", index, value);
			return true;
		}),
		entries: d(function () { return new Iterator(this, "key+value"); }),
		forEach: d(function (cb /*, thisArg*/) {
			var thisArg = arguments[1], iterator, result, value;
			callable(cb);
			iterator = this.values();
			result = iterator._next();
			while (result !== undefined) {
				value = iterator._resolve(result);
				call.call(cb, thisArg, value, value, this);
				result = iterator._next();
			}
		}),
		has: d(function (value) { return eIndexOf.call(this.__setData__, value) !== -1; }),
		keys: d((getValues = function () { return this.values(); })),
		size: d.gs(function () { return this.__setData__.length; }),
		values: d(function () { return new Iterator(this); }),
		toString: d(function () { return "[object Set]"; })
	})
);
defineProperty(SetPoly.prototype, Symbol.iterator, d(getValues));
defineProperty(SetPoly.prototype, Symbol.toStringTag, d("c", "Set"));