@shgysk8zer0/polyfills 0.2.8 → 0.3.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/CHANGELOG.md CHANGED
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [v0.3.0] - 2024-01-06
10
+
11
+ ### Added
12
+ - Implement `Iterator.from`
13
+
14
+ ### Changed
15
+ - Use iterators instead of generators iterator helper methods
16
+ - Update Set to conform to spec changes
17
+
18
+ ## [v0.2.9] - 2023-12-10
19
+
20
+ ### Fixed
21
+ - Fix misc issues with `Record` and `Tuple`
22
+
9
23
  ## [v0.2.8] - 2023-12-10
10
24
 
11
25
  ### Added
package/Record.js CHANGED
@@ -1,6 +1,10 @@
1
1
  if (! (globalThis.Record instanceof Function)) {
2
2
  /* eslint-disable no-inner-declarations */
3
3
  function Record(obj) {
4
+ if (new.target === Record) {
5
+ throw new TypeError('Record is not a constructor');
6
+ }
7
+
4
8
  const record = Object.create(Record.prototype);
5
9
  Object.defineProperties(record, Object.fromEntries(Object.entries(obj).map(([key, value]) => [
6
10
  key, {
@@ -15,10 +19,16 @@ if (! (globalThis.Record instanceof Function)) {
15
19
  return record;
16
20
  }
17
21
 
18
- Record.prototype = { constructor: Record };
22
+ Record.prototype.constructor = Record;
23
+ Object.defineProperty(Record.prototype, Symbol.toStringTag, {
24
+ value: 'Record',
25
+ enumerable: true,
26
+ configurable: false,
27
+ writable: false,
28
+ });
19
29
 
20
30
  Record.fromEntries = function fromEntries(entries) {
21
- return new Record(Object.fromEntries(entries));
31
+ return Record(Object.fromEntries(entries));
22
32
  };
23
33
 
24
34
  globalThis.Record = Record;
package/Tuple.js CHANGED
@@ -1,104 +1,130 @@
1
1
  if (! (globalThis.Tuple instanceof Function)) {
2
2
  /* eslint-disable no-inner-declarations */
3
+
4
+ /**
5
+ * Creates an unfrozen Tuple, such as for `Tuple.of()`
6
+ */
7
+ const createTuple = items => {
8
+ const tuple = Array.apply(Object.create(Tuple.prototype), items);
9
+ Object.setPrototypeOf(tuple, Tuple.prototype);
10
+ return tuple;
11
+ };
12
+
3
13
  function Tuple(...items) {
4
- const result = Array.apply(Object.create(Tuple.prototype), items);
5
- Object.setPrototypeOf(result, Tuple.prototype);
6
- Object.freeze(result);
7
- return result;
14
+ if (new.target === Tuple) {
15
+ throw new TypeError('Tuple is not a constructor');
16
+ }
17
+
18
+ const tuple = createTuple(items);
19
+ Object.freeze(tuple);
20
+ return tuple;
8
21
  }
9
22
 
10
- // const notAllowed = ['push', 'pop', 'sort', 'reverse', 'splice', 'shift', 'unshift', 'constructor'];
11
- // Tuple.prototype.constructor = Tuple;
23
+ Tuple.prototype.forEach = function forEach(callbackFn, thisArg) {
24
+ Array.prototype.forEach.call(this, callbackFn, thisArg);
25
+ };
26
+
27
+ Tuple.prototype.join = function join(separator = ',') {
28
+ return Array.prototype.join.call(this, separator);
29
+ };
12
30
 
13
- Tuple.prototype.forEach = function forEach() {
14
- Array.prototype.forEach.apply(this, arguments);
31
+ Tuple.prototype.concat = function concat(...values) {
32
+ return Tuple(...this, ...values);
15
33
  };
16
34
 
17
- Tuple.prototype.join = function join() {
18
- return Array.prototype.join.apply(this, arguments);
35
+ Tuple.prototype.slice = function slice(start, end) {
36
+ return Tuple.from(Array.prototype.slice.call(this, start, end));
19
37
  };
20
38
 
21
- Tuple.prototype.indexOf = function indexOf() {
22
- return Array.prototype.indexOf.apply(this, arguments);
39
+ Tuple.prototype.indexOf = function indexOf(searchElement, fromIndex) {
40
+ return Array.prototype.indexOf.call(this, searchElement, fromIndex);
23
41
  };
24
42
 
25
- Tuple.prototype.lastIndexOf = function lastIndexOf() {
26
- return Array.prototype.lastIndexOf.apply(this, arguments);
43
+ Tuple.prototype.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
44
+ return Array.prototype.lastIndexOf.call(this, searchElement, fromIndex);
27
45
  };
28
46
 
29
- Tuple.prototype.findIndex = function findIndex() {
30
- return Array.prototype.findIndex.apply(this, arguments);
47
+ Tuple.prototype.findIndex = function findIndex(callbackFn, thisArg) {
48
+ return Array.prototype.findIndex.call(this, callbackFn, thisArg);
31
49
  };
32
50
 
33
- Tuple.prototype.find = function find() {
34
- return Array.prototype.find.apply(this, arguments);
51
+ Tuple.prototype.find = function find(callbackFn, thisArg) {
52
+ return Array.prototype.find.call(this, callbackFn, thisArg);
35
53
  };
36
54
 
37
- Tuple.prototype.findLast = function findLast() {
38
- return Array.prototype.findLast.apply(this, arguments);
55
+ Tuple.prototype.findLast = function findLast(callbackFn, thisArg) {
56
+ return Array.prototype.findLast.call(this, callbackFn, thisArg);
39
57
  };
40
58
 
41
- Tuple.prototype.findLastIndex = function findLastIndex() {
42
- return Array.prototype.findLastIndex.apply(this, arguments);
59
+ Tuple.prototype.findLastIndex = function findLastIndex(callbackFn, thisArg) {
60
+ return Array.prototype.findLastIndex.call(this, callbackFn, thisArg);
43
61
  };
44
62
 
45
- Tuple.prototype.every = function every() {
46
- return Array.prototype.every.apply(this, arguments);
63
+ Tuple.prototype.every = function every(callbackFn, thisArg) {
64
+ return Array.prototype.every.call(this, callbackFn, thisArg);
47
65
  };
48
66
 
49
- Tuple.prototype.some = function some() {
50
- return Array.prototype.some.apply(this, arguments);
67
+ Tuple.prototype.some = function some(callbackFn, thisArg) {
68
+ return Array.prototype.some.call(this, callbackFn, thisArg);
51
69
  };
52
70
 
53
- Tuple.prototype.includes = function includes() {
54
- return Array.prototype.includes.apply(this, arguments);
71
+ Tuple.prototype.includes = function includes(searchElement, fromIndex) {
72
+ return Array.prototype.includes.call(this, searchElement, fromIndex);
55
73
  };
56
74
 
57
- Tuple.prototype.map = function map() {
58
- return Tuple.from(Array.prototype.map.apply(this, arguments));
75
+ Tuple.prototype.map = function map(callbackFn, thisArg) {
76
+ return Tuple.from(Array.prototype.map.call(this, callbackFn, thisArg));
59
77
  };
60
78
 
61
- Tuple.prototype.filter = function filter() {
62
- return Tuple.from(Array.prototype.filter.apply(this, arguments));
79
+ Tuple.prototype.filter = function filter(callbackFn, thisArg) {
80
+ return Tuple.from(Array.prototype.filter.call(this, callbackFn, thisArg));
63
81
  };
64
82
 
65
- Tuple.prototype.flat = function flat() {
66
- return Tuple.from(Array.prototype.flat.apply(this, arguments));
83
+ Tuple.prototype.flat = function flat(depth) {
84
+ return Tuple.from(Array.prototype.flat.call(this, depth));
67
85
  };
68
86
 
69
- Tuple.prototype.flatMap = function flatMap() {
70
- return Tuple.from(Array.prototype.flatMap.apply(this, arguments));
87
+ Tuple.prototype.flatMap = function flatMap(callbackFn, thisArg) {
88
+ return Tuple.from(Array.prototype.flatMap.call(this, callbackFn, thisArg));
71
89
  };
72
90
 
73
- Tuple.prototype.at = function at() {
74
- return Array.prototype.at.apply(this, arguments);
91
+ Tuple.prototype.at = function at(index) {
92
+ return Array.prototype.at.call(this, index);
75
93
  };
76
94
 
77
- Tuple.prototype.reduce = function reduce() {
78
- return Tuple.from(Array.prototype.reduce.apply(this, arguments));
95
+ Tuple.prototype.reduce = function reduce(callbackFn, initialValue) {
96
+ return Tuple.from(Array.prototype.reduce.call(this, callbackFn, initialValue));
79
97
  };
80
98
 
81
- Tuple.prototype.reduceRight = function reduceRight() {
82
- return Tuple.from(Array.prototype.reduceRight.apply(this, arguments));
99
+ Tuple.prototype.reduceRight = function reduceRight(callbackFn, initialValue) {
100
+ return Tuple.from(Array.prototype.reduceRight.call(this, callbackFn, initialValue));
83
101
  };
84
102
 
85
- Tuple.prototype.toSorted = function toSorted() {
86
- return Tuple.from(Array.prototype.toSorted.apply(this, arguments));
103
+ Tuple.prototype.toSorted = function toSorted(toStringTag) {
104
+ return Tuple.from(Array.prototype.toSorted.call(this, toStringTag));
87
105
  };
88
106
 
89
- Tuple.prototype.toSpliced = function toSpliced() {
90
- return Tuple.from(Array.prototype.toSpliced.apply(this, arguments));
107
+ Tuple.prototype.toSpliced = function toSpliced(start, deleteCount, ...items) {
108
+ return Tuple.from(Array.prototype.toSpliced.call(this, start, deleteCount, ...items));
91
109
  };
92
110
 
93
111
  Tuple.prototype.toReversed = function toReversed() {
94
- return Tuple.from(Array.prototype.toReversed.apply(this, arguments));
112
+ return Tuple.from(Array.prototype.toReversed.apply(this));
113
+ };
114
+
115
+ Tuple.prototype.with = function(index, value) {
116
+ return Tuple.from(Array.prototype.with.call(this, index, value));
117
+ };
118
+
119
+ Tuple.prototype.toLocaleString = function toLocaleString(locales, options) {
120
+ return Array.prototype.toLocaleString.call(this, locales, options);
95
121
  };
96
122
 
97
123
  Tuple.prototype.toJSON = function toJSON() {
98
124
  return [...this];
99
125
  };
100
126
 
101
- Tuple.prototype.keys = function keys(){
127
+ Tuple.prototype.keys = function keys() {
102
128
  return Array.prototype.keys.apply(this);
103
129
  };
104
130
 
@@ -114,8 +140,15 @@ if (! (globalThis.Tuple instanceof Function)) {
114
140
  return Array.prototype[Symbol.iterator].apply(this);
115
141
  };
116
142
 
117
- Tuple.from = function from(items) {
118
- return Tuple(...items);
143
+ Tuple.from = function from(items, mapFn, thisArg) {
144
+ const tuple = createTuple([]);
145
+ Array.prototype.push.apply(tuple, Array.from(items, mapFn, thisArg));
146
+ Object.freeze(tuple);
147
+ return tuple;
148
+ };
149
+
150
+ Tuple.of = function(...items) {
151
+ return Tuple.from(items);
119
152
  };
120
153
 
121
154
  Object.defineProperties(Tuple.prototype, {
@@ -139,7 +172,13 @@ if (! (globalThis.Tuple instanceof Function)) {
139
172
  },
140
173
  enumerable: true,
141
174
  configurable: false,
142
- }
175
+ },
176
+ [Symbol.toStringTag]: {
177
+ value: 'Tuple',
178
+ enumerable: true,
179
+ configurable: false,
180
+ writable: false,
181
+ },
143
182
  });
144
183
 
145
184
  globalThis.Tuple = Tuple;