@shgysk8zer0/polyfills 0.2.7 → 0.2.9
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 +11 -0
- package/JSON.js +23 -0
- package/Record.js +35 -0
- package/Tuple.js +185 -0
- package/all.js +3 -0
- package/all.min.js +11 -11
- package/all.min.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [v0.2.9] - 2023-12-10
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
- Fix misc issues with `Record` and `Tuple`
|
|
13
|
+
|
|
14
|
+
## [v0.2.8] - 2023-12-10
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- Add `Record()` and `Tuple()`... kinda
|
|
18
|
+
- Add `JSON.parseImmutable()`
|
|
19
|
+
|
|
9
20
|
## [v0.2.7] - 2023-11-26
|
|
10
21
|
|
|
11
22
|
### Added
|
package/JSON.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
if (! (JSON.parseImmutable instanceof Function)) {
|
|
2
|
+
/* eslint-disable no-inner-declarations */
|
|
3
|
+
function getImmutable(thing) {
|
|
4
|
+
switch (typeof thing) {
|
|
5
|
+
case 'object':
|
|
6
|
+
if (thing === null) {
|
|
7
|
+
return null;
|
|
8
|
+
} else if (Array.isArray(thing)) {
|
|
9
|
+
return Tuple.from(thing.map(item => getImmutable(item)));
|
|
10
|
+
}
|
|
11
|
+
return Record.fromEntries(
|
|
12
|
+
Object.entries(thing).map(([key, val]) => [key, getImmutable(val)])
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
default:
|
|
16
|
+
return thing;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
JSON.parseImmutable = function parseImmutable(str) {
|
|
21
|
+
return JSON.parse(str, (_, value) => getImmutable(value));
|
|
22
|
+
};
|
|
23
|
+
}
|
package/Record.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
if (! (globalThis.Record instanceof Function)) {
|
|
2
|
+
/* eslint-disable no-inner-declarations */
|
|
3
|
+
function Record(obj) {
|
|
4
|
+
if (new.target === Record) {
|
|
5
|
+
throw new TypeError('Record is not a constructor');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const record = Object.create(Record.prototype);
|
|
9
|
+
Object.defineProperties(record, Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
10
|
+
key, {
|
|
11
|
+
'enumerable': true,
|
|
12
|
+
'configurable': false,
|
|
13
|
+
'writable': false,
|
|
14
|
+
'value': value,
|
|
15
|
+
}
|
|
16
|
+
])));
|
|
17
|
+
|
|
18
|
+
Object.freeze(record);
|
|
19
|
+
return record;
|
|
20
|
+
}
|
|
21
|
+
|
|
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
|
+
});
|
|
29
|
+
|
|
30
|
+
Record.fromEntries = function fromEntries(entries) {
|
|
31
|
+
return Record(Object.fromEntries(entries));
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
globalThis.Record = Record;
|
|
35
|
+
}
|
package/Tuple.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
if (! (globalThis.Tuple instanceof Function)) {
|
|
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
|
+
|
|
13
|
+
function Tuple(...items) {
|
|
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;
|
|
21
|
+
}
|
|
22
|
+
|
|
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
|
+
};
|
|
30
|
+
|
|
31
|
+
Tuple.prototype.concat = function concat(...values) {
|
|
32
|
+
return Tuple(...this, ...values);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
Tuple.prototype.slice = function slice(start, end) {
|
|
36
|
+
return Tuple.from(Array.prototype.slice.call(this, start, end));
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Tuple.prototype.indexOf = function indexOf(searchElement, fromIndex) {
|
|
40
|
+
return Array.prototype.indexOf.call(this, searchElement, fromIndex);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
Tuple.prototype.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
|
|
44
|
+
return Array.prototype.lastIndexOf.call(this, searchElement, fromIndex);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
Tuple.prototype.findIndex = function findIndex(callbackFn, thisArg) {
|
|
48
|
+
return Array.prototype.findIndex.call(this, callbackFn, thisArg);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
Tuple.prototype.find = function find(callbackFn, thisArg) {
|
|
52
|
+
return Array.prototype.find.call(this, callbackFn, thisArg);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
Tuple.prototype.findLast = function findLast(callbackFn, thisArg) {
|
|
56
|
+
return Array.prototype.findLast.call(this, callbackFn, thisArg);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
Tuple.prototype.findLastIndex = function findLastIndex(callbackFn, thisArg) {
|
|
60
|
+
return Array.prototype.findLastIndex.call(this, callbackFn, thisArg);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Tuple.prototype.every = function every(callbackFn, thisArg) {
|
|
64
|
+
return Array.prototype.every.call(this, callbackFn, thisArg);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
Tuple.prototype.some = function some(callbackFn, thisArg) {
|
|
68
|
+
return Array.prototype.some.call(this, callbackFn, thisArg);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
Tuple.prototype.includes = function includes(searchElement, fromIndex) {
|
|
72
|
+
return Array.prototype.includes.call(this, searchElement, fromIndex);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
Tuple.prototype.map = function map(callbackFn, thisArg) {
|
|
76
|
+
return Tuple.from(Array.prototype.map.call(this, callbackFn, thisArg));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
Tuple.prototype.filter = function filter(callbackFn, thisArg) {
|
|
80
|
+
return Tuple.from(Array.prototype.filter.call(this, callbackFn, thisArg));
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
Tuple.prototype.flat = function flat(depth) {
|
|
84
|
+
return Tuple.from(Array.prototype.flat.call(this, depth));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
Tuple.prototype.flatMap = function flatMap(callbackFn, thisArg) {
|
|
88
|
+
return Tuple.from(Array.prototype.flatMap.call(this, callbackFn, thisArg));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
Tuple.prototype.at = function at(index) {
|
|
92
|
+
return Array.prototype.at.call(this, index);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
Tuple.prototype.reduce = function reduce(callbackFn, initialValue) {
|
|
96
|
+
return Tuple.from(Array.prototype.reduce.call(this, callbackFn, initialValue));
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
Tuple.prototype.reduceRight = function reduceRight(callbackFn, initialValue) {
|
|
100
|
+
return Tuple.from(Array.prototype.reduceRight.call(this, callbackFn, initialValue));
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
Tuple.prototype.toSorted = function toSorted(toStringTag) {
|
|
104
|
+
return Tuple.from(Array.prototype.toSorted.call(this, toStringTag));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
Tuple.prototype.toSpliced = function toSpliced(start, deleteCount, ...items) {
|
|
108
|
+
return Tuple.from(Array.prototype.toSpliced.call(this, start, deleteCount, ...items));
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
Tuple.prototype.toReversed = function toReversed() {
|
|
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);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
Tuple.prototype.toJSON = function toJSON() {
|
|
124
|
+
return [...this];
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
Tuple.prototype.keys = function keys() {
|
|
128
|
+
return Array.prototype.keys.apply(this);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
Tuple.prototype.values = function values(){
|
|
132
|
+
return Array.prototype.values.apply(this);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
Tuple.prototype.entries = function entries(){
|
|
136
|
+
return Array.prototype.entries.apply(this);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
Tuple.prototype[Symbol.iterator] = function() {
|
|
140
|
+
return Array.prototype[Symbol.iterator].apply(this);
|
|
141
|
+
};
|
|
142
|
+
|
|
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);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
Object.defineProperties(Tuple.prototype, {
|
|
155
|
+
'length': {
|
|
156
|
+
get: function() {
|
|
157
|
+
return Object.getOwnPropertyDescriptor(Array.prototype, 'length').get.apply(this);
|
|
158
|
+
},
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: false,
|
|
161
|
+
},
|
|
162
|
+
'lastItem': {
|
|
163
|
+
get: function() {
|
|
164
|
+
return this[this.lastIndex];
|
|
165
|
+
},
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: false,
|
|
168
|
+
},
|
|
169
|
+
'lastIndex': {
|
|
170
|
+
get: function() {
|
|
171
|
+
return Object.getOwnPropertyDescriptor(Array.prototype, 'lastIndex').get.apply(this);
|
|
172
|
+
},
|
|
173
|
+
enumerable: true,
|
|
174
|
+
configurable: false,
|
|
175
|
+
},
|
|
176
|
+
[Symbol.toStringTag]: {
|
|
177
|
+
value: 'Tuple',
|
|
178
|
+
enumerable: true,
|
|
179
|
+
configurable: false,
|
|
180
|
+
writable: false,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
globalThis.Tuple = Tuple;
|
|
185
|
+
}
|