@oscarpalmer/atoms 0.56.0 → 0.57.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/dist/css/flex.css +3 -2
- package/dist/js/array/chunk.mjs +18 -0
- package/dist/js/array/exists.mjs +8 -0
- package/dist/js/array/filter.mjs +8 -0
- package/dist/js/array/find.mjs +8 -0
- package/dist/js/array/group-by.mjs +33 -0
- package/dist/js/array/index-of.mjs +8 -0
- package/dist/js/{array.js → array/index.js} +95 -72
- package/dist/js/array/index.mjs +30 -0
- package/dist/js/array/insert.mjs +21 -0
- package/dist/js/array/models.mjs +0 -0
- package/dist/js/array/sort.mjs +56 -0
- package/dist/js/array/splice.mjs +9 -0
- package/dist/js/array/to-map.mjs +27 -0
- package/dist/js/array/to-record.mjs +8 -0
- package/dist/js/array/unique.mjs +8 -0
- package/dist/js/colour/hex.mjs +51 -0
- package/dist/js/colour/hsl.mjs +47 -0
- package/dist/js/{colour.js → colour/index.js} +97 -89
- package/dist/js/colour/index.mjs +24 -0
- package/dist/js/colour/models.mjs +0 -0
- package/dist/js/colour/rgb.mjs +76 -0
- package/dist/js/element/data.mjs +29 -0
- package/dist/js/element/find.mjs +65 -0
- package/dist/js/element/index.js +66 -62
- package/dist/js/element/index.mjs +5 -117
- package/dist/js/element/style.mjs +15 -0
- package/dist/js/index.js +600 -558
- package/dist/js/index.mjs +4 -6
- package/dist/js/internal/array-callbacks.mjs +19 -0
- package/dist/js/internal/array-find.mjs +51 -0
- package/dist/js/internal/colour-property.mjs +15 -0
- package/dist/js/internal/element-value.mjs +23 -0
- package/dist/js/internal/value-handle.mjs +22 -0
- package/dist/js/is.js +1 -2
- package/dist/js/query.js +10 -8
- package/dist/js/string/case.mjs +37 -0
- package/dist/js/{string.mjs → string/index.js} +27 -23
- package/dist/js/string/index.mjs +43 -0
- package/dist/js/{clone.mjs → value/clone.mjs} +2 -2
- package/dist/js/value/diff.mjs +73 -0
- package/dist/js/{equal.mjs → value/equal.mjs} +2 -2
- package/dist/js/value/get.mjs +16 -0
- package/dist/js/{value.js → value/index.js} +181 -27
- package/dist/js/value/index.mjs +7 -0
- package/dist/js/value/merge.mjs +29 -0
- package/dist/js/value/set.mjs +36 -0
- package/package.json +56 -14
- package/src/css/flex.scss +1 -1
- package/src/js/array/chunk.ts +24 -0
- package/src/js/array/exists.ts +32 -0
- package/src/js/array/filter.ts +32 -0
- package/src/js/array/find.ts +32 -0
- package/src/js/array/group-by.ts +52 -0
- package/src/js/array/index-of.ts +32 -0
- package/src/js/array/index.ts +31 -0
- package/src/js/array/insert.ts +41 -0
- package/src/js/array/models.ts +32 -0
- package/src/js/array/sort.ts +126 -0
- package/src/js/array/splice.ts +57 -0
- package/src/js/array/to-map.ts +87 -0
- package/src/js/array/to-record.ts +66 -0
- package/src/js/array/unique.ts +27 -0
- package/src/js/colour/hex.ts +69 -0
- package/src/js/colour/hsl.ts +57 -0
- package/src/js/colour/index.ts +26 -0
- package/src/js/colour/models.ts +83 -0
- package/src/js/colour/rgb.ts +101 -0
- package/src/js/element/data.ts +78 -0
- package/src/js/element/find.ts +137 -0
- package/src/js/element/index.ts +3 -293
- package/src/js/element/style.ts +45 -0
- package/src/js/index.ts +4 -6
- package/src/js/internal/array-callbacks.ts +28 -0
- package/src/js/internal/array-find.ts +81 -0
- package/src/js/internal/colour-property.ts +18 -0
- package/src/js/internal/element-value.ts +34 -0
- package/src/js/internal/value-handle.ts +39 -0
- package/src/js/models.ts +5 -0
- package/src/js/string/case.ts +82 -0
- package/src/js/string/index.ts +86 -0
- package/src/js/{clone.ts → value/clone.ts} +2 -2
- package/src/js/value/diff.ts +136 -0
- package/src/js/{equal.ts → value/equal.ts} +2 -2
- package/src/js/value/get.ts +50 -0
- package/src/js/value/index.ts +6 -0
- package/src/js/value/merge.ts +42 -0
- package/src/js/value/set.ts +83 -0
- package/types/array/chunk.d.ts +4 -0
- package/types/array/exists.d.ts +11 -0
- package/types/array/filter.d.ts +11 -0
- package/types/array/find.d.ts +11 -0
- package/types/array/group-by.d.ts +7 -0
- package/types/array/index-of.d.ts +11 -0
- package/types/array/index.d.ts +21 -0
- package/types/array/insert.d.ts +7 -0
- package/types/array/models.d.ts +19 -0
- package/types/array/sort.d.ts +16 -0
- package/types/array/splice.d.ts +18 -0
- package/types/array/to-map.d.ts +32 -0
- package/types/array/to-record.d.ts +32 -0
- package/types/array/unique.d.ts +11 -0
- package/types/colour/hex.d.ts +10 -0
- package/types/colour/hsl.d.ts +7 -0
- package/types/colour/index.d.ts +8 -0
- package/types/{colour.d.ts → colour/models.d.ts} +0 -26
- package/types/colour/rgb.d.ts +11 -0
- package/types/element/data.d.ts +17 -0
- package/types/element/find.d.ts +19 -0
- package/types/element/index.d.ts +3 -44
- package/types/element/style.d.ts +8 -0
- package/types/index.d.ts +4 -6
- package/types/internal/array-callbacks.d.ts +2 -0
- package/types/internal/array-find.d.ts +4 -0
- package/types/internal/colour-property.d.ts +2 -0
- package/types/internal/element-value.d.ts +3 -0
- package/types/internal/value-handle.d.ts +2 -0
- package/types/models.d.ts +4 -0
- package/types/string/case.d.ts +24 -0
- package/types/{string.d.ts → string/index.d.ts} +1 -25
- package/types/value/diff.d.ts +21 -0
- package/types/value/get.d.ts +15 -0
- package/types/value/index.d.ts +6 -0
- package/types/value/merge.d.ts +5 -0
- package/types/value/set.d.ts +16 -0
- package/dist/js/array.mjs +0 -231
- package/dist/js/clone.js +0 -70
- package/dist/js/colour.mjs +0 -182
- package/dist/js/equal.js +0 -103
- package/dist/js/string.js +0 -79
- package/dist/js/value.mjs +0 -158
- package/src/js/array.ts +0 -724
- package/src/js/colour.ts +0 -345
- package/src/js/string.ts +0 -168
- package/src/js/value.ts +0 -341
- package/types/array.d.ts +0 -165
- package/types/value.d.ts +0 -55
- /package/types/{clone.d.ts → value/clone.d.ts} +0 -0
- /package/types/{equal.d.ts → value/equal.d.ts} +0 -0
package/dist/css/flex.css
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/js/array/chunk.ts
|
|
2
|
+
function chunk(array, size) {
|
|
3
|
+
const { length } = array;
|
|
4
|
+
const chunkSize = typeof size === "number" && size > 0 ? size : 32000;
|
|
5
|
+
if (length <= chunkSize) {
|
|
6
|
+
return [array];
|
|
7
|
+
}
|
|
8
|
+
const chunks = [];
|
|
9
|
+
let remaining = Number(length);
|
|
10
|
+
while (remaining > 0) {
|
|
11
|
+
chunks.push(array.splice(0, chunkSize));
|
|
12
|
+
remaining -= chunkSize;
|
|
13
|
+
}
|
|
14
|
+
return chunks;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
chunk
|
|
18
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/js/array/group-by.ts
|
|
2
|
+
import {getCallbacks} from "../internal/array-callbacks";
|
|
3
|
+
function groupBy(array, key) {
|
|
4
|
+
return groupValues(array, key, true, false);
|
|
5
|
+
}
|
|
6
|
+
function groupValues(array, key, arrays, indicable) {
|
|
7
|
+
const callbacks = getCallbacks(undefined, key);
|
|
8
|
+
const hasCallback = typeof callbacks?.key === "function";
|
|
9
|
+
if (!hasCallback && !indicable) {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
const record = {};
|
|
13
|
+
const { length } = array;
|
|
14
|
+
for (let index = 0;index < length; index += 1) {
|
|
15
|
+
const value = array[index];
|
|
16
|
+
const key2 = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
|
|
17
|
+
if (arrays) {
|
|
18
|
+
const existing = record[key2];
|
|
19
|
+
if (Array.isArray(existing)) {
|
|
20
|
+
existing.push(value);
|
|
21
|
+
} else {
|
|
22
|
+
record[key2] = [value];
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
record[key2] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return record;
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
groupValues,
|
|
32
|
+
groupBy
|
|
33
|
+
};
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
// src/js/
|
|
2
|
-
function isKey(value) {
|
|
3
|
-
return typeof value === "number" || typeof value === "string";
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// src/js/array.ts
|
|
1
|
+
// src/js/array/chunk.ts
|
|
7
2
|
function chunk(array, size) {
|
|
8
3
|
const { length } = array;
|
|
9
4
|
const chunkSize = typeof size === "number" && size > 0 ? size : 32000;
|
|
@@ -18,24 +13,51 @@ function chunk(array, size) {
|
|
|
18
13
|
}
|
|
19
14
|
return chunks;
|
|
20
15
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
|
|
17
|
+
// src/js/array/insert.ts
|
|
18
|
+
function insert(array, index, values) {
|
|
19
|
+
insertValues("splice", array, values, index, 0);
|
|
20
|
+
}
|
|
21
|
+
function insertValues(type, array, values, start, deleteCount) {
|
|
22
|
+
const chunked = chunk(values).reverse();
|
|
23
|
+
const { length } = chunked;
|
|
24
|
+
let returned;
|
|
25
|
+
for (let index = 0;index < length; index += 1) {
|
|
26
|
+
const result = array.splice(start, index === 0 ? deleteCount : 0, ...chunked[index]);
|
|
27
|
+
if (returned == null) {
|
|
28
|
+
returned = result;
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
|
-
|
|
26
|
-
const secondAsNumber = Number(second);
|
|
27
|
-
return Number.isNaN(firstAsNumber) || Number.isNaN(secondAsNumber) ? String(first).localeCompare(String(second)) : firstAsNumber - secondAsNumber;
|
|
28
|
-
};
|
|
29
|
-
function exists(array, value, key) {
|
|
30
|
-
return findValue("index", array, value, key) > -1;
|
|
31
|
+
return type === "splice" ? returned : array.length;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
// src/js/array/index.ts
|
|
35
|
+
function compact(array2) {
|
|
36
|
+
return array2.filter((value) => value != null);
|
|
34
37
|
}
|
|
35
|
-
function
|
|
36
|
-
return
|
|
38
|
+
function push(array2, values) {
|
|
39
|
+
return insertValues("push", array2, values, array2.length, 0);
|
|
37
40
|
}
|
|
38
|
-
|
|
41
|
+
|
|
42
|
+
// src/js/internal/array-callbacks.ts
|
|
43
|
+
function getCallbacks(bool, key) {
|
|
44
|
+
if (typeof bool === "function") {
|
|
45
|
+
return { bool };
|
|
46
|
+
}
|
|
47
|
+
if (typeof key === "function") {
|
|
48
|
+
return { key };
|
|
49
|
+
}
|
|
50
|
+
const isString = typeof key === "string";
|
|
51
|
+
if (!isString && typeof key !== "number" || isString && key.includes(".")) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
key: (value) => value?.[key]
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/js/internal/array-find.ts
|
|
60
|
+
function findValue(type, array, value, key) {
|
|
39
61
|
const callbacks = getCallbacks(value, key);
|
|
40
62
|
if (callbacks?.bool == null && callbacks?.key == null) {
|
|
41
63
|
return type === "index" ? array.indexOf(value) : array.find((item) => item === value);
|
|
@@ -52,8 +74,8 @@ var findValue = function(type, array, value, key) {
|
|
|
52
74
|
}
|
|
53
75
|
}
|
|
54
76
|
return type === "index" ? -1 : undefined;
|
|
55
|
-
}
|
|
56
|
-
|
|
77
|
+
}
|
|
78
|
+
function findValues(type, array, value, key) {
|
|
57
79
|
const callbacks = getCallbacks(value, key);
|
|
58
80
|
const { length } = array;
|
|
59
81
|
if (type === "unique" && callbacks?.key == null && length >= 100) {
|
|
@@ -79,26 +101,25 @@ var findValues = function(type, array, value, key) {
|
|
|
79
101
|
}
|
|
80
102
|
}
|
|
81
103
|
return result;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/js/array/exists.ts
|
|
107
|
+
function exists(array, value, key) {
|
|
108
|
+
return findValue("index", array, value, key) > -1;
|
|
109
|
+
}
|
|
110
|
+
// src/js/array/filter.ts
|
|
111
|
+
function filter(array, value, key) {
|
|
112
|
+
return findValues("all", array, value, key);
|
|
113
|
+
}
|
|
114
|
+
// src/js/array/find.ts
|
|
115
|
+
function find(array, value, key) {
|
|
116
|
+
return findValue("value", array, value, key);
|
|
117
|
+
}
|
|
118
|
+
// src/js/array/group-by.ts
|
|
98
119
|
function groupBy(array, key) {
|
|
99
120
|
return groupValues(array, key, true, false);
|
|
100
121
|
}
|
|
101
|
-
|
|
122
|
+
function groupValues(array, key, arrays, indicable) {
|
|
102
123
|
const callbacks = getCallbacks(undefined, key);
|
|
103
124
|
const hasCallback = typeof callbacks?.key === "function";
|
|
104
125
|
if (!hasCallback && !indicable) {
|
|
@@ -121,34 +142,31 @@ var groupValues = function(array, key, arrays, indicable) {
|
|
|
121
142
|
}
|
|
122
143
|
}
|
|
123
144
|
return record;
|
|
124
|
-
}
|
|
145
|
+
}
|
|
146
|
+
// src/js/array/index-of.ts
|
|
125
147
|
function indexOf(array, value, key) {
|
|
126
148
|
return findValue("index", array, value, key);
|
|
127
149
|
}
|
|
128
|
-
|
|
129
|
-
|
|
150
|
+
// src/js/is.ts
|
|
151
|
+
function isKey(value) {
|
|
152
|
+
return typeof value === "number" || typeof value === "string";
|
|
130
153
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const result = array.splice(start, index === 0 ? deleteCount : 0, ...chunked[index]);
|
|
137
|
-
if (returned == null) {
|
|
138
|
-
returned = result;
|
|
139
|
-
}
|
|
154
|
+
|
|
155
|
+
// src/js/array/sort.ts
|
|
156
|
+
var comparison = function(first, second) {
|
|
157
|
+
if (typeof first === "number" && typeof second === "number") {
|
|
158
|
+
return first - second;
|
|
140
159
|
}
|
|
141
|
-
|
|
160
|
+
const firstAsNumber = Number(first);
|
|
161
|
+
const secondAsNumber = Number(second);
|
|
162
|
+
return Number.isNaN(firstAsNumber) || Number.isNaN(secondAsNumber) ? String(first).localeCompare(String(second)) : firstAsNumber - secondAsNumber;
|
|
142
163
|
};
|
|
143
|
-
function
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
function sort(array, first, second) {
|
|
147
|
-
if (array.length < 2) {
|
|
148
|
-
return array;
|
|
164
|
+
function sort(array2, first, second) {
|
|
165
|
+
if (array2.length < 2) {
|
|
166
|
+
return array2;
|
|
149
167
|
}
|
|
150
168
|
if (first == null || typeof first === "boolean") {
|
|
151
|
-
return first === true ?
|
|
169
|
+
return first === true ? array2.sort((first2, second2) => second2 - first2) : array2.sort();
|
|
152
170
|
}
|
|
153
171
|
const direction = second === true ? "desc" : "asc";
|
|
154
172
|
const keys = (Array.isArray(first) ? first : [first]).map((key) => {
|
|
@@ -168,12 +186,12 @@ function sort(array, first, second) {
|
|
|
168
186
|
}).filter((key) => typeof key.callback === "function");
|
|
169
187
|
const { length } = keys;
|
|
170
188
|
if (length === 0) {
|
|
171
|
-
return direction === "asc" ?
|
|
189
|
+
return direction === "asc" ? array2.sort() : array2.sort((first2, second2) => second2 - first2);
|
|
172
190
|
}
|
|
173
191
|
if (length === 1) {
|
|
174
|
-
return
|
|
192
|
+
return array2.sort((first2, second2) => comparison(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1));
|
|
175
193
|
}
|
|
176
|
-
const sorted =
|
|
194
|
+
const sorted = array2.sort((first2, second2) => {
|
|
177
195
|
for (let index = 0;index < length; index += 1) {
|
|
178
196
|
const { callback, direction: direction2 } = keys[index];
|
|
179
197
|
const descending = direction2 === "desc";
|
|
@@ -186,19 +204,21 @@ function sort(array, first, second) {
|
|
|
186
204
|
});
|
|
187
205
|
return sorted;
|
|
188
206
|
}
|
|
189
|
-
|
|
207
|
+
// src/js/array/splice.ts
|
|
208
|
+
function splice(array2, start, amountOrValues, values) {
|
|
190
209
|
const amoutOrValuesIsArray = Array.isArray(amountOrValues);
|
|
191
|
-
return insertValues("splice",
|
|
210
|
+
return insertValues("splice", array2, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array2.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
|
|
192
211
|
}
|
|
193
|
-
|
|
212
|
+
// src/js/array/to-map.ts
|
|
213
|
+
function toMap(array2, first, second) {
|
|
194
214
|
const asArrays = first === true || second === true;
|
|
195
215
|
const callbacks = getCallbacks(undefined, first);
|
|
196
216
|
const hasCallback = typeof callbacks?.key === "function";
|
|
197
217
|
const map = new Map;
|
|
198
|
-
const { length } =
|
|
218
|
+
const { length } = array2;
|
|
199
219
|
for (let index = 0;index < length; index += 1) {
|
|
200
|
-
const value =
|
|
201
|
-
const key = hasCallback ? callbacks?.key?.(value, index,
|
|
220
|
+
const value = array2[index];
|
|
221
|
+
const key = hasCallback ? callbacks?.key?.(value, index, array2) ?? index : index;
|
|
202
222
|
if (asArrays) {
|
|
203
223
|
const existing = map.get(key);
|
|
204
224
|
if (Array.isArray(existing)) {
|
|
@@ -212,11 +232,13 @@ function toMap(array, first, second) {
|
|
|
212
232
|
}
|
|
213
233
|
return map;
|
|
214
234
|
}
|
|
215
|
-
|
|
216
|
-
|
|
235
|
+
// src/js/array/to-record.ts
|
|
236
|
+
function toRecord(array2, first, second) {
|
|
237
|
+
return groupValues(array2, first, first === true || second === true, true);
|
|
217
238
|
}
|
|
218
|
-
|
|
219
|
-
|
|
239
|
+
// src/js/array/unique.ts
|
|
240
|
+
function unique(array2, key) {
|
|
241
|
+
return findValues("unique", array2, undefined, key);
|
|
220
242
|
}
|
|
221
243
|
export {
|
|
222
244
|
unique,
|
|
@@ -231,5 +253,6 @@ export {
|
|
|
231
253
|
find,
|
|
232
254
|
filter,
|
|
233
255
|
exists,
|
|
256
|
+
compact,
|
|
234
257
|
chunk
|
|
235
258
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/js/array/index.ts
|
|
2
|
+
import {insertValues} from "./insert";
|
|
3
|
+
function compact(array) {
|
|
4
|
+
return array.filter((value) => value != null);
|
|
5
|
+
}
|
|
6
|
+
function push(array, values) {
|
|
7
|
+
return insertValues("push", array, values, array.length, 0);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export * from "./chunk";
|
|
11
|
+
export * from "./exists";
|
|
12
|
+
export * from "./filter";
|
|
13
|
+
export * from "./find";
|
|
14
|
+
import {groupBy} from "./group-by";
|
|
15
|
+
|
|
16
|
+
export * from "./index-of";
|
|
17
|
+
import {insert as insert2} from "./insert";
|
|
18
|
+
import {sort} from "./sort";
|
|
19
|
+
|
|
20
|
+
export * from "./splice";
|
|
21
|
+
export * from "./to-map";
|
|
22
|
+
export * from "./to-record";
|
|
23
|
+
export * from "./unique";
|
|
24
|
+
export {
|
|
25
|
+
sort,
|
|
26
|
+
push,
|
|
27
|
+
insert2 as insert,
|
|
28
|
+
groupBy,
|
|
29
|
+
compact
|
|
30
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/js/array/insert.ts
|
|
2
|
+
import {chunk as chunk2} from "./chunk";
|
|
3
|
+
function insert(array, index, values) {
|
|
4
|
+
insertValues("splice", array, values, index, 0);
|
|
5
|
+
}
|
|
6
|
+
function insertValues(type, array, values, start, deleteCount) {
|
|
7
|
+
const chunked = chunk2(values).reverse();
|
|
8
|
+
const { length } = chunked;
|
|
9
|
+
let returned;
|
|
10
|
+
for (let index = 0;index < length; index += 1) {
|
|
11
|
+
const result = array.splice(start, index === 0 ? deleteCount : 0, ...chunked[index]);
|
|
12
|
+
if (returned == null) {
|
|
13
|
+
returned = result;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return type === "splice" ? returned : array.length;
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
insertValues,
|
|
20
|
+
insert
|
|
21
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/js/array/sort.ts
|
|
2
|
+
import {isKey} from "../is";
|
|
3
|
+
var comparison = function(first, second) {
|
|
4
|
+
if (typeof first === "number" && typeof second === "number") {
|
|
5
|
+
return first - second;
|
|
6
|
+
}
|
|
7
|
+
const firstAsNumber = Number(first);
|
|
8
|
+
const secondAsNumber = Number(second);
|
|
9
|
+
return Number.isNaN(firstAsNumber) || Number.isNaN(secondAsNumber) ? String(first).localeCompare(String(second)) : firstAsNumber - secondAsNumber;
|
|
10
|
+
};
|
|
11
|
+
function sort(array, first, second) {
|
|
12
|
+
if (array.length < 2) {
|
|
13
|
+
return array;
|
|
14
|
+
}
|
|
15
|
+
if (first == null || typeof first === "boolean") {
|
|
16
|
+
return first === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
|
|
17
|
+
}
|
|
18
|
+
const direction = second === true ? "desc" : "asc";
|
|
19
|
+
const keys = (Array.isArray(first) ? first : [first]).map((key) => {
|
|
20
|
+
const returned = {
|
|
21
|
+
direction,
|
|
22
|
+
callback: undefined
|
|
23
|
+
};
|
|
24
|
+
if (isKey(key)) {
|
|
25
|
+
returned.callback = (value) => value[key];
|
|
26
|
+
} else if (typeof key === "function") {
|
|
27
|
+
returned.callback = key;
|
|
28
|
+
} else if (typeof key?.value === "function" || isKey(key?.value)) {
|
|
29
|
+
returned.direction = key?.direction ?? direction;
|
|
30
|
+
returned.callback = typeof key.value === "function" ? key.value : (value) => value[key.value];
|
|
31
|
+
}
|
|
32
|
+
return returned;
|
|
33
|
+
}).filter((key) => typeof key.callback === "function");
|
|
34
|
+
const { length } = keys;
|
|
35
|
+
if (length === 0) {
|
|
36
|
+
return direction === "asc" ? array.sort() : array.sort((first2, second2) => second2 - first2);
|
|
37
|
+
}
|
|
38
|
+
if (length === 1) {
|
|
39
|
+
return array.sort((first2, second2) => comparison(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1));
|
|
40
|
+
}
|
|
41
|
+
const sorted = array.sort((first2, second2) => {
|
|
42
|
+
for (let index = 0;index < length; index += 1) {
|
|
43
|
+
const { callback, direction: direction2 } = keys[index];
|
|
44
|
+
const descending = direction2 === "desc";
|
|
45
|
+
const compared = comparison(callback(descending ? second2 : first2), callback(descending ? first2 : second2));
|
|
46
|
+
if (compared !== 0) {
|
|
47
|
+
return compared;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return 0;
|
|
51
|
+
});
|
|
52
|
+
return sorted;
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
sort
|
|
56
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/js/array/splice.ts
|
|
2
|
+
import {insertValues} from "./insert";
|
|
3
|
+
function splice(array, start, amountOrValues, values) {
|
|
4
|
+
const amoutOrValuesIsArray = Array.isArray(amountOrValues);
|
|
5
|
+
return insertValues("splice", array, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
|
|
6
|
+
}
|
|
7
|
+
export {
|
|
8
|
+
splice
|
|
9
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/js/array/to-map.ts
|
|
2
|
+
import {getCallbacks} from "../internal/array-callbacks";
|
|
3
|
+
function toMap(array, first, second) {
|
|
4
|
+
const asArrays = first === true || second === true;
|
|
5
|
+
const callbacks = getCallbacks(undefined, first);
|
|
6
|
+
const hasCallback = typeof callbacks?.key === "function";
|
|
7
|
+
const map = new Map;
|
|
8
|
+
const { length } = array;
|
|
9
|
+
for (let index = 0;index < length; index += 1) {
|
|
10
|
+
const value = array[index];
|
|
11
|
+
const key = hasCallback ? callbacks?.key?.(value, index, array) ?? index : index;
|
|
12
|
+
if (asArrays) {
|
|
13
|
+
const existing = map.get(key);
|
|
14
|
+
if (Array.isArray(existing)) {
|
|
15
|
+
existing.push(value);
|
|
16
|
+
} else {
|
|
17
|
+
map.set(key, [value]);
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
map.set(key, value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return map;
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
toMap
|
|
27
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/js/colour/hex.ts
|
|
2
|
+
import {createRgb} from "./rgb";
|
|
3
|
+
function createHex(original) {
|
|
4
|
+
let value = original.slice();
|
|
5
|
+
const instance = Object.create({
|
|
6
|
+
toHsl() {
|
|
7
|
+
return hexToRgb(value).toHsl();
|
|
8
|
+
},
|
|
9
|
+
toRgb() {
|
|
10
|
+
return hexToRgb(value);
|
|
11
|
+
},
|
|
12
|
+
toString() {
|
|
13
|
+
return `#${value}`;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(instance, "value", {
|
|
17
|
+
get() {
|
|
18
|
+
return `#${value}`;
|
|
19
|
+
},
|
|
20
|
+
set(hex) {
|
|
21
|
+
if (anyPattern.test(hex)) {
|
|
22
|
+
value = getNormalisedHex(hex);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return instance;
|
|
27
|
+
}
|
|
28
|
+
function getHexColour(value) {
|
|
29
|
+
return createHex(anyPattern.test(value) ? getNormalisedHex(value) : "000000");
|
|
30
|
+
}
|
|
31
|
+
var getNormalisedHex = function(value) {
|
|
32
|
+
const normalised = value.replace(/^#/, "");
|
|
33
|
+
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
34
|
+
};
|
|
35
|
+
function hexToRgb(value) {
|
|
36
|
+
const hex = anyPattern.test(value) ? getNormalisedHex(value) : "";
|
|
37
|
+
const pairs = groupedPattern.exec(hex) ?? [];
|
|
38
|
+
const rgb2 = [];
|
|
39
|
+
const { length } = pairs;
|
|
40
|
+
for (let index = 1;index < length; index += 1) {
|
|
41
|
+
rgb2.push(Number.parseInt(pairs[index], 16));
|
|
42
|
+
}
|
|
43
|
+
return createRgb({ blue: rgb2[2] ?? 0, green: rgb2[1] ?? 0, red: rgb2[0] ?? 0 });
|
|
44
|
+
}
|
|
45
|
+
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
46
|
+
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
47
|
+
export {
|
|
48
|
+
hexToRgb,
|
|
49
|
+
getHexColour,
|
|
50
|
+
createHex
|
|
51
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/js/colour/hsl.ts
|
|
2
|
+
import {createProperty} from "../internal/colour-property";
|
|
3
|
+
import {clamp} from "../number";
|
|
4
|
+
import {createRgb} from "./rgb";
|
|
5
|
+
function createHsl(original) {
|
|
6
|
+
const value = { ...original };
|
|
7
|
+
const instance = Object.create({
|
|
8
|
+
toHex() {
|
|
9
|
+
return hslToRgb(value).toHex();
|
|
10
|
+
},
|
|
11
|
+
toRgb() {
|
|
12
|
+
return hslToRgb(value);
|
|
13
|
+
},
|
|
14
|
+
toString() {
|
|
15
|
+
return `hsl(${value.hue}, ${value.saturation}%, ${value.lightness}%)`;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperties(instance, {
|
|
19
|
+
hue: createProperty(value, "hue", 0, 360),
|
|
20
|
+
lightness: createProperty(value, "lightness", 0, 100),
|
|
21
|
+
saturation: createProperty(value, "saturation", 0, 100),
|
|
22
|
+
value: { value }
|
|
23
|
+
});
|
|
24
|
+
return instance;
|
|
25
|
+
}
|
|
26
|
+
function hslToRgb(value) {
|
|
27
|
+
let hue = value.hue % 360;
|
|
28
|
+
if (hue < 0) {
|
|
29
|
+
hue += 360;
|
|
30
|
+
}
|
|
31
|
+
const saturation = value.saturation / 100;
|
|
32
|
+
const lightness = value.lightness / 100;
|
|
33
|
+
function get(value2) {
|
|
34
|
+
const part = (value2 + hue / 30) % 12;
|
|
35
|
+
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
36
|
+
return lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1));
|
|
37
|
+
}
|
|
38
|
+
return createRgb({
|
|
39
|
+
blue: clamp(Math.round(get(4) * 255), 0, 255),
|
|
40
|
+
green: clamp(Math.round(get(8) * 255), 0, 255),
|
|
41
|
+
red: clamp(Math.round(get(0) * 255), 0, 255)
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
hslToRgb,
|
|
46
|
+
createHsl
|
|
47
|
+
};
|