@shgysk8zer0/polyfills 0.2.7 → 0.2.8
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 +6 -0
- package/JSON.js +23 -0
- package/Record.js +25 -0
- package/Tuple.js +146 -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,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [v0.2.8] - 2023-12-10
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Add `Record()` and `Tuple()`... kinda
|
|
13
|
+
- Add `JSON.parseImmutable()`
|
|
14
|
+
|
|
9
15
|
## [v0.2.7] - 2023-11-26
|
|
10
16
|
|
|
11
17
|
### 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,25 @@
|
|
|
1
|
+
if (! (globalThis.Record instanceof Function)) {
|
|
2
|
+
/* eslint-disable no-inner-declarations */
|
|
3
|
+
function Record(obj) {
|
|
4
|
+
const record = Object.create(Record.prototype);
|
|
5
|
+
Object.defineProperties(record, Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
6
|
+
key, {
|
|
7
|
+
'enumerable': true,
|
|
8
|
+
'configurable': false,
|
|
9
|
+
'writable': false,
|
|
10
|
+
'value': value,
|
|
11
|
+
}
|
|
12
|
+
])));
|
|
13
|
+
|
|
14
|
+
Object.freeze(record);
|
|
15
|
+
return record;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Record.prototype = { constructor: Record };
|
|
19
|
+
|
|
20
|
+
Record.fromEntries = function fromEntries(entries) {
|
|
21
|
+
return new Record(Object.fromEntries(entries));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
globalThis.Record = Record;
|
|
25
|
+
}
|
package/Tuple.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
if (! (globalThis.Tuple instanceof Function)) {
|
|
2
|
+
/* eslint-disable no-inner-declarations */
|
|
3
|
+
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;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// const notAllowed = ['push', 'pop', 'sort', 'reverse', 'splice', 'shift', 'unshift', 'constructor'];
|
|
11
|
+
// Tuple.prototype.constructor = Tuple;
|
|
12
|
+
|
|
13
|
+
Tuple.prototype.forEach = function forEach() {
|
|
14
|
+
Array.prototype.forEach.apply(this, arguments);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
Tuple.prototype.join = function join() {
|
|
18
|
+
return Array.prototype.join.apply(this, arguments);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
Tuple.prototype.indexOf = function indexOf() {
|
|
22
|
+
return Array.prototype.indexOf.apply(this, arguments);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
Tuple.prototype.lastIndexOf = function lastIndexOf() {
|
|
26
|
+
return Array.prototype.lastIndexOf.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
Tuple.prototype.findIndex = function findIndex() {
|
|
30
|
+
return Array.prototype.findIndex.apply(this, arguments);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
Tuple.prototype.find = function find() {
|
|
34
|
+
return Array.prototype.find.apply(this, arguments);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
Tuple.prototype.findLast = function findLast() {
|
|
38
|
+
return Array.prototype.findLast.apply(this, arguments);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
Tuple.prototype.findLastIndex = function findLastIndex() {
|
|
42
|
+
return Array.prototype.findLastIndex.apply(this, arguments);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
Tuple.prototype.every = function every() {
|
|
46
|
+
return Array.prototype.every.apply(this, arguments);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
Tuple.prototype.some = function some() {
|
|
50
|
+
return Array.prototype.some.apply(this, arguments);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
Tuple.prototype.includes = function includes() {
|
|
54
|
+
return Array.prototype.includes.apply(this, arguments);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
Tuple.prototype.map = function map() {
|
|
58
|
+
return Tuple.from(Array.prototype.map.apply(this, arguments));
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
Tuple.prototype.filter = function filter() {
|
|
62
|
+
return Tuple.from(Array.prototype.filter.apply(this, arguments));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
Tuple.prototype.flat = function flat() {
|
|
66
|
+
return Tuple.from(Array.prototype.flat.apply(this, arguments));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
Tuple.prototype.flatMap = function flatMap() {
|
|
70
|
+
return Tuple.from(Array.prototype.flatMap.apply(this, arguments));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
Tuple.prototype.at = function at() {
|
|
74
|
+
return Array.prototype.at.apply(this, arguments);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
Tuple.prototype.reduce = function reduce() {
|
|
78
|
+
return Tuple.from(Array.prototype.reduce.apply(this, arguments));
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
Tuple.prototype.reduceRight = function reduceRight() {
|
|
82
|
+
return Tuple.from(Array.prototype.reduceRight.apply(this, arguments));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Tuple.prototype.toSorted = function toSorted() {
|
|
86
|
+
return Tuple.from(Array.prototype.toSorted.apply(this, arguments));
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
Tuple.prototype.toSpliced = function toSpliced() {
|
|
90
|
+
return Tuple.from(Array.prototype.toSpliced.apply(this, arguments));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
Tuple.prototype.toReversed = function toReversed() {
|
|
94
|
+
return Tuple.from(Array.prototype.toReversed.apply(this, arguments));
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
Tuple.prototype.toJSON = function toJSON() {
|
|
98
|
+
return [...this];
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
Tuple.prototype.keys = function keys(){
|
|
102
|
+
return Array.prototype.keys.apply(this);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
Tuple.prototype.values = function values(){
|
|
106
|
+
return Array.prototype.values.apply(this);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
Tuple.prototype.entries = function entries(){
|
|
110
|
+
return Array.prototype.entries.apply(this);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
Tuple.prototype[Symbol.iterator] = function() {
|
|
114
|
+
return Array.prototype[Symbol.iterator].apply(this);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
Tuple.from = function from(items) {
|
|
118
|
+
return Tuple(...items);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
Object.defineProperties(Tuple.prototype, {
|
|
122
|
+
'length': {
|
|
123
|
+
get: function() {
|
|
124
|
+
return Object.getOwnPropertyDescriptor(Array.prototype, 'length').get.apply(this);
|
|
125
|
+
},
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: false,
|
|
128
|
+
},
|
|
129
|
+
'lastItem': {
|
|
130
|
+
get: function() {
|
|
131
|
+
return this[this.lastIndex];
|
|
132
|
+
},
|
|
133
|
+
enumerable: true,
|
|
134
|
+
configurable: false,
|
|
135
|
+
},
|
|
136
|
+
'lastIndex': {
|
|
137
|
+
get: function() {
|
|
138
|
+
return Object.getOwnPropertyDescriptor(Array.prototype, 'lastIndex').get.apply(this);
|
|
139
|
+
},
|
|
140
|
+
enumerable: true,
|
|
141
|
+
configurable: false,
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
globalThis.Tuple = Tuple;
|
|
146
|
+
}
|