@oscarpalmer/atoms 0.156.0 → 0.158.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/array/index.js +2 -1
- package/dist/array/slice.js +51 -0
- package/dist/atoms.full.js +253 -124
- package/dist/function/index.js +2 -1
- package/dist/function/once.js +97 -0
- package/dist/index.js +3 -1
- package/package.json +3 -3
- package/src/array/index.ts +1 -0
- package/src/array/slice.ts +245 -0
- package/src/function/index.ts +1 -0
- package/src/function/once.ts +188 -0
- package/src/models.ts +35 -0
- package/types/array/index.d.ts +1 -0
- package/types/array/slice.d.ts +82 -0
- package/types/function/index.d.ts +1 -0
- package/types/function/once.d.ts +17 -0
- package/types/models.d.ts +31 -0
package/dist/array/index.js
CHANGED
|
@@ -14,10 +14,11 @@ import { intersection } from "./intersection.js";
|
|
|
14
14
|
import { partition } from "./partition.js";
|
|
15
15
|
import { push } from "./push.js";
|
|
16
16
|
import { select } from "./select.js";
|
|
17
|
+
import { drop, slice, take } from "./slice.js";
|
|
17
18
|
import { sort } from "./sort.js";
|
|
18
19
|
import { splice } from "./splice.js";
|
|
19
20
|
import { toSet } from "./to-set.js";
|
|
20
21
|
import { toggle } from "./toggle.js";
|
|
21
22
|
import { union } from "./union.js";
|
|
22
23
|
import { update } from "./update.js";
|
|
23
|
-
export { chunk, compact, difference, exists, filter, find, flatten, getArray, indexOf, insert, intersection, partition, push, range, select, shuffle, sort, splice, times, toSet, toggle, union, update };
|
|
24
|
+
export { chunk, compact, difference, drop, exists, filter, find, flatten, getArray, indexOf, insert, intersection, partition, push, range, select, shuffle, slice, sort, splice, take, times, toSet, toggle, union, update };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getArrayCallbacks } from "../internal/array/callbacks.js";
|
|
2
|
+
function drop(array, first, second) {
|
|
3
|
+
return extract(EXTRACT_DROP, array, first, second);
|
|
4
|
+
}
|
|
5
|
+
function extract(type, array, first, second) {
|
|
6
|
+
if (!Array.isArray(array)) return [];
|
|
7
|
+
const { length } = array;
|
|
8
|
+
if (length === 0) return [];
|
|
9
|
+
const isTake = type === EXTRACT_TAKE;
|
|
10
|
+
if (typeof first === "number") {
|
|
11
|
+
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
12
|
+
if (first === 0) return isTake ? [] : array.slice();
|
|
13
|
+
if (isTake) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
14
|
+
return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
|
|
15
|
+
}
|
|
16
|
+
const callbacks = second == null ? getArrayCallbacks(first) : getArrayCallbacks(void 0, void 0, first);
|
|
17
|
+
const isBoolean = callbacks?.bool != null;
|
|
18
|
+
if (callbacks?.bool == null && callbacks?.value == null) return isTake ? array.slice() : [];
|
|
19
|
+
const extracted = [];
|
|
20
|
+
let push = false;
|
|
21
|
+
for (let index = 0; index < length; index += 1) {
|
|
22
|
+
const item = array[index];
|
|
23
|
+
const matches = isBoolean ? callbacks.bool(item, index, array) : Object.is(callbacks.value(item, index, array), second);
|
|
24
|
+
if (isTake) {
|
|
25
|
+
if (isBoolean ? !matches : matches) break;
|
|
26
|
+
extracted.push(item);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (push) extracted.push(item);
|
|
30
|
+
else if (isBoolean ? !matches : matches) {
|
|
31
|
+
push = true;
|
|
32
|
+
if (isBoolean) extracted.push(item);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return extracted;
|
|
36
|
+
}
|
|
37
|
+
function slice(array, first, second) {
|
|
38
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
39
|
+
const firstIsNumber = typeof first === "number";
|
|
40
|
+
const secondIsNumber = typeof second === "number";
|
|
41
|
+
if (!firstIsNumber && !secondIsNumber) return array.slice();
|
|
42
|
+
if (!secondIsNumber) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
43
|
+
if (!firstIsNumber) return array.slice();
|
|
44
|
+
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
45
|
+
}
|
|
46
|
+
function take(array, first, second) {
|
|
47
|
+
return extract(EXTRACT_TAKE, array, first, second);
|
|
48
|
+
}
|
|
49
|
+
var EXTRACT_DROP = "drop";
|
|
50
|
+
var EXTRACT_TAKE = "take";
|
|
51
|
+
export { drop, slice, take };
|
package/dist/atoms.full.js
CHANGED
|
@@ -244,12 +244,12 @@ function findValues(type, array, parameters, mapper) {
|
|
|
244
244
|
const { length } = array;
|
|
245
245
|
const { bool, key, value } = getParameters(parameters);
|
|
246
246
|
const callbacks = getArrayCallbacks(bool, key);
|
|
247
|
-
if (type ===
|
|
247
|
+
if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
|
|
248
248
|
result.matched = [...new Set(array)];
|
|
249
249
|
return result;
|
|
250
250
|
}
|
|
251
251
|
const mapCallback = typeof mapper === "function" ? mapper : void 0;
|
|
252
|
-
if (callbacks?.bool != null || type ===
|
|
252
|
+
if (callbacks?.bool != null || type === "all" && key == null) {
|
|
253
253
|
const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
|
|
254
254
|
for (let index = 0; index < length; index += 1) {
|
|
255
255
|
const item = array[index];
|
|
@@ -262,7 +262,7 @@ function findValues(type, array, parameters, mapper) {
|
|
|
262
262
|
for (let index = 0; index < length; index += 1) {
|
|
263
263
|
const item = array[index];
|
|
264
264
|
const keyed = callbacks?.keyed?.(item, index, array) ?? item;
|
|
265
|
-
if (type ===
|
|
265
|
+
if (type === "all" && Object.is(keyed, value) || type === "unique" && !keys.has(keyed)) {
|
|
266
266
|
keys.add(keyed);
|
|
267
267
|
result.matched.push(mapCallback?.(item, index, array) ?? item);
|
|
268
268
|
} else result.notMatched.push(item);
|
|
@@ -279,7 +279,6 @@ function getParameters(original) {
|
|
|
279
279
|
}
|
|
280
280
|
const FIND_VALUE_INDEX = "index";
|
|
281
281
|
const FIND_VALUE_VALUE = "value";
|
|
282
|
-
const FIND_VALUES_ALL = "all";
|
|
283
282
|
const FIND_VALUES_UNIQUE = "unique";
|
|
284
283
|
const UNIQUE_THRESHOLD = 100;
|
|
285
284
|
function exists(array, ...parameters) {
|
|
@@ -287,11 +286,11 @@ function exists(array, ...parameters) {
|
|
|
287
286
|
return findValue(FIND_VALUE_INDEX, array, parameters) > -1;
|
|
288
287
|
}
|
|
289
288
|
function filter(array, ...parameters) {
|
|
290
|
-
return findValues(
|
|
289
|
+
return findValues("all", array, parameters).matched;
|
|
291
290
|
}
|
|
292
291
|
filter.remove = removeFiltered;
|
|
293
292
|
function removeFiltered(array, ...parameters) {
|
|
294
|
-
return findValues(
|
|
293
|
+
return findValues("all", array, parameters).notMatched;
|
|
295
294
|
}
|
|
296
295
|
function find(array, ...parameters) {
|
|
297
296
|
return findValue(FIND_VALUE_VALUE, array, parameters);
|
|
@@ -366,7 +365,7 @@ function intersection(first, second, key) {
|
|
|
366
365
|
return compareSets(COMPARE_SETS_INTERSECTION, first, second, key);
|
|
367
366
|
}
|
|
368
367
|
function partition(array, ...parameters) {
|
|
369
|
-
const { matched, notMatched } = findValues(
|
|
368
|
+
const { matched, notMatched } = findValues("all", array, parameters);
|
|
370
369
|
return [matched, notMatched];
|
|
371
370
|
}
|
|
372
371
|
/**
|
|
@@ -379,8 +378,57 @@ function push(array, pushed) {
|
|
|
379
378
|
return insertValues("push", array, pushed, array.length, 0);
|
|
380
379
|
}
|
|
381
380
|
function select(array, ...parameters) {
|
|
382
|
-
return findValues(
|
|
381
|
+
return findValues("all", array, parameters, parameters.pop()).matched;
|
|
383
382
|
}
|
|
383
|
+
function drop(array, first, second) {
|
|
384
|
+
return extract(EXTRACT_DROP, array, first, second);
|
|
385
|
+
}
|
|
386
|
+
function extract(type, array, first, second) {
|
|
387
|
+
if (!Array.isArray(array)) return [];
|
|
388
|
+
const { length } = array;
|
|
389
|
+
if (length === 0) return [];
|
|
390
|
+
const isTake = type === EXTRACT_TAKE;
|
|
391
|
+
if (typeof first === "number") {
|
|
392
|
+
if (Math.abs(first) >= length) return isTake ? array.slice() : [];
|
|
393
|
+
if (first === 0) return isTake ? [] : array.slice();
|
|
394
|
+
if (isTake) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
395
|
+
return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
|
|
396
|
+
}
|
|
397
|
+
const callbacks = second == null ? getArrayCallbacks(first) : getArrayCallbacks(void 0, void 0, first);
|
|
398
|
+
const isBoolean = callbacks?.bool != null;
|
|
399
|
+
if (callbacks?.bool == null && callbacks?.value == null) return isTake ? array.slice() : [];
|
|
400
|
+
const extracted = [];
|
|
401
|
+
let push = false;
|
|
402
|
+
for (let index = 0; index < length; index += 1) {
|
|
403
|
+
const item = array[index];
|
|
404
|
+
const matches = isBoolean ? callbacks.bool(item, index, array) : Object.is(callbacks.value(item, index, array), second);
|
|
405
|
+
if (isTake) {
|
|
406
|
+
if (isBoolean ? !matches : matches) break;
|
|
407
|
+
extracted.push(item);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if (push) extracted.push(item);
|
|
411
|
+
else if (isBoolean ? !matches : matches) {
|
|
412
|
+
push = true;
|
|
413
|
+
if (isBoolean) extracted.push(item);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return extracted;
|
|
417
|
+
}
|
|
418
|
+
function slice(array, first, second) {
|
|
419
|
+
if (!Array.isArray(array) || array.length === 0) return [];
|
|
420
|
+
const firstIsNumber = typeof first === "number";
|
|
421
|
+
const secondIsNumber = typeof second === "number";
|
|
422
|
+
if (!firstIsNumber && !secondIsNumber) return array.slice();
|
|
423
|
+
if (!secondIsNumber) return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
|
|
424
|
+
if (!firstIsNumber) return array.slice();
|
|
425
|
+
return first >= 0 ? array.slice(first, second) : array.slice(array.length + first, array.length + second);
|
|
426
|
+
}
|
|
427
|
+
function take(array, first, second) {
|
|
428
|
+
return extract(EXTRACT_TAKE, array, first, second);
|
|
429
|
+
}
|
|
430
|
+
const EXTRACT_DROP = "drop";
|
|
431
|
+
const EXTRACT_TAKE = "take";
|
|
384
432
|
function aggregate(type, array, key) {
|
|
385
433
|
const length = Array.isArray(array) ? array.length : 0;
|
|
386
434
|
if (length === 0) return {
|
|
@@ -968,6 +1016,159 @@ function memoize(callback, options) {
|
|
|
968
1016
|
}
|
|
969
1017
|
const DEFAULT_CACHE_SIZE = 1024;
|
|
970
1018
|
/**
|
|
1019
|
+
* Asserts that a condition is true, throwing an error if it is not
|
|
1020
|
+
* @param condition Condition to assert
|
|
1021
|
+
* @param message Error message
|
|
1022
|
+
* @param error Error constructor
|
|
1023
|
+
*/
|
|
1024
|
+
function assert(condition, message, error) {
|
|
1025
|
+
if (!condition()) throw new (error ?? Error)(message);
|
|
1026
|
+
}
|
|
1027
|
+
assert.condition = assertCondition;
|
|
1028
|
+
assert.defined = assertDefined;
|
|
1029
|
+
assert.instanceOf = assertInstanceOf;
|
|
1030
|
+
assert.is = assertIs;
|
|
1031
|
+
/**
|
|
1032
|
+
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
1033
|
+
* @param condition Condition to assert
|
|
1034
|
+
* @param message Error message
|
|
1035
|
+
* @param error Error constructor
|
|
1036
|
+
* @returns Asserter
|
|
1037
|
+
*/
|
|
1038
|
+
function assertCondition(condition, message, error) {
|
|
1039
|
+
return (value) => {
|
|
1040
|
+
assert(() => condition(value), message, error);
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Asserts that a value is defined throwing an error if it is not
|
|
1045
|
+
* @param value Value to assert
|
|
1046
|
+
* @param message Error message
|
|
1047
|
+
*/
|
|
1048
|
+
function assertDefined(value, message) {
|
|
1049
|
+
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
1053
|
+
* @param constructor Constructor to check against
|
|
1054
|
+
* @param message Error message
|
|
1055
|
+
* @param error Error constructor
|
|
1056
|
+
* @returns Asserter
|
|
1057
|
+
*/
|
|
1058
|
+
function assertInstanceOf(constructor, message, error) {
|
|
1059
|
+
return (value) => {
|
|
1060
|
+
assert(() => value instanceof constructor, message, error);
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
1065
|
+
* @param condition Type guard function to check the value
|
|
1066
|
+
* @param message Error message
|
|
1067
|
+
* @param error Error constructor
|
|
1068
|
+
* @returns Asserter
|
|
1069
|
+
*/
|
|
1070
|
+
function assertIs(condition, message, error) {
|
|
1071
|
+
return (value) => {
|
|
1072
|
+
assert(() => condition(value), message, error);
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
1076
|
+
/**
|
|
1077
|
+
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
1078
|
+
* @param callback Callback to use once
|
|
1079
|
+
* @returns Once callback
|
|
1080
|
+
*/
|
|
1081
|
+
function asyncOnce(callback) {
|
|
1082
|
+
assert(() => typeof callback === "function", MESSAGE_EXPECTATION$1);
|
|
1083
|
+
const state = {
|
|
1084
|
+
called: false,
|
|
1085
|
+
cleared: false,
|
|
1086
|
+
error: false,
|
|
1087
|
+
finished: false,
|
|
1088
|
+
items: [],
|
|
1089
|
+
value: void 0
|
|
1090
|
+
};
|
|
1091
|
+
const fn = (...parameters) => {
|
|
1092
|
+
if (state.cleared) return Promise.reject(new Error(MESSAGE_CLEARED));
|
|
1093
|
+
if (state.finished) return state.error ? Promise.reject(state.value) : Promise.resolve(state.value);
|
|
1094
|
+
if (state.called) return new Promise((resolve, reject) => {
|
|
1095
|
+
state.items.push({
|
|
1096
|
+
reject,
|
|
1097
|
+
resolve
|
|
1098
|
+
});
|
|
1099
|
+
});
|
|
1100
|
+
state.called = true;
|
|
1101
|
+
return new Promise((resolve, reject) => {
|
|
1102
|
+
state.items.push({
|
|
1103
|
+
reject,
|
|
1104
|
+
resolve
|
|
1105
|
+
});
|
|
1106
|
+
callback(...parameters).then((value) => {
|
|
1107
|
+
handleResult$1(state, value, false);
|
|
1108
|
+
}).catch((error) => {
|
|
1109
|
+
handleResult$1(state, error, true);
|
|
1110
|
+
});
|
|
1111
|
+
});
|
|
1112
|
+
};
|
|
1113
|
+
Object.defineProperties(fn, {
|
|
1114
|
+
called: { get: () => state.called },
|
|
1115
|
+
cleared: { get: () => state.cleared },
|
|
1116
|
+
error: { get: () => state.error },
|
|
1117
|
+
finished: { get: () => state.finished }
|
|
1118
|
+
});
|
|
1119
|
+
fn.clear = () => {
|
|
1120
|
+
if (!state.called || !state.finished || state.cleared) return;
|
|
1121
|
+
state.cleared = true;
|
|
1122
|
+
state.value = void 0;
|
|
1123
|
+
};
|
|
1124
|
+
return fn;
|
|
1125
|
+
}
|
|
1126
|
+
function handleResult$1(state, value, error) {
|
|
1127
|
+
state.error = error;
|
|
1128
|
+
state.finished = true;
|
|
1129
|
+
state.value = value;
|
|
1130
|
+
const items = state.items.splice(0);
|
|
1131
|
+
const { length } = items;
|
|
1132
|
+
for (let index = 0; index < length; index += 1) {
|
|
1133
|
+
const { reject, resolve } = items[index];
|
|
1134
|
+
if (error) reject(value);
|
|
1135
|
+
else resolve(value);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Create a function that can only be called once, returning the same value on subsequent calls
|
|
1140
|
+
* @param callback Callback to use once
|
|
1141
|
+
* @returns Once callback
|
|
1142
|
+
*/
|
|
1143
|
+
function once(callback) {
|
|
1144
|
+
assert(() => typeof callback === "function", MESSAGE_EXPECTATION$1);
|
|
1145
|
+
const state = {
|
|
1146
|
+
called: false,
|
|
1147
|
+
cleared: false,
|
|
1148
|
+
value: void 0
|
|
1149
|
+
};
|
|
1150
|
+
const fn = (...parameters) => {
|
|
1151
|
+
if (state.cleared) throw new Error(MESSAGE_CLEARED);
|
|
1152
|
+
if (state.called) return state.value;
|
|
1153
|
+
state.called = true;
|
|
1154
|
+
state.value = callback(...parameters);
|
|
1155
|
+
return state.value;
|
|
1156
|
+
};
|
|
1157
|
+
Object.defineProperties(fn, {
|
|
1158
|
+
called: { get: () => state.called },
|
|
1159
|
+
cleared: { get: () => state.cleared }
|
|
1160
|
+
});
|
|
1161
|
+
fn.clear = () => {
|
|
1162
|
+
if (!state.called || state.cleared) return;
|
|
1163
|
+
state.cleared = true;
|
|
1164
|
+
state.value = void 0;
|
|
1165
|
+
};
|
|
1166
|
+
return fn;
|
|
1167
|
+
}
|
|
1168
|
+
once.async = asyncOnce;
|
|
1169
|
+
const MESSAGE_CLEARED = "Once has been cleared";
|
|
1170
|
+
const MESSAGE_EXPECTATION$1 = "Once expected a function";
|
|
1171
|
+
/**
|
|
971
1172
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
972
1173
|
*
|
|
973
1174
|
* On subsequent calls, the timer is reset and will wait another `time` milliseconds _(and so on...)_
|
|
@@ -1086,64 +1287,6 @@ function isOk(value) {
|
|
|
1086
1287
|
function isResult(value) {
|
|
1087
1288
|
return _isResult(value, true) || _isResult(value, false);
|
|
1088
1289
|
}
|
|
1089
|
-
/**
|
|
1090
|
-
* Asserts that a condition is true, throwing an error if it is not
|
|
1091
|
-
* @param condition Condition to assert
|
|
1092
|
-
* @param message Error message
|
|
1093
|
-
* @param error Error constructor
|
|
1094
|
-
*/
|
|
1095
|
-
function assert(condition, message, error) {
|
|
1096
|
-
if (!condition()) throw new (error ?? Error)(message);
|
|
1097
|
-
}
|
|
1098
|
-
assert.condition = assertCondition;
|
|
1099
|
-
assert.defined = assertDefined;
|
|
1100
|
-
assert.instanceOf = assertInstanceOf;
|
|
1101
|
-
assert.is = assertIs;
|
|
1102
|
-
/**
|
|
1103
|
-
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
1104
|
-
* @param condition Condition to assert
|
|
1105
|
-
* @param message Error message
|
|
1106
|
-
* @param error Error constructor
|
|
1107
|
-
* @returns Asserter
|
|
1108
|
-
*/
|
|
1109
|
-
function assertCondition(condition, message, error) {
|
|
1110
|
-
return (value) => {
|
|
1111
|
-
assert(() => condition(value), message, error);
|
|
1112
|
-
};
|
|
1113
|
-
}
|
|
1114
|
-
/**
|
|
1115
|
-
* Asserts that a value is defined throwing an error if it is not
|
|
1116
|
-
* @param value Value to assert
|
|
1117
|
-
* @param message Error message
|
|
1118
|
-
*/
|
|
1119
|
-
function assertDefined(value, message) {
|
|
1120
|
-
assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
|
|
1121
|
-
}
|
|
1122
|
-
/**
|
|
1123
|
-
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
1124
|
-
* @param constructor Constructor to check against
|
|
1125
|
-
* @param message Error message
|
|
1126
|
-
* @param error Error constructor
|
|
1127
|
-
* @returns Asserter
|
|
1128
|
-
*/
|
|
1129
|
-
function assertInstanceOf(constructor, message, error) {
|
|
1130
|
-
return (value) => {
|
|
1131
|
-
assert(() => value instanceof constructor, message, error);
|
|
1132
|
-
};
|
|
1133
|
-
}
|
|
1134
|
-
/**
|
|
1135
|
-
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
1136
|
-
* @param condition Type guard function to check the value
|
|
1137
|
-
* @param message Error message
|
|
1138
|
-
* @param error Error constructor
|
|
1139
|
-
* @returns Asserter
|
|
1140
|
-
*/
|
|
1141
|
-
function assertIs(condition, message, error) {
|
|
1142
|
-
return (value) => {
|
|
1143
|
-
assert(() => condition(value), message, error);
|
|
1144
|
-
};
|
|
1145
|
-
}
|
|
1146
|
-
const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
|
|
1147
1290
|
function asyncFlow(...fns) {
|
|
1148
1291
|
assertFlowFunctions(fns);
|
|
1149
1292
|
return (...args) => asyncWork(args.map((value) => {
|
|
@@ -2239,14 +2382,10 @@ function getObserver(first, second, third) {
|
|
|
2239
2382
|
}
|
|
2240
2383
|
const MESSAGE_BEACON = "Cannot retrieve observable from a destroyed beacon";
|
|
2241
2384
|
const MESSAGE_OBSERVABLE = "Cannot subscribe to a destroyed observable";
|
|
2242
|
-
const
|
|
2243
|
-
const ALPHA_FULL_HEX_LONG = `${ALPHA_FULL_HEX_SHORT}${ALPHA_FULL_HEX_SHORT}`;
|
|
2244
|
-
const ALPHA_FULL_VALUE = 1;
|
|
2245
|
-
const ALPHA_NONE_HEX = "00";
|
|
2246
|
-
const ALPHA_NONE_VALUE = 0;
|
|
2385
|
+
const ALPHA_FULL_HEX_LONG = `ff`;
|
|
2247
2386
|
const DEFAULT_ALPHA = {
|
|
2248
2387
|
hex: ALPHA_FULL_HEX_LONG,
|
|
2249
|
-
value:
|
|
2388
|
+
value: 1
|
|
2250
2389
|
};
|
|
2251
2390
|
const DEFAULT_HSL = {
|
|
2252
2391
|
hue: 0,
|
|
@@ -2263,8 +2402,6 @@ const EXPRESSION_HEX_SHORT = /^#?([a-f0-9]{3,4})$/i;
|
|
|
2263
2402
|
const EXPRESSION_PREFIX = /^#/;
|
|
2264
2403
|
const HEX_BLACK = "000000";
|
|
2265
2404
|
const HEX_WHITE = "ffffff";
|
|
2266
|
-
const LENGTH_LONG = 6;
|
|
2267
|
-
const LENGTH_SHORT = 3;
|
|
2268
2405
|
const KEYS_HSL = [
|
|
2269
2406
|
"hue",
|
|
2270
2407
|
"saturation",
|
|
@@ -2277,18 +2414,10 @@ const KEYS_RGB = [
|
|
|
2277
2414
|
"blue"
|
|
2278
2415
|
];
|
|
2279
2416
|
const KEYS_RGBA = [...KEYS_RGB, "alpha"];
|
|
2280
|
-
const MAX_DEGREE = 360;
|
|
2281
|
-
const MAX_HEX = 255;
|
|
2282
|
-
const MAX_PERCENT = 100;
|
|
2283
|
-
const SRGB_LUMINANCE_BLUE = .0722;
|
|
2284
2417
|
const SRGB_LUMINANCE_EXPONENT = 2.4;
|
|
2285
|
-
const SRGB_LUMINANCE_GREEN = .7152;
|
|
2286
|
-
const SRGB_LUMINANCE_MINIMUM = .03928;
|
|
2287
2418
|
const SRGB_LUMINANCE_MODIFIER = 1.055;
|
|
2288
2419
|
const SRGB_LUMINANCE_MULTIPLIER = 12.92;
|
|
2289
2420
|
const SRGB_LUMINANCE_OFFSET = .055;
|
|
2290
|
-
const SRGB_LUMINANCE_RED = .2126;
|
|
2291
|
-
const SRGB_LUMINANCE_THRESHOLD = .625;
|
|
2292
2421
|
function formatColor(space, color, alpha) {
|
|
2293
2422
|
const suffix = alpha ? ` / ${color.alpha}` : "";
|
|
2294
2423
|
const value = color[space];
|
|
@@ -2302,14 +2431,14 @@ function getAlpha(value) {
|
|
|
2302
2431
|
if (typeof value === "number") return getAlphaFromValue(value);
|
|
2303
2432
|
if (typeof value === "string" && value !== ALPHA_FULL_HEX_LONG) return {
|
|
2304
2433
|
hex: value,
|
|
2305
|
-
value: Number.parseInt(value, 16) /
|
|
2434
|
+
value: Number.parseInt(value, 16) / 255
|
|
2306
2435
|
};
|
|
2307
2436
|
return { ...DEFAULT_ALPHA };
|
|
2308
2437
|
}
|
|
2309
2438
|
function getAlphaHexadecimal(value) {
|
|
2310
|
-
if (value ===
|
|
2311
|
-
if (value ===
|
|
2312
|
-
return Math.round(value *
|
|
2439
|
+
if (value === 0) return "00";
|
|
2440
|
+
if (value === 1) return ALPHA_FULL_HEX_LONG;
|
|
2441
|
+
return Math.round(value * 255).toString(16);
|
|
2313
2442
|
}
|
|
2314
2443
|
function getAlphaFromValue(value) {
|
|
2315
2444
|
const alpha = getAlphaValue(value);
|
|
@@ -2319,18 +2448,18 @@ function getAlphaFromValue(value) {
|
|
|
2319
2448
|
};
|
|
2320
2449
|
}
|
|
2321
2450
|
function getAlphaValue(original) {
|
|
2322
|
-
if (Number.isNaN(original) || original >=
|
|
2323
|
-
if (original <
|
|
2324
|
-
return original <=
|
|
2451
|
+
if (Number.isNaN(original) || original >= 100 || original === 1) return 1;
|
|
2452
|
+
if (original < 0) return 0;
|
|
2453
|
+
return original <= 1 ? original : original / 100;
|
|
2325
2454
|
}
|
|
2326
2455
|
function hasKeys(value, keys) {
|
|
2327
2456
|
return typeof value === "object" && value !== null && keys.every((key) => key in value);
|
|
2328
2457
|
}
|
|
2329
2458
|
function isAlpha(value) {
|
|
2330
|
-
return typeof value === "number" && between(value,
|
|
2459
|
+
return typeof value === "number" && between(value, 0, 1);
|
|
2331
2460
|
}
|
|
2332
2461
|
function isBytey(value) {
|
|
2333
|
-
return typeof value === "number" && between(value, 0,
|
|
2462
|
+
return typeof value === "number" && between(value, 0, 255);
|
|
2334
2463
|
}
|
|
2335
2464
|
/**
|
|
2336
2465
|
* Is the value a Color?
|
|
@@ -2352,7 +2481,7 @@ function isColorValue(obj, properties) {
|
|
|
2352
2481
|
return true;
|
|
2353
2482
|
}
|
|
2354
2483
|
function isDegree(value) {
|
|
2355
|
-
return typeof value === "number" && between(value, 0,
|
|
2484
|
+
return typeof value === "number" && between(value, 0, 360);
|
|
2356
2485
|
}
|
|
2357
2486
|
/**
|
|
2358
2487
|
* Is the value a hex color?
|
|
@@ -2363,7 +2492,7 @@ function isDegree(value) {
|
|
|
2363
2492
|
function isHexColor(value, alpha) {
|
|
2364
2493
|
if (typeof value !== "string") return false;
|
|
2365
2494
|
if (!(EXPRESSION_HEX_SHORT.test(value) || EXPRESSION_HEX_LONG.test(value))) return false;
|
|
2366
|
-
if (alpha === false) return value.length ===
|
|
2495
|
+
if (alpha === false) return value.length === 3 || value.length === 6;
|
|
2367
2496
|
return true;
|
|
2368
2497
|
}
|
|
2369
2498
|
/**
|
|
@@ -2405,7 +2534,7 @@ function isRgbLike(value) {
|
|
|
2405
2534
|
return hasKeys(value, KEYS_RGB);
|
|
2406
2535
|
}
|
|
2407
2536
|
function isPercentage(value) {
|
|
2408
|
-
return typeof value === "number" && between(value, 0,
|
|
2537
|
+
return typeof value === "number" && between(value, 0, 100);
|
|
2409
2538
|
}
|
|
2410
2539
|
const validators = {
|
|
2411
2540
|
alpha: isAlpha,
|
|
@@ -2427,17 +2556,17 @@ function getClampedValue(value, minimum, maximum) {
|
|
|
2427
2556
|
function getForegroundColor(value) {
|
|
2428
2557
|
const { blue, green, red } = getState(value).rgb;
|
|
2429
2558
|
const values = [
|
|
2430
|
-
blue /
|
|
2431
|
-
green /
|
|
2432
|
-
red /
|
|
2559
|
+
blue / 255,
|
|
2560
|
+
green / 255,
|
|
2561
|
+
red / 255
|
|
2433
2562
|
];
|
|
2434
2563
|
const { length } = values;
|
|
2435
2564
|
for (let index = 0; index < length; index += 1) {
|
|
2436
2565
|
const color = values[index];
|
|
2437
|
-
if (color <=
|
|
2566
|
+
if (color <= .03928) values[index] /= SRGB_LUMINANCE_MULTIPLIER;
|
|
2438
2567
|
else values[index] = ((color + SRGB_LUMINANCE_OFFSET) / SRGB_LUMINANCE_MODIFIER) ** SRGB_LUMINANCE_EXPONENT;
|
|
2439
2568
|
}
|
|
2440
|
-
return new Color(
|
|
2569
|
+
return new Color(.2126 * values[2] + .7152 * values[1] + .0722 * values[0] > .625 ? HEX_BLACK : HEX_WHITE);
|
|
2441
2570
|
}
|
|
2442
2571
|
/**
|
|
2443
2572
|
* Get the hex color _(with alpha channel)_ from any kind of value
|
|
@@ -2457,10 +2586,10 @@ function getHexColor(value) {
|
|
|
2457
2586
|
return getState(value).hex;
|
|
2458
2587
|
}
|
|
2459
2588
|
function getHexValue(value) {
|
|
2460
|
-
return getClampedValue(value, 0,
|
|
2589
|
+
return getClampedValue(value, 0, 255);
|
|
2461
2590
|
}
|
|
2462
2591
|
function getDegrees(value) {
|
|
2463
|
-
return getClampedValue(value, 0,
|
|
2592
|
+
return getClampedValue(value, 0, 360);
|
|
2464
2593
|
}
|
|
2465
2594
|
/**
|
|
2466
2595
|
* Get the HSLA color from any kind of value
|
|
@@ -2483,7 +2612,7 @@ function getHslColor(value) {
|
|
|
2483
2612
|
return getState(value).hsl;
|
|
2484
2613
|
}
|
|
2485
2614
|
function getPercentage(value) {
|
|
2486
|
-
return getClampedValue(value, 0,
|
|
2615
|
+
return getClampedValue(value, 0, 100);
|
|
2487
2616
|
}
|
|
2488
2617
|
/**
|
|
2489
2618
|
* Get the RGBA color from any kind of value
|
|
@@ -2520,9 +2649,9 @@ function convertRgbToHex(rgb, alpha) {
|
|
|
2520
2649
|
}
|
|
2521
2650
|
function convertRgbToHsla(value) {
|
|
2522
2651
|
const rgb = isRgbLike(value) ? getRgbValue(value) : { ...DEFAULT_RGB };
|
|
2523
|
-
const blue = rgb.blue /
|
|
2524
|
-
const green = rgb.green /
|
|
2525
|
-
const red = rgb.red /
|
|
2652
|
+
const blue = rgb.blue / 255;
|
|
2653
|
+
const green = rgb.green / 255;
|
|
2654
|
+
const red = rgb.red / 255;
|
|
2526
2655
|
const maxHex = Math.max(blue, green, red);
|
|
2527
2656
|
const minHex = Math.min(blue, green, red);
|
|
2528
2657
|
const delta = maxHex - minHex;
|
|
@@ -2545,10 +2674,10 @@ function convertRgbToHsla(value) {
|
|
|
2545
2674
|
hue *= 60;
|
|
2546
2675
|
}
|
|
2547
2676
|
return {
|
|
2548
|
-
alpha: getAlphaValue(value.alpha ??
|
|
2677
|
+
alpha: getAlphaValue(value.alpha ?? 1),
|
|
2549
2678
|
hue: +hue.toFixed(2),
|
|
2550
|
-
lightness: +(lightness *
|
|
2551
|
-
saturation: +(saturation *
|
|
2679
|
+
lightness: +(lightness * 100).toFixed(2),
|
|
2680
|
+
saturation: +(saturation * 100).toFixed(2)
|
|
2552
2681
|
};
|
|
2553
2682
|
}
|
|
2554
2683
|
function getRgbValue(value) {
|
|
@@ -2599,7 +2728,7 @@ function convertHexToRgba(value) {
|
|
|
2599
2728
|
const { length } = pairs;
|
|
2600
2729
|
for (let index = 1; index < length; index += 1) values.push(Number.parseInt(pairs[index], 16));
|
|
2601
2730
|
return {
|
|
2602
|
-
alpha: values[3] /
|
|
2731
|
+
alpha: values[3] / 255,
|
|
2603
2732
|
blue: values[2],
|
|
2604
2733
|
green: values[1],
|
|
2605
2734
|
red: values[0]
|
|
@@ -2615,8 +2744,8 @@ function getNormalizedHex(value, alpha) {
|
|
|
2615
2744
|
const includeAlpha = alpha ?? false;
|
|
2616
2745
|
if (!isHexColor(value)) return `${HEX_BLACK}${includeAlpha ? ALPHA_FULL_HEX_LONG : ""}`;
|
|
2617
2746
|
const normalized = value.replace(EXPRESSION_PREFIX, "");
|
|
2618
|
-
if (normalized.length <
|
|
2619
|
-
return `${normalized.slice(0,
|
|
2747
|
+
if (normalized.length < 6) return join(`${normalized.slice(0, 3)}${includeAlpha ? normalized[3] ?? "f" : ""}`.split("").map((character) => character.repeat(2)));
|
|
2748
|
+
return `${normalized.slice(0, 6)}${includeAlpha ? normalized.slice(6) || ALPHA_FULL_HEX_LONG : ""}`;
|
|
2620
2749
|
}
|
|
2621
2750
|
function hexToHsl(value) {
|
|
2622
2751
|
const { hue, lightness, saturation } = hexToHsla(value);
|
|
@@ -2652,11 +2781,11 @@ function hexToRgba(value) {
|
|
|
2652
2781
|
}
|
|
2653
2782
|
function convertHslToRgba(value) {
|
|
2654
2783
|
const hsl = isHslLike(value) ? getHslValue(value) : { ...DEFAULT_HSL };
|
|
2655
|
-
const hue = hsl.hue %
|
|
2656
|
-
const saturation = hsl.saturation /
|
|
2657
|
-
const lightness = hsl.lightness /
|
|
2784
|
+
const hue = hsl.hue % 360;
|
|
2785
|
+
const saturation = hsl.saturation / 100;
|
|
2786
|
+
const lightness = hsl.lightness / 100;
|
|
2658
2787
|
return {
|
|
2659
|
-
alpha: getAlphaValue(value.alpha ??
|
|
2788
|
+
alpha: getAlphaValue(value.alpha ?? 1),
|
|
2660
2789
|
blue: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 4))),
|
|
2661
2790
|
green: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 8))),
|
|
2662
2791
|
red: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 0)))
|
|
@@ -2664,7 +2793,7 @@ function convertHslToRgba(value) {
|
|
|
2664
2793
|
}
|
|
2665
2794
|
function getHexyValue(hue, lightness, saturation, value) {
|
|
2666
2795
|
const part = (value + hue / 30) % 12;
|
|
2667
|
-
return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) *
|
|
2796
|
+
return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * 255;
|
|
2668
2797
|
}
|
|
2669
2798
|
function getHslValue(value) {
|
|
2670
2799
|
return {
|
|
@@ -2704,12 +2833,12 @@ function hslToRgba(hsl) {
|
|
|
2704
2833
|
function getState(value) {
|
|
2705
2834
|
if (typeof value === "string") {
|
|
2706
2835
|
const normalized = getNormalizedHex(value, true);
|
|
2707
|
-
const hex = normalized.slice(0,
|
|
2836
|
+
const hex = normalized.slice(0, 6);
|
|
2708
2837
|
const rgb = hexToRgb(hex);
|
|
2709
2838
|
return {
|
|
2710
2839
|
hex,
|
|
2711
2840
|
rgb,
|
|
2712
|
-
alpha: getAlpha(normalized.slice(
|
|
2841
|
+
alpha: getAlpha(normalized.slice(6)),
|
|
2713
2842
|
hsl: rgbToHsl(rgb)
|
|
2714
2843
|
};
|
|
2715
2844
|
}
|
|
@@ -2735,7 +2864,7 @@ function getState(value) {
|
|
|
2735
2864
|
return state;
|
|
2736
2865
|
}
|
|
2737
2866
|
}
|
|
2738
|
-
state.alpha ??= getAlpha(
|
|
2867
|
+
state.alpha ??= getAlpha(1);
|
|
2739
2868
|
state.hex ??= String(HEX_BLACK);
|
|
2740
2869
|
state.hsl ??= { ...DEFAULT_HSL };
|
|
2741
2870
|
state.rgb ??= { ...DEFAULT_RGB };
|
|
@@ -2744,12 +2873,12 @@ function getState(value) {
|
|
|
2744
2873
|
function setHexColor(state, value, alpha) {
|
|
2745
2874
|
if (!isHexColor(value) || !alpha && value === state.hex) return;
|
|
2746
2875
|
const normalized = getNormalizedHex(value, true);
|
|
2747
|
-
const hex = normalized.slice(0,
|
|
2876
|
+
const hex = normalized.slice(0, 6);
|
|
2748
2877
|
const rgb = hexToRgb(hex);
|
|
2749
2878
|
state.hex = hex;
|
|
2750
2879
|
state.hsl = rgbToHsl(rgb);
|
|
2751
2880
|
state.rgb = rgb;
|
|
2752
|
-
if (alpha) state.alpha = getAlpha(normalized.slice(
|
|
2881
|
+
if (alpha) state.alpha = getAlpha(normalized.slice(6));
|
|
2753
2882
|
}
|
|
2754
2883
|
function setHSLColor(state, value, alpha) {
|
|
2755
2884
|
if (!isHslLike(value)) return;
|
|
@@ -3280,11 +3409,11 @@ function cancelable(executor) {
|
|
|
3280
3409
|
function handleResult(status, parameters) {
|
|
3281
3410
|
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3282
3411
|
if (signal?.aborted ?? false) return;
|
|
3283
|
-
if (!complete && status ===
|
|
3412
|
+
if (!complete && status === "rejected") {
|
|
3284
3413
|
settlePromise(abort, handlers.reject, value, signal);
|
|
3285
3414
|
return;
|
|
3286
3415
|
}
|
|
3287
|
-
data.result[index] = !complete ? value : status ===
|
|
3416
|
+
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
3288
3417
|
status,
|
|
3289
3418
|
value
|
|
3290
3419
|
} : {
|
|
@@ -3874,4 +4003,4 @@ var SizedSet = class extends Set {
|
|
|
3874
4003
|
}
|
|
3875
4004
|
}
|
|
3876
4005
|
};
|
|
3877
|
-
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
|
4006
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, drop, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, slice, smush, snakeCase, sort, splice, startsWith, sum, take, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|