nhb-toolbox 4.10.71 → 4.10.73
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/esm/array/Finder.js +1 -5
- package/dist/esm/array/basics.js +7 -15
- package/dist/esm/array/sort.js +13 -16
- package/dist/esm/array/transform.js +14 -23
- package/dist/esm/array/types.js +1 -2
- package/dist/esm/array/utils.js +1 -4
- package/dist/esm/colors/Color.js +47 -51
- package/dist/esm/colors/constants.js +3 -6
- package/dist/esm/colors/convert.js +57 -74
- package/dist/esm/colors/css-colors.js +1 -4
- package/dist/esm/colors/helpers.js +14 -31
- package/dist/esm/colors/initials.js +13 -16
- package/dist/esm/colors/random.js +7 -12
- package/dist/esm/colors/types.js +1 -2
- package/dist/esm/colors/utils.js +5 -10
- package/dist/esm/date/Chronos.js +53 -57
- package/dist/esm/date/chronos-fn.js +6 -9
- package/dist/esm/date/constants.js +26 -29
- package/dist/esm/date/greet.js +12 -15
- package/dist/esm/date/guards.js +9 -15
- package/dist/esm/date/types.js +1 -2
- package/dist/esm/date/utils.js +6 -14
- package/dist/esm/dom/query.js +10 -17
- package/dist/esm/dom/storage.js +6 -15
- package/dist/esm/dom/utils.js +3 -8
- package/dist/esm/form/convert.js +27 -31
- package/dist/esm/form/guards.js +8 -18
- package/dist/esm/form/transform.js +6 -10
- package/dist/esm/form/types.js +1 -2
- package/dist/esm/guards/non-primitives.js +19 -39
- package/dist/esm/guards/primitives.js +14 -30
- package/dist/esm/guards/specials.js +23 -37
- package/dist/esm/index.js +45 -358
- package/dist/esm/number/Currency.js +3 -7
- package/dist/esm/number/Unit.js +4 -8
- package/dist/esm/number/basics.js +15 -27
- package/dist/esm/number/constants.js +10 -13
- package/dist/esm/number/convert.js +7 -12
- package/dist/esm/number/fibonacci.js +4 -10
- package/dist/esm/number/guards.js +6 -15
- package/dist/esm/number/helpers.js +11 -18
- package/dist/esm/number/percent.js +11 -14
- package/dist/esm/number/prime.js +3 -8
- package/dist/esm/number/range.js +13 -16
- package/dist/esm/number/types.js +1 -2
- package/dist/esm/number/utilities.js +7 -15
- package/dist/esm/object/basics.js +2 -7
- package/dist/esm/object/convert.js +4 -10
- package/dist/esm/object/objectify.js +27 -38
- package/dist/esm/object/sanitize.js +21 -25
- package/dist/esm/object/types.js +1 -2
- package/dist/esm/string/anagram.js +1 -4
- package/dist/esm/string/basics.js +4 -11
- package/dist/esm/string/constants.js +1 -4
- package/dist/esm/string/convert.js +16 -28
- package/dist/esm/string/guards.js +8 -17
- package/dist/esm/string/types.js +1 -2
- package/dist/esm/string/utilities.js +3 -9
- package/dist/esm/types/index.js +1 -2
- package/dist/esm/utils/Paginator.js +1 -5
- package/dist/esm/utils/index.js +25 -39
- package/dist/esm/utils/types.js +1 -2
- package/package.json +1 -1
package/dist/esm/array/Finder.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Finder = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* The `Finder` class performs optimized searching on arrays.
|
|
6
3
|
* It supports binary search, fuzzy search, and smart caching with TTL.
|
|
7
4
|
*/
|
|
8
|
-
class Finder {
|
|
5
|
+
export class Finder {
|
|
9
6
|
static #DEFAULT_TTL = 1000 * 60 * 5;
|
|
10
7
|
#cachedResult = new Map();
|
|
11
8
|
#sortedCache = new Map();
|
|
@@ -286,4 +283,3 @@ class Finder {
|
|
|
286
283
|
};
|
|
287
284
|
}
|
|
288
285
|
}
|
|
289
|
-
exports.Finder = Finder;
|
package/dist/esm/array/basics.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLastArrayElement = exports.shuffleArray = exports.isInvalidOrEmptyArray = exports.filterArrayOfObjects = exports.flattenArray = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* * Flattens a nested array recursively or wraps any non-array data type in an array.
|
|
6
3
|
*
|
|
7
4
|
* @param input - The input value, which can be a nested array or a non-array value.
|
|
8
5
|
* @returns A fully flattened array of type `Flatten<T>`. If the input is not an array, it wraps it in a single-element array.
|
|
9
6
|
*/
|
|
10
|
-
const flattenArray = (input) => {
|
|
7
|
+
export const flattenArray = (input) => {
|
|
11
8
|
if (!Array.isArray(input))
|
|
12
9
|
return [input];
|
|
13
10
|
return input.reduce((acc, item) => {
|
|
14
11
|
// If item is an array, recursively flatten it; otherwise, add it directly.
|
|
15
|
-
return acc.concat(Array.isArray(item) ?
|
|
12
|
+
return acc.concat(Array.isArray(item) ? flattenArray(item) : [item]);
|
|
16
13
|
}, []);
|
|
17
14
|
};
|
|
18
|
-
exports.flattenArray = flattenArray;
|
|
19
15
|
/**
|
|
20
16
|
* @deprecated _Please, use `findAll` instance method from `Finder` class for **more advanced filtering and searching.**_
|
|
21
17
|
*
|
|
@@ -26,7 +22,7 @@ exports.flattenArray = flattenArray;
|
|
|
26
22
|
* @returns The filtered array of objects.
|
|
27
23
|
* @throws `Error` If the input is not a valid array.
|
|
28
24
|
*/
|
|
29
|
-
const filterArrayOfObjects = (array, conditions) => {
|
|
25
|
+
export const filterArrayOfObjects = (array, conditions) => {
|
|
30
26
|
if (!Array.isArray(array)) {
|
|
31
27
|
throw new Error('The provided input is not a valid array!');
|
|
32
28
|
}
|
|
@@ -37,14 +33,13 @@ const filterArrayOfObjects = (array, conditions) => {
|
|
|
37
33
|
return true;
|
|
38
34
|
}));
|
|
39
35
|
};
|
|
40
|
-
exports.filterArrayOfObjects = filterArrayOfObjects;
|
|
41
36
|
/**
|
|
42
37
|
* * Checks if a value is an empty array or an array with only empty values.
|
|
43
38
|
*
|
|
44
39
|
* @param value - The value to check.
|
|
45
40
|
* @returns `true` if the value is not an array, an empty array, or an array containing only `null`, `undefined`, empty objects, or empty arrays.
|
|
46
41
|
*/
|
|
47
|
-
const isInvalidOrEmptyArray = (value) => {
|
|
42
|
+
export const isInvalidOrEmptyArray = (value) => {
|
|
48
43
|
if (!Array.isArray(value))
|
|
49
44
|
return true;
|
|
50
45
|
if (value?.length === 0)
|
|
@@ -53,15 +48,14 @@ const isInvalidOrEmptyArray = (value) => {
|
|
|
53
48
|
(Array.isArray(item) && item?.length === 0) ||
|
|
54
49
|
(typeof item === 'object' && Object.keys(item || {})?.length === 0));
|
|
55
50
|
};
|
|
56
|
-
exports.isInvalidOrEmptyArray = isInvalidOrEmptyArray;
|
|
57
51
|
/**
|
|
58
52
|
* * Shuffle the elements of an array.
|
|
59
53
|
*
|
|
60
54
|
* @param array Array to shuffle.
|
|
61
55
|
* @returns Shuffled array.
|
|
62
56
|
*/
|
|
63
|
-
const shuffleArray = (array) => {
|
|
64
|
-
if (
|
|
57
|
+
export const shuffleArray = (array) => {
|
|
58
|
+
if (isInvalidOrEmptyArray(array))
|
|
65
59
|
return array;
|
|
66
60
|
const shuffled = structuredClone(array);
|
|
67
61
|
for (let i = shuffled?.length - 1; i > 0; i--) {
|
|
@@ -70,14 +64,12 @@ const shuffleArray = (array) => {
|
|
|
70
64
|
}
|
|
71
65
|
return shuffled;
|
|
72
66
|
};
|
|
73
|
-
exports.shuffleArray = shuffleArray;
|
|
74
67
|
/**
|
|
75
68
|
* * Get the last element of an array.
|
|
76
69
|
*
|
|
77
70
|
* @param array Array to get the last element from.
|
|
78
71
|
* @returns The last element or `undefined` if the array is empty.
|
|
79
72
|
*/
|
|
80
|
-
const getLastArrayElement = (array) => {
|
|
73
|
+
export const getLastArrayElement = (array) => {
|
|
81
74
|
return array?.length > 0 ? array[array?.length - 1] : undefined;
|
|
82
75
|
};
|
|
83
|
-
exports.getLastArrayElement = getLastArrayElement;
|
package/dist/esm/array/sort.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const non_primitives_1 = require("../guards/non-primitives");
|
|
5
|
-
const primitives_1 = require("../guards/primitives");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { isArrayOfType, isObject, isValidArray, } from '../guards/non-primitives.js';
|
|
2
|
+
import { isBoolean, isNumber, isString } from '../guards/primitives.js';
|
|
3
|
+
import { naturalSort } from './utils.js';
|
|
7
4
|
/**
|
|
8
5
|
* * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
|
|
9
6
|
*
|
|
@@ -16,27 +13,27 @@ const utils_1 = require("./utils");
|
|
|
16
13
|
* @param options - Sorting options for objects.
|
|
17
14
|
* @returns The sorted array.
|
|
18
15
|
*/
|
|
19
|
-
function sortAnArray(array, options) {
|
|
20
|
-
if (!
|
|
16
|
+
export function sortAnArray(array, options) {
|
|
17
|
+
if (!isValidArray(array))
|
|
21
18
|
return array;
|
|
22
19
|
// Check if the array contains strings
|
|
23
|
-
if (
|
|
20
|
+
if (isArrayOfType(array, isString)) {
|
|
24
21
|
return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
|
|
25
|
-
|
|
26
|
-
:
|
|
22
|
+
naturalSort(b, a)
|
|
23
|
+
: naturalSort(a, b));
|
|
27
24
|
}
|
|
28
25
|
// Check if the array contains numbers
|
|
29
|
-
if (
|
|
26
|
+
if (isArrayOfType(array, isNumber)) {
|
|
30
27
|
return [...array].sort((a, b) => options?.sortOrder === 'desc' ? b - a : a - b);
|
|
31
28
|
}
|
|
32
29
|
// Check if the array contains booleans
|
|
33
|
-
if (
|
|
30
|
+
if (isArrayOfType(array, isBoolean)) {
|
|
34
31
|
return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
|
|
35
32
|
Number(b) - Number(a)
|
|
36
33
|
: Number(a) - Number(b));
|
|
37
34
|
}
|
|
38
35
|
// Handle array of objects
|
|
39
|
-
if (
|
|
36
|
+
if (isArrayOfType(array, isObject) && options && 'sortByField' in options) {
|
|
40
37
|
return [...array].sort((a, b) => {
|
|
41
38
|
const _getKeyValue = (obj, path) => {
|
|
42
39
|
return path
|
|
@@ -50,8 +47,8 @@ function sortAnArray(array, options) {
|
|
|
50
47
|
}
|
|
51
48
|
if (typeof keyA === 'string' && typeof keyB === 'string') {
|
|
52
49
|
return options?.sortOrder === 'desc' ?
|
|
53
|
-
|
|
54
|
-
:
|
|
50
|
+
naturalSort(keyB, keyA)
|
|
51
|
+
: naturalSort(keyA, keyB);
|
|
55
52
|
}
|
|
56
53
|
if (typeof keyA === 'number' && typeof keyB === 'number') {
|
|
57
54
|
return options?.sortOrder === 'desc' ?
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.createOptionsArray = createOptionsArray;
|
|
4
|
-
exports.removeDuplicatesFromArray = removeDuplicatesFromArray;
|
|
5
|
-
exports.getDuplicates = getDuplicates;
|
|
6
|
-
exports.findMissingElements = findMissingElements;
|
|
7
|
-
exports.splitArray = splitArray;
|
|
8
|
-
exports.rotateArray = rotateArray;
|
|
9
|
-
exports.moveArrayElement = moveArrayElement;
|
|
10
|
-
const primitives_1 = require("../guards/primitives");
|
|
11
|
-
const utils_1 = require("../utils");
|
|
1
|
+
import { isNumber } from '../guards/primitives.js';
|
|
2
|
+
import { isDeepEqual } from '../utils.js';
|
|
12
3
|
/**
|
|
13
4
|
* * Converts an array of objects into a formatted array of options.
|
|
14
5
|
*
|
|
@@ -16,11 +7,11 @@ const utils_1 = require("../utils");
|
|
|
16
7
|
* @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
|
|
17
8
|
* @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
|
|
18
9
|
*/
|
|
19
|
-
function createOptionsArray(data, config) {
|
|
10
|
+
export function createOptionsArray(data, config) {
|
|
20
11
|
const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', retainNumberValue = false, } = config || {};
|
|
21
12
|
if (data && data?.length) {
|
|
22
13
|
return data?.map((datum) => {
|
|
23
|
-
const firstValue = retainNumberValue &&
|
|
14
|
+
const firstValue = retainNumberValue && isNumber(datum[firstFieldKey]) ?
|
|
24
15
|
datum[firstFieldKey]
|
|
25
16
|
: String(datum[firstFieldKey] ?? '');
|
|
26
17
|
return {
|
|
@@ -39,8 +30,8 @@ function createOptionsArray(data, config) {
|
|
|
39
30
|
* @param array - The array from which duplicates need to be removed.
|
|
40
31
|
* @returns A new array with duplicates removed.
|
|
41
32
|
*/
|
|
42
|
-
function removeDuplicatesFromArray(array) {
|
|
43
|
-
return array?.filter((item, index, self) => index === self?.findIndex((el) =>
|
|
33
|
+
export function removeDuplicatesFromArray(array) {
|
|
34
|
+
return array?.filter((item, index, self) => index === self?.findIndex((el) => isDeepEqual(el, item)));
|
|
44
35
|
}
|
|
45
36
|
/**
|
|
46
37
|
* * Finds duplicate values in an array, runs deep comparison for objects and arrays.
|
|
@@ -48,12 +39,12 @@ function removeDuplicatesFromArray(array) {
|
|
|
48
39
|
* @param array - The array in which to find duplicates.
|
|
49
40
|
* @returns An array containing all duplicate entries (each one only once).
|
|
50
41
|
*/
|
|
51
|
-
function getDuplicates(array) {
|
|
42
|
+
export function getDuplicates(array) {
|
|
52
43
|
const seen = [];
|
|
53
44
|
const duplicates = [];
|
|
54
45
|
for (const item of array) {
|
|
55
|
-
const hasSeen = seen?.find((el) =>
|
|
56
|
-
const hasDuplicate = duplicates?.find((el) =>
|
|
46
|
+
const hasSeen = seen?.find((el) => isDeepEqual(el, item));
|
|
47
|
+
const hasDuplicate = duplicates?.find((el) => isDeepEqual(el, item));
|
|
57
48
|
if (hasSeen && !hasDuplicate) {
|
|
58
49
|
duplicates?.push(item);
|
|
59
50
|
}
|
|
@@ -79,10 +70,10 @@ function getDuplicates(array) {
|
|
|
79
70
|
* - `'from-second'` → values in `array2` missing in `array1`.
|
|
80
71
|
* @returns An array of missing elements based on the comparison direction.
|
|
81
72
|
*/
|
|
82
|
-
function findMissingElements(array1, array2, missingFrom) {
|
|
73
|
+
export function findMissingElements(array1, array2, missingFrom) {
|
|
83
74
|
const source = (missingFrom === 'from-first' ? array1 : array2) ?? [];
|
|
84
75
|
const target = (missingFrom === 'from-first' ? array2 : array1) ?? [];
|
|
85
|
-
return source.filter((s) => !target?.some((t) =>
|
|
76
|
+
return source.filter((s) => !target?.some((t) => isDeepEqual(t, s)));
|
|
86
77
|
}
|
|
87
78
|
/**
|
|
88
79
|
* * Splits an array into chunks of a given size.
|
|
@@ -91,7 +82,7 @@ function findMissingElements(array1, array2, missingFrom) {
|
|
|
91
82
|
* @param chunkSize The size of each chunk.
|
|
92
83
|
* @returns An array of chunked arrays.
|
|
93
84
|
*/
|
|
94
|
-
function splitArray(arr, chunkSize) {
|
|
85
|
+
export function splitArray(arr, chunkSize) {
|
|
95
86
|
const result = [];
|
|
96
87
|
for (let i = 0; i < arr?.length; i += chunkSize) {
|
|
97
88
|
result.push(arr.slice(i, i + chunkSize));
|
|
@@ -105,7 +96,7 @@ function splitArray(arr, chunkSize) {
|
|
|
105
96
|
* @param steps The number of positions to rotate (positive: right, negative: left).
|
|
106
97
|
* @returns The rotated array.
|
|
107
98
|
*/
|
|
108
|
-
function rotateArray(arr, steps) {
|
|
99
|
+
export function rotateArray(arr, steps) {
|
|
109
100
|
const length = arr?.length;
|
|
110
101
|
if (length === 0)
|
|
111
102
|
return arr;
|
|
@@ -120,7 +111,7 @@ function rotateArray(arr, steps) {
|
|
|
120
111
|
* @param toIndex The new index for the element.
|
|
121
112
|
* @returns A new array with the element moved.
|
|
122
113
|
*/
|
|
123
|
-
function moveArrayElement(arr, fromIndex, toIndex) {
|
|
114
|
+
export function moveArrayElement(arr, fromIndex, toIndex) {
|
|
124
115
|
const newArr = [...arr];
|
|
125
116
|
const [item] = newArr.splice(fromIndex, 1);
|
|
126
117
|
newArr.splice(toIndex, 0, item);
|
package/dist/esm/array/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/esm/array/utils.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.naturalSort = naturalSort;
|
|
4
1
|
/**
|
|
5
2
|
* * Compare two strings using natural sorting (e.g., "file2" < "file10").
|
|
6
3
|
* Optionally supports case-insensitive and locale-aware string chunk comparisons.
|
|
@@ -12,7 +9,7 @@ exports.naturalSort = naturalSort;
|
|
|
12
9
|
* @param options.localeAware - If true, uses localeCompare for string chunk comparisons. Defaults to `false`.
|
|
13
10
|
* @returns A negative number if `a` comes before `b`, a positive number if `a` comes after `b`, or 0 if equal.
|
|
14
11
|
*/
|
|
15
|
-
function naturalSort(a, b, options) {
|
|
12
|
+
export function naturalSort(a, b, options) {
|
|
16
13
|
const { caseInsensitive = true, localeAware = false } = options || {};
|
|
17
14
|
/**
|
|
18
15
|
* * Splits a string into an array of number and non-number chunks.
|
package/dist/esm/colors/Color.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const utils_1 = require("./utils");
|
|
9
|
-
const hsl = (0, random_1.generateRandomHSLColor)();
|
|
10
|
-
const { hex, rgb } = (0, convert_1.convertColorCode)(hsl);
|
|
1
|
+
import { convertColorCode } from './convert.js';
|
|
2
|
+
import { CSS_COLORS } from './css-colors.js';
|
|
3
|
+
import { _convertOpacityToHex, _isHSL, _isHSLA, _isRGB, _isRGBA, } from './helpers.js';
|
|
4
|
+
import { generateRandomHSLColor } from './random.js';
|
|
5
|
+
import { extractAlphaColorValues, extractSolidColorValues } from './utils.js';
|
|
6
|
+
const hsl = generateRandomHSLColor();
|
|
7
|
+
const { hex, rgb } = convertColorCode(hsl);
|
|
11
8
|
/**
|
|
12
9
|
* * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
|
|
13
10
|
* * It has 13 instance methods to manipulate and play with the color values.
|
|
@@ -20,7 +17,7 @@ const { hex, rgb } = (0, convert_1.convertColorCode)(hsl);
|
|
|
20
17
|
* @property hsl - The color in `HSL` format.
|
|
21
18
|
* @property hsla - The color in `HSLA` format.
|
|
22
19
|
*/
|
|
23
|
-
class Color {
|
|
20
|
+
export class Color {
|
|
24
21
|
hex;
|
|
25
22
|
hex8;
|
|
26
23
|
rgb;
|
|
@@ -73,7 +70,7 @@ class Color {
|
|
|
73
70
|
constructor(color) {
|
|
74
71
|
if (color) {
|
|
75
72
|
if (Color.isCSSColor(color)) {
|
|
76
|
-
const newColor = new Color(
|
|
73
|
+
const newColor = new Color(CSS_COLORS[color]);
|
|
77
74
|
this.hex = newColor.hex;
|
|
78
75
|
this.hex8 = newColor.hex8;
|
|
79
76
|
this.rgb = newColor.rgb;
|
|
@@ -85,8 +82,8 @@ class Color {
|
|
|
85
82
|
const colors = this.#convertColorToOthers(color);
|
|
86
83
|
if ('hex8' in colors) {
|
|
87
84
|
// Extract alpha color values (Hex8, RGBA, HSLA)
|
|
88
|
-
const rgbaValues =
|
|
89
|
-
const hslaValues =
|
|
85
|
+
const rgbaValues = extractAlphaColorValues(colors.rgba);
|
|
86
|
+
const hslaValues = extractAlphaColorValues(colors.hsla);
|
|
90
87
|
this.hex = colors.hex8.toUpperCase().slice(0, 7);
|
|
91
88
|
this.hex8 = colors.hex8.toUpperCase();
|
|
92
89
|
this.rgb = `rgb(${rgbaValues[0]}, ${rgbaValues[1]}, ${rgbaValues[2]})`;
|
|
@@ -96,11 +93,11 @@ class Color {
|
|
|
96
93
|
}
|
|
97
94
|
else {
|
|
98
95
|
// Extract solid color values (Hex, RGB, HSL)
|
|
99
|
-
const rgbValues =
|
|
100
|
-
const hslValues =
|
|
96
|
+
const rgbValues = extractSolidColorValues(colors.rgb);
|
|
97
|
+
const hslValues = extractSolidColorValues(colors.hsl);
|
|
101
98
|
this.hex = colors.hex.toUpperCase();
|
|
102
99
|
this.hex8 =
|
|
103
|
-
`${colors.hex.toUpperCase()}${
|
|
100
|
+
`${colors.hex.toUpperCase()}${_convertOpacityToHex(100)}`;
|
|
104
101
|
this.rgb = colors.rgb;
|
|
105
102
|
this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
|
|
106
103
|
this.hsl = colors.hsl;
|
|
@@ -109,12 +106,12 @@ class Color {
|
|
|
109
106
|
}
|
|
110
107
|
}
|
|
111
108
|
else {
|
|
112
|
-
const rgbValues =
|
|
113
|
-
const hslValues =
|
|
109
|
+
const rgbValues = extractSolidColorValues(rgb);
|
|
110
|
+
const hslValues = extractSolidColorValues(hsl);
|
|
114
111
|
// Generate random colors
|
|
115
112
|
this.hex = hex.toUpperCase();
|
|
116
113
|
this.hex8 =
|
|
117
|
-
`${hex.toUpperCase()}${
|
|
114
|
+
`${hex.toUpperCase()}${_convertOpacityToHex(100)}`;
|
|
118
115
|
this.rgb = rgb;
|
|
119
116
|
this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
|
|
120
117
|
this.hsl = hsl;
|
|
@@ -150,10 +147,10 @@ class Color {
|
|
|
150
147
|
*/
|
|
151
148
|
applyOpacity(opacity) {
|
|
152
149
|
const validOpacity = Math.min(100, Math.max(0, opacity));
|
|
153
|
-
const alphaHex =
|
|
150
|
+
const alphaHex = _convertOpacityToHex(opacity);
|
|
154
151
|
const alphaDecimal = validOpacity / 100;
|
|
155
|
-
const rgbValues =
|
|
156
|
-
const hslValues =
|
|
152
|
+
const rgbValues = extractSolidColorValues(this.rgb);
|
|
153
|
+
const hslValues = extractSolidColorValues(this.hsl);
|
|
157
154
|
return Color.#fromParts({
|
|
158
155
|
hex: this.hex.slice(0, 7).toUpperCase(),
|
|
159
156
|
hex8: `${this.hex.slice(0, 7)}${alphaHex}`.toUpperCase(),
|
|
@@ -169,7 +166,7 @@ class Color {
|
|
|
169
166
|
* @returns A new `Color` instance with the modified darkness.
|
|
170
167
|
*/
|
|
171
168
|
applyDarkness(percent) {
|
|
172
|
-
const [h, s, l, a] =
|
|
169
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
173
170
|
const newL = Math.max(0, l - percent);
|
|
174
171
|
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
175
172
|
return new Color(newHSL).applyOpacity((a * 100));
|
|
@@ -180,7 +177,7 @@ class Color {
|
|
|
180
177
|
* @returns A new `Color` instance with the modified lightness.
|
|
181
178
|
*/
|
|
182
179
|
applyBrightness(percent) {
|
|
183
|
-
const [h, s, l, a] =
|
|
180
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
184
181
|
const newL = Math.min(100, l + percent);
|
|
185
182
|
const newHSL = `hsl(${h}, ${s}%, ${newL}%)`;
|
|
186
183
|
return new Color(newHSL).applyOpacity((a * 100));
|
|
@@ -191,7 +188,7 @@ class Color {
|
|
|
191
188
|
* @returns A new `Color` instance with the modified saturation.
|
|
192
189
|
*/
|
|
193
190
|
applyDullness(percent) {
|
|
194
|
-
const [h, s, l, a] =
|
|
191
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
195
192
|
const newS = Math.max(0, s - percent);
|
|
196
193
|
const newHSL = `hsl(${h}, ${newS}%, ${l}%)`;
|
|
197
194
|
return new Color(newHSL).applyOpacity((a * 100));
|
|
@@ -203,7 +200,7 @@ class Color {
|
|
|
203
200
|
* @returns A new `Color` instance shifted toward white.
|
|
204
201
|
*/
|
|
205
202
|
applyWhiteShade(percent) {
|
|
206
|
-
const [h, s, l, a] =
|
|
203
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
207
204
|
// Cap values to avoid overshooting
|
|
208
205
|
const newS = Math.max(0, s - (s * percent) / 100);
|
|
209
206
|
const newL = Math.min(100, l + ((100 - l) * percent) / 100);
|
|
@@ -225,8 +222,8 @@ class Color {
|
|
|
225
222
|
blendWith(other, weight = 0.5) {
|
|
226
223
|
const w = Math.max(0, Math.min(1, weight));
|
|
227
224
|
const converted = Color.isCSSColor(other) ? new Color(other) : new Color(other);
|
|
228
|
-
const [r1, b1, g1, a1] =
|
|
229
|
-
const [r2, b2, g2, a2] =
|
|
225
|
+
const [r1, b1, g1, a1] = extractAlphaColorValues(this.rgba);
|
|
226
|
+
const [r2, b2, g2, a2] = extractAlphaColorValues(converted.rgba);
|
|
230
227
|
const alpha = Math.round((a1 * (1 - w) + a2 * w) * 100) / 100;
|
|
231
228
|
const blendChannel = (c1, c2) => {
|
|
232
229
|
return Math.round((c1 * a1 * (1 - w) + c2 * a2 * w) / alpha);
|
|
@@ -245,7 +242,7 @@ class Color {
|
|
|
245
242
|
contrastRatio(other) {
|
|
246
243
|
const newColor = Color.isCSSColor(other) ? new Color(other) : new Color(other);
|
|
247
244
|
const luminance = (rgb) => {
|
|
248
|
-
const [r, g, b] =
|
|
245
|
+
const [r, g, b] = extractSolidColorValues(rgb).map((v) => {
|
|
249
246
|
const c = v / 255;
|
|
250
247
|
return c <= 0.03928 ?
|
|
251
248
|
c / 12.92
|
|
@@ -265,7 +262,7 @@ class Color {
|
|
|
265
262
|
* @returns A new Color that is the complement of the current color.
|
|
266
263
|
*/
|
|
267
264
|
getComplementaryColor() {
|
|
268
|
-
const [h, s, l, a] =
|
|
265
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
269
266
|
const newHue = (h + 180) % 360;
|
|
270
267
|
const newHSL = `hsl(${newHue}, ${s}%, ${l}%)`;
|
|
271
268
|
return new Color(newHSL).applyOpacity((a * 100));
|
|
@@ -276,7 +273,7 @@ class Color {
|
|
|
276
273
|
* @returns An array of three Color instances: [base, left, right].
|
|
277
274
|
*/
|
|
278
275
|
getAnalogousColors() {
|
|
279
|
-
const [h, s, l, a] =
|
|
276
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
280
277
|
const left = `hsl(${(h + 330) % 360}, ${s}%, ${l}%)`;
|
|
281
278
|
const right = `hsl(${(h + 30) % 360}, ${s}%, ${l}%)`;
|
|
282
279
|
const analogous = [this, new Color(left), new Color(right)];
|
|
@@ -288,7 +285,7 @@ class Color {
|
|
|
288
285
|
* @returns An array of three Color instances: [base, triad1, triad2].
|
|
289
286
|
*/
|
|
290
287
|
getTriadColors() {
|
|
291
|
-
const [h, s, l, a] =
|
|
288
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
292
289
|
const c1 = `hsl(${(h + 120) % 360}, ${s}%, ${l}%)`;
|
|
293
290
|
const c2 = `hsl(${(h + 240) % 360}, ${s}%, ${l}%)`;
|
|
294
291
|
const triad = [this, new Color(c1), new Color(c2)];
|
|
@@ -300,7 +297,7 @@ class Color {
|
|
|
300
297
|
* @returns An array of four Color instances: [base, tetrad1, tetrad2, tetrad3].
|
|
301
298
|
*/
|
|
302
299
|
getTetradColors() {
|
|
303
|
-
const [h, s, l, a] =
|
|
300
|
+
const [h, s, l, a] = extractAlphaColorValues(this.hsla);
|
|
304
301
|
const c1 = `hsl(${(h + 90) % 360}, ${s}%, ${l}%)`;
|
|
305
302
|
const c2 = `hsl(${(h + 180) % 360}, ${s}%, ${l}%)`;
|
|
306
303
|
const c3 = `hsl(${(h + 270) % 360}, ${s}%, ${l}%)`;
|
|
@@ -325,7 +322,7 @@ class Color {
|
|
|
325
322
|
* @returns `true` if light, `false` if dark.
|
|
326
323
|
*/
|
|
327
324
|
isLightColor() {
|
|
328
|
-
const [r, g, b] =
|
|
325
|
+
const [r, g, b] = extractSolidColorValues(this.rgb);
|
|
329
326
|
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
|
|
330
327
|
return brightness > 127.5;
|
|
331
328
|
}
|
|
@@ -354,7 +351,7 @@ class Color {
|
|
|
354
351
|
* @returns `true` if it's a `RGB` color, `false` if not.
|
|
355
352
|
*/
|
|
356
353
|
static isRGB(color) {
|
|
357
|
-
return
|
|
354
|
+
return _isRGB(color);
|
|
358
355
|
}
|
|
359
356
|
/**
|
|
360
357
|
* @static Checks if a color is in `RGBA` format and within valid ranges.
|
|
@@ -363,7 +360,7 @@ class Color {
|
|
|
363
360
|
* @returns `true` if it's a `RGBA` color, `false` if not.
|
|
364
361
|
*/
|
|
365
362
|
static isRGBA(color) {
|
|
366
|
-
return
|
|
363
|
+
return _isRGBA(color);
|
|
367
364
|
}
|
|
368
365
|
/**
|
|
369
366
|
* @static Checks if a color is in `HSL` format and within valid ranges.
|
|
@@ -372,7 +369,7 @@ class Color {
|
|
|
372
369
|
* @returns `true` if it's a `HSL` color, `false` if not.
|
|
373
370
|
*/
|
|
374
371
|
static isHSL(color) {
|
|
375
|
-
return
|
|
372
|
+
return _isHSL(color);
|
|
376
373
|
}
|
|
377
374
|
/**
|
|
378
375
|
* @static Checks if a color is in `HSLA` format and within valid ranges.
|
|
@@ -381,7 +378,7 @@ class Color {
|
|
|
381
378
|
* @returns `true` if it's a `HSLA` color, `false` if not.
|
|
382
379
|
*/
|
|
383
380
|
static isHSLA(color) {
|
|
384
|
-
return
|
|
381
|
+
return _isHSLA(color);
|
|
385
382
|
}
|
|
386
383
|
/**
|
|
387
384
|
* @static Checks if a color is a valid CSS color name.
|
|
@@ -394,11 +391,11 @@ class Color {
|
|
|
394
391
|
static isCSSColor(color) {
|
|
395
392
|
return (!Color.isHex6(color) &&
|
|
396
393
|
!Color.isHex8(color) &&
|
|
397
|
-
!
|
|
398
|
-
!
|
|
399
|
-
!
|
|
400
|
-
!
|
|
401
|
-
color in
|
|
394
|
+
!_isRGB(color) &&
|
|
395
|
+
!_isRGBA(color) &&
|
|
396
|
+
!_isHSL(color) &&
|
|
397
|
+
!_isHSLA(color) &&
|
|
398
|
+
color in CSS_COLORS);
|
|
402
399
|
}
|
|
403
400
|
/**
|
|
404
401
|
* @private Converts the given color to all other formats while preserving the original.
|
|
@@ -408,27 +405,27 @@ class Color {
|
|
|
408
405
|
*/
|
|
409
406
|
#convertColorToOthers(color) {
|
|
410
407
|
if (Color.isHex6(color)) {
|
|
411
|
-
const { rgb, hsl } =
|
|
408
|
+
const { rgb, hsl } = convertColorCode(color);
|
|
412
409
|
return { hex: color, rgb, hsl };
|
|
413
410
|
}
|
|
414
411
|
else if (Color.isRGB(color)) {
|
|
415
|
-
const { hex, hsl } =
|
|
412
|
+
const { hex, hsl } = convertColorCode(color);
|
|
416
413
|
return { hex, rgb: color, hsl };
|
|
417
414
|
}
|
|
418
415
|
else if (Color.isHSL(color)) {
|
|
419
|
-
const { hex, rgb } =
|
|
416
|
+
const { hex, rgb } = convertColorCode(color);
|
|
420
417
|
return { hex, rgb, hsl: color };
|
|
421
418
|
}
|
|
422
419
|
else if (Color.isHex8(color)) {
|
|
423
|
-
const { rgba, hsla } =
|
|
420
|
+
const { rgba, hsla } = convertColorCode(color);
|
|
424
421
|
return { hex8: color, rgba, hsla };
|
|
425
422
|
}
|
|
426
423
|
else if (Color.isRGBA(color)) {
|
|
427
|
-
const { hex8, hsla } =
|
|
424
|
+
const { hex8, hsla } = convertColorCode(color);
|
|
428
425
|
return { hex8, rgba: color, hsla };
|
|
429
426
|
}
|
|
430
427
|
else if (Color.isHSLA(color)) {
|
|
431
|
-
const { hex8, rgba } =
|
|
428
|
+
const { hex8, rgba } = convertColorCode(color);
|
|
432
429
|
return { hex8, rgba, hsla: color };
|
|
433
430
|
}
|
|
434
431
|
throw new Error(`Unrecognized color format: ${color}`);
|
|
@@ -449,4 +446,3 @@ class Color {
|
|
|
449
446
|
return color;
|
|
450
447
|
}
|
|
451
448
|
}
|
|
452
|
-
exports.Color = Color;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PERCENT_VALUES = exports.numberColorPalette = exports.alphabetColorPalette = void 0;
|
|
4
1
|
/** Colors based on the ASCII value of the letter. */
|
|
5
|
-
|
|
2
|
+
export const alphabetColorPalette = [
|
|
6
3
|
'#00094C',
|
|
7
4
|
'#00376E',
|
|
8
5
|
'#005600',
|
|
@@ -31,7 +28,7 @@ exports.alphabetColorPalette = [
|
|
|
31
28
|
'#824809',
|
|
32
29
|
];
|
|
33
30
|
/** Colors based on the index of numbers. */
|
|
34
|
-
|
|
31
|
+
export const numberColorPalette = [
|
|
35
32
|
'#893213',
|
|
36
33
|
'#A44C15',
|
|
37
34
|
'#8B4513',
|
|
@@ -43,7 +40,7 @@ exports.numberColorPalette = [
|
|
|
43
40
|
'#B5680A',
|
|
44
41
|
'#6437B3',
|
|
45
42
|
];
|
|
46
|
-
|
|
43
|
+
export const PERCENT_VALUES = [
|
|
47
44
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
|
48
45
|
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
|
|
49
46
|
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
|