nhb-toolbox 3.9.73 → 4.0.1

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.
Files changed (266) hide show
  1. package/dist/cjs/array/basics.js +82 -0
  2. package/dist/cjs/array/sort.js +70 -0
  3. package/dist/cjs/array/transform.js +79 -0
  4. package/dist/cjs/array/types.js +2 -0
  5. package/dist/cjs/array/utils.js +81 -0
  6. package/dist/cjs/colors/Color.js +241 -0
  7. package/dist/cjs/colors/constants.js +45 -0
  8. package/dist/cjs/colors/convert.js +322 -0
  9. package/dist/cjs/colors/helpers.js +163 -0
  10. package/dist/cjs/colors/initials.js +61 -0
  11. package/dist/cjs/colors/random.js +42 -0
  12. package/dist/cjs/colors/types.js +2 -0
  13. package/dist/cjs/date/Chronos.js +1454 -0
  14. package/dist/cjs/date/chronos-fn.js +110 -0
  15. package/dist/cjs/date/constants.js +412 -0
  16. package/dist/cjs/date/greet.js +47 -0
  17. package/dist/cjs/date/guards.js +46 -0
  18. package/dist/cjs/date/types.js +2 -0
  19. package/dist/cjs/date/utils.js +83 -0
  20. package/dist/cjs/dom/query.js +55 -0
  21. package/dist/cjs/dom/storage.js +69 -0
  22. package/dist/cjs/dom/utils.js +71 -0
  23. package/dist/cjs/form/convert.js +238 -0
  24. package/dist/cjs/form/guards.js +89 -0
  25. package/dist/cjs/form/transform.js +92 -0
  26. package/dist/cjs/form/types.js +2 -0
  27. package/dist/cjs/guards/non-primitives.js +166 -0
  28. package/dist/cjs/guards/primitives.js +111 -0
  29. package/dist/cjs/guards/specials.js +122 -0
  30. package/dist/cjs/index.js +304 -0
  31. package/dist/cjs/number/basics.js +139 -0
  32. package/dist/cjs/number/constants.js +507 -0
  33. package/dist/cjs/number/convert.js +64 -0
  34. package/dist/cjs/number/guards.js +54 -0
  35. package/dist/cjs/number/helpers.js +71 -0
  36. package/dist/cjs/number/prime.js +38 -0
  37. package/dist/cjs/number/range.js +79 -0
  38. package/dist/cjs/number/types.js +2 -0
  39. package/dist/cjs/number/utilities.js +83 -0
  40. package/dist/cjs/object/basics.js +25 -0
  41. package/dist/cjs/object/convert.js +124 -0
  42. package/dist/cjs/object/objectify.js +215 -0
  43. package/dist/cjs/object/sanitize.js +115 -0
  44. package/dist/cjs/object/types.js +2 -0
  45. package/dist/cjs/string/anagram.js +33 -0
  46. package/dist/cjs/string/basics.js +110 -0
  47. package/dist/cjs/string/constants.js +130 -0
  48. package/dist/cjs/string/convert.js +189 -0
  49. package/dist/cjs/string/guards.js +59 -0
  50. package/dist/cjs/string/types.js +2 -0
  51. package/dist/cjs/string/utilities.js +35 -0
  52. package/dist/cjs/types/index.js +2 -0
  53. package/dist/cjs/utils/index.js +119 -0
  54. package/dist/dts/array/basics.d.ts.map +1 -0
  55. package/dist/dts/array/sort.d.ts.map +1 -0
  56. package/dist/dts/array/transform.d.ts.map +1 -0
  57. package/dist/dts/array/types.d.ts.map +1 -0
  58. package/dist/dts/array/utils.d.ts.map +1 -0
  59. package/dist/dts/colors/Color.d.ts.map +1 -0
  60. package/dist/dts/colors/constants.d.ts.map +1 -0
  61. package/dist/dts/colors/convert.d.ts.map +1 -0
  62. package/dist/dts/colors/helpers.d.ts.map +1 -0
  63. package/dist/dts/colors/initials.d.ts.map +1 -0
  64. package/dist/dts/colors/random.d.ts.map +1 -0
  65. package/dist/dts/colors/types.d.ts.map +1 -0
  66. package/dist/dts/date/Chronos.d.ts.map +1 -0
  67. package/dist/dts/date/chronos-fn.d.ts.map +1 -0
  68. package/dist/dts/date/constants.d.ts.map +1 -0
  69. package/dist/dts/date/greet.d.ts.map +1 -0
  70. package/dist/dts/date/guards.d.ts.map +1 -0
  71. package/dist/dts/date/types.d.ts.map +1 -0
  72. package/dist/dts/date/utils.d.ts.map +1 -0
  73. package/dist/dts/dom/query.d.ts.map +1 -0
  74. package/dist/dts/dom/storage.d.ts.map +1 -0
  75. package/dist/dts/dom/utils.d.ts.map +1 -0
  76. package/dist/dts/form/convert.d.ts.map +1 -0
  77. package/dist/dts/form/guards.d.ts.map +1 -0
  78. package/dist/dts/form/transform.d.ts.map +1 -0
  79. package/dist/dts/form/types.d.ts.map +1 -0
  80. package/dist/dts/guards/non-primitives.d.ts.map +1 -0
  81. package/dist/dts/guards/primitives.d.ts.map +1 -0
  82. package/dist/dts/guards/specials.d.ts.map +1 -0
  83. package/dist/dts/index.d.ts.map +1 -0
  84. package/dist/dts/number/basics.d.ts.map +1 -0
  85. package/dist/dts/number/constants.d.ts.map +1 -0
  86. package/dist/dts/number/convert.d.ts.map +1 -0
  87. package/dist/dts/number/guards.d.ts.map +1 -0
  88. package/dist/dts/number/helpers.d.ts.map +1 -0
  89. package/dist/dts/number/prime.d.ts.map +1 -0
  90. package/dist/dts/number/range.d.ts.map +1 -0
  91. package/dist/dts/number/types.d.ts.map +1 -0
  92. package/dist/dts/number/utilities.d.ts.map +1 -0
  93. package/dist/dts/object/basics.d.ts.map +1 -0
  94. package/dist/dts/object/convert.d.ts.map +1 -0
  95. package/dist/dts/object/objectify.d.ts.map +1 -0
  96. package/dist/dts/object/sanitize.d.ts.map +1 -0
  97. package/dist/dts/object/types.d.ts.map +1 -0
  98. package/dist/dts/string/anagram.d.ts.map +1 -0
  99. package/dist/dts/string/basics.d.ts.map +1 -0
  100. package/dist/dts/string/constants.d.ts.map +1 -0
  101. package/dist/dts/string/convert.d.ts.map +1 -0
  102. package/dist/dts/string/guards.d.ts.map +1 -0
  103. package/dist/dts/string/types.d.ts.map +1 -0
  104. package/dist/dts/string/utilities.d.ts.map +1 -0
  105. package/dist/dts/types/index.d.ts.map +1 -0
  106. package/dist/dts/utils/index.d.ts.map +1 -0
  107. package/package.json +18 -3
  108. package/dist/array/basics.d.ts.map +0 -1
  109. package/dist/array/sort.d.ts.map +0 -1
  110. package/dist/array/transform.d.ts.map +0 -1
  111. package/dist/array/types.d.ts.map +0 -1
  112. package/dist/array/utils.d.ts.map +0 -1
  113. package/dist/colors/Color.d.ts.map +0 -1
  114. package/dist/colors/constants.d.ts.map +0 -1
  115. package/dist/colors/convert.d.ts.map +0 -1
  116. package/dist/colors/helpers.d.ts.map +0 -1
  117. package/dist/colors/initials.d.ts.map +0 -1
  118. package/dist/colors/random.d.ts.map +0 -1
  119. package/dist/colors/types.d.ts.map +0 -1
  120. package/dist/date/Chronos.d.ts.map +0 -1
  121. package/dist/date/chronos-fn.d.ts.map +0 -1
  122. package/dist/date/constants.d.ts.map +0 -1
  123. package/dist/date/greet.d.ts.map +0 -1
  124. package/dist/date/guards.d.ts.map +0 -1
  125. package/dist/date/types.d.ts.map +0 -1
  126. package/dist/date/utils.d.ts.map +0 -1
  127. package/dist/dom/query.d.ts.map +0 -1
  128. package/dist/dom/storage.d.ts.map +0 -1
  129. package/dist/dom/utils.d.ts.map +0 -1
  130. package/dist/form/convert.d.ts.map +0 -1
  131. package/dist/form/guards.d.ts.map +0 -1
  132. package/dist/form/transform.d.ts.map +0 -1
  133. package/dist/form/types.d.ts.map +0 -1
  134. package/dist/guards/non-primitives.d.ts.map +0 -1
  135. package/dist/guards/primitives.d.ts.map +0 -1
  136. package/dist/guards/specials.d.ts.map +0 -1
  137. package/dist/index.d.ts.map +0 -1
  138. package/dist/number/basics.d.ts.map +0 -1
  139. package/dist/number/constants.d.ts.map +0 -1
  140. package/dist/number/convert.d.ts.map +0 -1
  141. package/dist/number/guards.d.ts.map +0 -1
  142. package/dist/number/helpers.d.ts.map +0 -1
  143. package/dist/number/prime.d.ts.map +0 -1
  144. package/dist/number/range.d.ts.map +0 -1
  145. package/dist/number/types.d.ts.map +0 -1
  146. package/dist/number/utilities.d.ts.map +0 -1
  147. package/dist/object/basics.d.ts.map +0 -1
  148. package/dist/object/convert.d.ts.map +0 -1
  149. package/dist/object/objectify.d.ts.map +0 -1
  150. package/dist/object/sanitize.d.ts.map +0 -1
  151. package/dist/object/types.d.ts.map +0 -1
  152. package/dist/string/anagram.d.ts.map +0 -1
  153. package/dist/string/basics.d.ts.map +0 -1
  154. package/dist/string/constants.d.ts.map +0 -1
  155. package/dist/string/convert.d.ts.map +0 -1
  156. package/dist/string/guards.d.ts.map +0 -1
  157. package/dist/string/types.d.ts.map +0 -1
  158. package/dist/string/utilities.d.ts.map +0 -1
  159. package/dist/types/index.d.ts.map +0 -1
  160. package/dist/utils/index.d.ts.map +0 -1
  161. /package/dist/{array → dts/array}/basics.d.ts +0 -0
  162. /package/dist/{array → dts/array}/sort.d.ts +0 -0
  163. /package/dist/{array → dts/array}/transform.d.ts +0 -0
  164. /package/dist/{array → dts/array}/types.d.ts +0 -0
  165. /package/dist/{array → dts/array}/utils.d.ts +0 -0
  166. /package/dist/{colors → dts/colors}/Color.d.ts +0 -0
  167. /package/dist/{colors → dts/colors}/constants.d.ts +0 -0
  168. /package/dist/{colors → dts/colors}/convert.d.ts +0 -0
  169. /package/dist/{colors → dts/colors}/helpers.d.ts +0 -0
  170. /package/dist/{colors → dts/colors}/initials.d.ts +0 -0
  171. /package/dist/{colors → dts/colors}/random.d.ts +0 -0
  172. /package/dist/{colors → dts/colors}/types.d.ts +0 -0
  173. /package/dist/{date → dts/date}/Chronos.d.ts +0 -0
  174. /package/dist/{date → dts/date}/chronos-fn.d.ts +0 -0
  175. /package/dist/{date → dts/date}/constants.d.ts +0 -0
  176. /package/dist/{date → dts/date}/greet.d.ts +0 -0
  177. /package/dist/{date → dts/date}/guards.d.ts +0 -0
  178. /package/dist/{date → dts/date}/types.d.ts +0 -0
  179. /package/dist/{date → dts/date}/utils.d.ts +0 -0
  180. /package/dist/{dom → dts/dom}/query.d.ts +0 -0
  181. /package/dist/{dom → dts/dom}/storage.d.ts +0 -0
  182. /package/dist/{dom → dts/dom}/utils.d.ts +0 -0
  183. /package/dist/{form → dts/form}/convert.d.ts +0 -0
  184. /package/dist/{form → dts/form}/guards.d.ts +0 -0
  185. /package/dist/{form → dts/form}/transform.d.ts +0 -0
  186. /package/dist/{form → dts/form}/types.d.ts +0 -0
  187. /package/dist/{guards → dts/guards}/non-primitives.d.ts +0 -0
  188. /package/dist/{guards → dts/guards}/primitives.d.ts +0 -0
  189. /package/dist/{guards → dts/guards}/specials.d.ts +0 -0
  190. /package/dist/{index.d.ts → dts/index.d.ts} +0 -0
  191. /package/dist/{number → dts/number}/basics.d.ts +0 -0
  192. /package/dist/{number → dts/number}/constants.d.ts +0 -0
  193. /package/dist/{number → dts/number}/convert.d.ts +0 -0
  194. /package/dist/{number → dts/number}/guards.d.ts +0 -0
  195. /package/dist/{number → dts/number}/helpers.d.ts +0 -0
  196. /package/dist/{number → dts/number}/prime.d.ts +0 -0
  197. /package/dist/{number → dts/number}/range.d.ts +0 -0
  198. /package/dist/{number → dts/number}/types.d.ts +0 -0
  199. /package/dist/{number → dts/number}/utilities.d.ts +0 -0
  200. /package/dist/{object → dts/object}/basics.d.ts +0 -0
  201. /package/dist/{object → dts/object}/convert.d.ts +0 -0
  202. /package/dist/{object → dts/object}/objectify.d.ts +0 -0
  203. /package/dist/{object → dts/object}/sanitize.d.ts +0 -0
  204. /package/dist/{object → dts/object}/types.d.ts +0 -0
  205. /package/dist/{string → dts/string}/anagram.d.ts +0 -0
  206. /package/dist/{string → dts/string}/basics.d.ts +0 -0
  207. /package/dist/{string → dts/string}/constants.d.ts +0 -0
  208. /package/dist/{string → dts/string}/convert.d.ts +0 -0
  209. /package/dist/{string → dts/string}/guards.d.ts +0 -0
  210. /package/dist/{string → dts/string}/types.d.ts +0 -0
  211. /package/dist/{string → dts/string}/utilities.d.ts +0 -0
  212. /package/dist/{types → dts/types}/index.d.ts +0 -0
  213. /package/dist/{utils → dts/utils}/index.d.ts +0 -0
  214. /package/dist/{array → esm/array}/basics.js +0 -0
  215. /package/dist/{array → esm/array}/sort.js +0 -0
  216. /package/dist/{array → esm/array}/transform.js +0 -0
  217. /package/dist/{array → esm/array}/types.js +0 -0
  218. /package/dist/{array → esm/array}/utils.js +0 -0
  219. /package/dist/{colors → esm/colors}/Color.js +0 -0
  220. /package/dist/{colors → esm/colors}/constants.js +0 -0
  221. /package/dist/{colors → esm/colors}/convert.js +0 -0
  222. /package/dist/{colors → esm/colors}/helpers.js +0 -0
  223. /package/dist/{colors → esm/colors}/initials.js +0 -0
  224. /package/dist/{colors → esm/colors}/random.js +0 -0
  225. /package/dist/{colors → esm/colors}/types.js +0 -0
  226. /package/dist/{date → esm/date}/Chronos.js +0 -0
  227. /package/dist/{date → esm/date}/chronos-fn.js +0 -0
  228. /package/dist/{date → esm/date}/constants.js +0 -0
  229. /package/dist/{date → esm/date}/greet.js +0 -0
  230. /package/dist/{date → esm/date}/guards.js +0 -0
  231. /package/dist/{date → esm/date}/types.js +0 -0
  232. /package/dist/{date → esm/date}/utils.js +0 -0
  233. /package/dist/{dom → esm/dom}/query.js +0 -0
  234. /package/dist/{dom → esm/dom}/storage.js +0 -0
  235. /package/dist/{dom → esm/dom}/utils.js +0 -0
  236. /package/dist/{form → esm/form}/convert.js +0 -0
  237. /package/dist/{form → esm/form}/guards.js +0 -0
  238. /package/dist/{form → esm/form}/transform.js +0 -0
  239. /package/dist/{form → esm/form}/types.js +0 -0
  240. /package/dist/{guards → esm/guards}/non-primitives.js +0 -0
  241. /package/dist/{guards → esm/guards}/primitives.js +0 -0
  242. /package/dist/{guards → esm/guards}/specials.js +0 -0
  243. /package/dist/{index.js → esm/index.js} +0 -0
  244. /package/dist/{number → esm/number}/basics.js +0 -0
  245. /package/dist/{number → esm/number}/constants.js +0 -0
  246. /package/dist/{number → esm/number}/convert.js +0 -0
  247. /package/dist/{number → esm/number}/guards.js +0 -0
  248. /package/dist/{number → esm/number}/helpers.js +0 -0
  249. /package/dist/{number → esm/number}/prime.js +0 -0
  250. /package/dist/{number → esm/number}/range.js +0 -0
  251. /package/dist/{number → esm/number}/types.js +0 -0
  252. /package/dist/{number → esm/number}/utilities.js +0 -0
  253. /package/dist/{object → esm/object}/basics.js +0 -0
  254. /package/dist/{object → esm/object}/convert.js +0 -0
  255. /package/dist/{object → esm/object}/objectify.js +0 -0
  256. /package/dist/{object → esm/object}/sanitize.js +0 -0
  257. /package/dist/{object → esm/object}/types.js +0 -0
  258. /package/dist/{string → esm/string}/anagram.js +0 -0
  259. /package/dist/{string → esm/string}/basics.js +0 -0
  260. /package/dist/{string → esm/string}/constants.js +0 -0
  261. /package/dist/{string → esm/string}/convert.js +0 -0
  262. /package/dist/{string → esm/string}/guards.js +0 -0
  263. /package/dist/{string → esm/string}/types.js +0 -0
  264. /package/dist/{string → esm/string}/utilities.js +0 -0
  265. /package/dist/{types → esm/types}/index.js +0 -0
  266. /package/dist/{utils → esm/utils}/index.js +0 -0
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLastArrayElement = exports.shuffleArray = exports.isInvalidOrEmptyArray = exports.filterArrayOfObjects = exports.flattenArray = void 0;
4
+ /**
5
+ * * Flattens a nested array recursively or wraps any non-array data type in an array.
6
+ *
7
+ * @param input - The input value, which can be a nested array or a non-array value.
8
+ * @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
+ */
10
+ const flattenArray = (input) => {
11
+ if (!Array.isArray(input))
12
+ return [input];
13
+ return input.reduce((acc, item) => {
14
+ // If item is an array, recursively flatten it; otherwise, add it directly.
15
+ return acc.concat(Array.isArray(item) ? (0, exports.flattenArray)(item) : [item]);
16
+ }, []);
17
+ };
18
+ exports.flattenArray = flattenArray;
19
+ /**
20
+ * * Filters an array of objects based on multiple conditions for specified keys.
21
+ *
22
+ * @param array - The array of objects to filter.
23
+ * @param conditions - An object where keys represent the property names and values represent filter conditions.
24
+ * The conditions can be a function `(value: T[K]) => boolean`.
25
+ * @returns The filtered array of objects.
26
+ * @throws `Error` If the input is not a valid array.
27
+ */
28
+ const filterArrayOfObjects = (array, conditions) => {
29
+ if (!Array.isArray(array)) {
30
+ throw new Error('The provided input is not a valid array!');
31
+ }
32
+ return array.filter((item) => Object.entries(conditions).every(([key, conditionFn]) => {
33
+ if (typeof conditionFn === 'function') {
34
+ return conditionFn(item[key]);
35
+ }
36
+ return true;
37
+ }));
38
+ };
39
+ exports.filterArrayOfObjects = filterArrayOfObjects;
40
+ /**
41
+ * * Checks if a value is an empty array or an array with only empty values.
42
+ *
43
+ * @param value - The value to check.
44
+ * @returns `true` if the value is not an array, an empty array, or an array containing only `null`, `undefined`, empty objects, or empty arrays.
45
+ */
46
+ const isInvalidOrEmptyArray = (value) => {
47
+ if (!Array.isArray(value))
48
+ return true;
49
+ if (value.length === 0)
50
+ return true;
51
+ return value.every((item) => item == null ||
52
+ (Array.isArray(item) && item.length === 0) ||
53
+ (typeof item === 'object' && Object.keys(item || {}).length === 0));
54
+ };
55
+ exports.isInvalidOrEmptyArray = isInvalidOrEmptyArray;
56
+ /**
57
+ * * Shuffle the elements of an array.
58
+ *
59
+ * @param array Array to shuffle.
60
+ * @returns Shuffled array.
61
+ */
62
+ const shuffleArray = (array) => {
63
+ if ((0, exports.isInvalidOrEmptyArray)(array))
64
+ return array;
65
+ const shuffled = structuredClone(array);
66
+ for (let i = shuffled.length - 1; i > 0; i--) {
67
+ const j = Math.floor(Math.random() * (i + 1));
68
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
69
+ }
70
+ return shuffled;
71
+ };
72
+ exports.shuffleArray = shuffleArray;
73
+ /**
74
+ * * Get the last element of an array.
75
+ *
76
+ * @param array Array to get the last element from.
77
+ * @returns The last element or `undefined` if the array is empty.
78
+ */
79
+ const getLastArrayElement = (array) => {
80
+ return array?.length > 0 ? array[array?.length - 1] : undefined;
81
+ };
82
+ exports.getLastArrayElement = getLastArrayElement;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortAnArray = sortAnArray;
4
+ const non_primitives_1 = require("../guards/non-primitives");
5
+ const primitives_1 = require("../guards/primitives");
6
+ const utils_1 = require("./utils");
7
+ /**
8
+ * * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
9
+ *
10
+ * - If the array contains strings, it sorts them alphabetically.
11
+ * - If the array contains numbers, it sorts them numerically.
12
+ * - If the array contains booleans, it sorts them by their boolean value.
13
+ * - If the array contains objects, it sorts them by the specified field in the options `sortByField`.
14
+ *
15
+ * @param array - The array to sort.
16
+ * @param options - Sorting options for objects.
17
+ * @returns The sorted array.
18
+ */
19
+ function sortAnArray(array, options) {
20
+ if (!(0, non_primitives_1.isValidArray)(array))
21
+ return array;
22
+ // Check if the array contains strings
23
+ if ((0, non_primitives_1.isArrayOfType)(array, primitives_1.isString)) {
24
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
25
+ (0, utils_1.naturalSort)(b, a)
26
+ : (0, utils_1.naturalSort)(a, b));
27
+ }
28
+ // Check if the array contains numbers
29
+ if ((0, non_primitives_1.isArrayOfType)(array, primitives_1.isNumber)) {
30
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ? b - a : a - b);
31
+ }
32
+ // Check if the array contains booleans
33
+ if ((0, non_primitives_1.isArrayOfType)(array, primitives_1.isBoolean)) {
34
+ return [...array].sort((a, b) => options?.sortOrder === 'desc' ?
35
+ Number(b) - Number(a)
36
+ : Number(a) - Number(b));
37
+ }
38
+ // Handle array of objects
39
+ if ((0, non_primitives_1.isArrayOfType)(array, non_primitives_1.isObject) && options && 'sortByField' in options) {
40
+ return [...array].sort((a, b) => {
41
+ const _getKeyValue = (obj, path) => {
42
+ return path
43
+ .split('.')
44
+ .reduce((acc, key) => acc?.[key], obj);
45
+ };
46
+ const keyA = _getKeyValue(a, options?.sortByField);
47
+ const keyB = _getKeyValue(b, options?.sortByField);
48
+ if (keyA == null || keyB == null) {
49
+ return keyA == null ? 1 : -1;
50
+ }
51
+ if (typeof keyA === 'string' && typeof keyB === 'string') {
52
+ return options?.sortOrder === 'desc' ?
53
+ (0, utils_1.naturalSort)(keyB, keyA)
54
+ : (0, utils_1.naturalSort)(keyA, keyB);
55
+ }
56
+ if (typeof keyA === 'number' && typeof keyB === 'number') {
57
+ return options?.sortOrder === 'desc' ?
58
+ keyB - keyA
59
+ : keyA - keyB;
60
+ }
61
+ if (typeof keyA === 'boolean' && typeof keyB === 'boolean') {
62
+ return options?.sortOrder === 'desc' ?
63
+ Number(keyB) - Number(keyA)
64
+ : Number(keyA) - Number(keyB);
65
+ }
66
+ return 0;
67
+ });
68
+ }
69
+ return array;
70
+ }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOptionsArray = void 0;
4
+ exports.removeDuplicatesFromArray = removeDuplicatesFromArray;
5
+ exports.splitArray = splitArray;
6
+ exports.rotateArray = rotateArray;
7
+ exports.moveArrayElement = moveArrayElement;
8
+ const utils_1 = require("../utils");
9
+ /**
10
+ * * Converts an array of objects into a formatted array of options.
11
+ *
12
+ * @param data - An array of objects to convert into options.
13
+ * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
14
+ * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
15
+ */
16
+ const createOptionsArray = (data, config) => {
17
+ const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
18
+ if (data && data.length) {
19
+ return data.map((datum) => ({
20
+ [firstFieldName]: String(datum[firstFieldKey] ?? ''),
21
+ [secondFieldName]: String(datum[secondFieldKey] ?? ''),
22
+ }));
23
+ }
24
+ else {
25
+ return [];
26
+ }
27
+ };
28
+ exports.createOptionsArray = createOptionsArray;
29
+ /**
30
+ * * Removes duplicate values from an array, supporting deep comparison for objects and arrays.
31
+ *
32
+ * @param array - The array from which duplicates need to be removed.
33
+ * @returns A new array with duplicates removed.
34
+ */
35
+ function removeDuplicatesFromArray(array) {
36
+ return array.filter((item, index, self) => index === self.findIndex((el) => (0, utils_1.isDeepEqual)(el, item)));
37
+ }
38
+ /**
39
+ * * Splits an array into chunks of a given size.
40
+ *
41
+ * @param arr The array to split.
42
+ * @param chunkSize The size of each chunk.
43
+ * @returns An array of chunked arrays.
44
+ */
45
+ function splitArray(arr, chunkSize) {
46
+ const result = [];
47
+ for (let i = 0; i < arr.length; i += chunkSize) {
48
+ result.push(arr.slice(i, i + chunkSize));
49
+ }
50
+ return result;
51
+ }
52
+ /**
53
+ * * Rotates an array left or right by a given number of steps.
54
+ *
55
+ * @param arr The array to rotate.
56
+ * @param steps The number of positions to rotate (positive: right, negative: left).
57
+ * @returns The rotated array.
58
+ */
59
+ function rotateArray(arr, steps) {
60
+ const length = arr.length;
61
+ if (length === 0)
62
+ return arr;
63
+ const offset = ((steps % length) + length) % length;
64
+ return arr.slice(-offset).concat(arr.slice(0, -offset));
65
+ }
66
+ /**
67
+ * * Moves an element within an array from one index to another.
68
+ *
69
+ * @param arr The array to modify.
70
+ * @param fromIndex The index of the element to move.
71
+ * @param toIndex The new index for the element.
72
+ * @returns A new array with the element moved.
73
+ */
74
+ function moveArrayElement(arr, fromIndex, toIndex) {
75
+ const newArr = [...arr];
76
+ const [item] = newArr.splice(fromIndex, 1);
77
+ newArr.splice(toIndex, 0, item);
78
+ return newArr;
79
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.naturalSort = naturalSort;
4
+ /**
5
+ * * Compare two strings using natural sorting (e.g., "file2" < "file10").
6
+ * Optionally supports case-insensitive and locale-aware string chunk comparisons.
7
+ *
8
+ * @param a - The first string to compare.
9
+ * @param b - The second string to compare.
10
+ * @param options - Optional settings to configure comparison behavior.
11
+ * @param options.caseInsensitive - If true, compares string chunks without case sensitivity. Defaults to `true`.
12
+ * @param options.localeAware - If true, uses localeCompare for string chunk comparisons. Defaults to `false`.
13
+ * @returns A negative number if `a` comes before `b`, a positive number if `a` comes after `b`, or 0 if equal.
14
+ */
15
+ function naturalSort(a, b, options) {
16
+ const { caseInsensitive = true, localeAware = false } = options || {};
17
+ /**
18
+ * * Splits a string into an array of number and non-number chunks.
19
+ * @param str - The string to split.
20
+ * @returns An array of string and number parts.
21
+ */
22
+ const _createChunks = (str) => {
23
+ const chunks = [];
24
+ let current = '';
25
+ let isNumeric = false;
26
+ for (const char of str) {
27
+ const charIsNum = !Number.isNaN(Number(char));
28
+ if (current.length === 0) {
29
+ current = char;
30
+ isNumeric = charIsNum;
31
+ continue;
32
+ }
33
+ if (charIsNum === isNumeric) {
34
+ current += char;
35
+ }
36
+ else {
37
+ chunks.push(isNumeric ? Number(current) : current);
38
+ current = char;
39
+ isNumeric = charIsNum;
40
+ }
41
+ }
42
+ if (current.length > 0) {
43
+ chunks.push(isNumeric ? Number(current) : current);
44
+ }
45
+ return chunks;
46
+ };
47
+ const aChunks = _createChunks(a);
48
+ const bChunks = _createChunks(b);
49
+ for (let i = 0; i < Math.min(aChunks.length, bChunks.length); i++) {
50
+ let aChunk = aChunks[i];
51
+ let bChunk = bChunks[i];
52
+ // Normalize string chunks if case-insensitive
53
+ if (caseInsensitive &&
54
+ typeof aChunk === 'string' &&
55
+ typeof bChunk === 'string') {
56
+ aChunk = aChunk.toLowerCase();
57
+ bChunk = bChunk.toLowerCase();
58
+ }
59
+ // Compare types: number vs string
60
+ if (typeof aChunk !== typeof bChunk) {
61
+ return typeof aChunk === 'string' ? 1 : -1;
62
+ }
63
+ // Compare same-type chunks
64
+ if (aChunk !== bChunk) {
65
+ if (typeof aChunk === 'number' && typeof bChunk === 'number') {
66
+ return aChunk - bChunk;
67
+ }
68
+ if (typeof aChunk === 'string' && typeof bChunk === 'string') {
69
+ if (localeAware) {
70
+ const cmp = aChunk.localeCompare(bChunk, undefined, {
71
+ sensitivity: caseInsensitive ? 'accent' : 'variant',
72
+ });
73
+ if (cmp !== 0)
74
+ return cmp;
75
+ }
76
+ return aChunk < bChunk ? -1 : 1;
77
+ }
78
+ }
79
+ }
80
+ return aChunks.length - bChunks.length;
81
+ }
@@ -0,0 +1,241 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Color = void 0;
4
+ const convert_1 = require("./convert");
5
+ const helpers_1 = require("./helpers");
6
+ const random_1 = require("./random");
7
+ const hsl = (0, random_1.generateRandomHSLColor)();
8
+ const { hex, rgb } = (0, convert_1.convertColorCode)(hsl);
9
+ /**
10
+ * * Class representing a color and its conversions among `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` and `HSLA` formats.
11
+ * * It has 1 instance method `applyOpacity()` to apply opacity to `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` color.
12
+ * * It has 6 static methods that can be used to check if a color is in `Hex`, `Hex8` `RGB`, `RGBA`, `HSL` or `HSLA` format.
13
+ *
14
+ * @property hex - The color in `Hex` format.
15
+ * @property hex8 - The color in `Hex8` format.
16
+ * @property rgb - The color in `RGB` format.
17
+ * @property rgba - The color in `RGBA` format.
18
+ * @property hsl - The color in `HSL` format.
19
+ * @property hsla - The color in `HSLA` format.
20
+ */
21
+ class Color {
22
+ hex;
23
+ hex8;
24
+ rgb;
25
+ rgba;
26
+ hsl;
27
+ hsla;
28
+ /**
29
+ * * Creates a new `Color` instance and automatically converts the input color to all other supported formats: `Hex`, `Hex8`, `RGB`, `RGBA`, `HSL`, and `HSLA`.
30
+ *
31
+ * @description
32
+ * The `Color` class allows seamless transformation between six common color representations:
33
+ * - `Hex` (e.g., `#ff5733`)
34
+ * - `Hex8` (Hex with opacity, e.g., `#ff573380`)
35
+ * - `RGB` (e.g., `rgb(255, 87, 51)`)
36
+ * - `RGBA` (e.g., `rgba(255, 87, 51, 1)`)
37
+ * - `HSL` (e.g., `hsl(14, 100%, 60%)`)
38
+ * - `HSLA` (e.g., `hsla(14, 100%, 60%, 1)`)
39
+ *
40
+ * You can create a color from any of these formats, and the class will populate the rest.
41
+ * If no color is passed, a random color will be generated.
42
+ *
43
+ * Additionally:
44
+ * - Use `.applyOpacity(opacity)` to modify or add opacity to the color.
45
+ * - Use static methods like `Color.isHex6(color)` to validate color strings.
46
+ *
47
+ * @param toConvert - An optional input color string in any supported format (`Hex`, `Hex8`, `RGB`, `RGBA`, `HSL`, or `HSLA`) to convert in all other (includes the current format) formats.
48
+ *
49
+ * @example
50
+ * // Convert an existing Hex color to all other formats
51
+ * const color = new Color("#ff5733");
52
+ * console.log(color.rgb); // 'rgb(255, 87, 51)'
53
+ * console.log(color.hsl); // 'hsl(14, 100%, 60%)'
54
+ * console.log(color.rgba); // 'rgba(255, 87, 51, 1)'
55
+ * console.log(color.hsla); // 'hsla(14, 100%, 60%, 1)'
56
+ * console.log(color.hex8); // '#FF5733FF'
57
+ *
58
+ * @example
59
+ * // Handle a color with alpha
60
+ * const alphaColor = new Color("rgba(255, 0, 0, 0.5)");
61
+ * console.log(alphaColor.hex8); // '#FF000080'
62
+ * console.log(alphaColor.hsla); // 'hsla(0, 100%, 50%, 0.5)'
63
+ *
64
+ * @example
65
+ * // Generate a random color
66
+ * const randomColor = new Color();
67
+ * console.log(randomColor.hex, randomColor.rgb, randomColor.hsl);
68
+ *
69
+ * @returns Instance of `Color`.
70
+ */
71
+ constructor(toConvert) {
72
+ if (toConvert) {
73
+ const colors = this.#convertColorToOthers(toConvert);
74
+ if ('hex8' in colors) {
75
+ // Extract alpha color values (Hex8, RGBA, HSLA)
76
+ const rgbaValues = (0, helpers_1._extractAlphaColorValues)(colors.rgba);
77
+ const hslaValues = (0, helpers_1._extractAlphaColorValues)(colors.hsla);
78
+ this.hex = colors.hex8.toUpperCase().slice(0, 7);
79
+ this.hex8 = colors.hex8.toUpperCase();
80
+ this.rgb = `rgb(${rgbaValues[0]}, ${rgbaValues[1]}, ${rgbaValues[2]})`;
81
+ this.rgba = colors.rgba;
82
+ this.hsl = `hsl(${hslaValues[0]}, ${hslaValues[1]}%, ${hslaValues[2]}%)`;
83
+ this.hsla = colors.hsla;
84
+ }
85
+ else {
86
+ // Extract solid color values (Hex, RGB, HSL)
87
+ const rgbValues = (0, helpers_1._extractSolidColorValues)(colors.rgb);
88
+ const hslValues = (0, helpers_1._extractSolidColorValues)(colors.hsl);
89
+ this.hex = colors.hex.toUpperCase();
90
+ this.hex8 =
91
+ `${colors.hex.toUpperCase()}${(0, helpers_1._convertOpacityToHex)(100)}`;
92
+ this.rgb = colors.rgb;
93
+ this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
94
+ this.hsl = colors.hsl;
95
+ this.hsla = `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, 1)`;
96
+ }
97
+ }
98
+ else {
99
+ const rgbValues = (0, helpers_1._extractSolidColorValues)(rgb);
100
+ const hslValues = (0, helpers_1._extractSolidColorValues)(hsl);
101
+ // Generate random colors
102
+ this.hex = hex.toUpperCase();
103
+ this.hex8 =
104
+ `${hex.toUpperCase()}${(0, helpers_1._convertOpacityToHex)(100)}`;
105
+ this.rgb = rgb;
106
+ this.rgba = `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, 1)`;
107
+ this.hsl = hsl;
108
+ this.hsla = `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, 1)`;
109
+ }
110
+ }
111
+ /** - Iterates over the color representations (Hex, RGB, HSL). */
112
+ *[Symbol.iterator]() {
113
+ yield this.hex;
114
+ yield this.hex8;
115
+ yield this.rgb;
116
+ yield this.rgba;
117
+ yield this.hsl;
118
+ yield this.hsla;
119
+ }
120
+ /**
121
+ * * Applies or modifies the opacity of a color.
122
+ * - For solid colors (Hex6/RGB/HSL): Adds an alpha channel with the specified opacity
123
+ * - For alpha colors (Hex8/RGBA/HSLA): Updates the existing alpha channel
124
+ *
125
+ * @param opacity - A number between 0-100 representing the opacity percentage
126
+ * @returns An object containing all color formats with the applied opacity
127
+ *
128
+ * @example
129
+ * const color = new Color("#ff0000");
130
+ * const alpha50 = color.applyOpacity(50); // 50% opacity
131
+ * console.log(alpha50.rgba); // rgba(255, 0, 0, 0.5)
132
+ *
133
+ * @example
134
+ * const alphaColor = new Color("#ff000080"); // Color with 50% opacity
135
+ * const alpha75 = alphaColor.applyOpacity(75); // Change to 75% opacity
136
+ * console.log(alpha75.hex8); // #FF0000BF
137
+ */
138
+ applyOpacity(opacity) {
139
+ const validOpacity = Math.min(100, Math.max(0, opacity));
140
+ const alphaHex = (0, helpers_1._convertOpacityToHex)(opacity);
141
+ const alphaDecimal = validOpacity / 100;
142
+ const rgbValues = (0, helpers_1._extractSolidColorValues)(this.rgb);
143
+ const hslValues = (0, helpers_1._extractSolidColorValues)(this.hsl);
144
+ return {
145
+ hex: this.hex.slice(0, 7).toUpperCase(),
146
+ hex8: `${this.hex.slice(0, 7)}${alphaHex}`.toUpperCase(),
147
+ rgb: `rgb(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]})`,
148
+ rgba: `rgba(${rgbValues[0]}, ${rgbValues[1]}, ${rgbValues[2]}, ${alphaDecimal})`,
149
+ hsl: `hsl(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%)`,
150
+ hsla: `hsla(${hslValues[0]}, ${hslValues[1]}%, ${hslValues[2]}%, ${alphaDecimal})`,
151
+ };
152
+ }
153
+ /**
154
+ * @static Checks if a color is in `Hex6` format.
155
+ *
156
+ * @param color Color to check.
157
+ * @returns Boolean: `true` if it's a `Hex6` color, `false` if not.
158
+ */
159
+ static isHex6(color) {
160
+ return /^#[0-9A-Fa-f]{6}$/.test(color);
161
+ }
162
+ /**
163
+ * @static Checks if a color is in `Hex8` format.
164
+ *
165
+ * @param color Color to check.
166
+ * @returns Boolean: `true` if it's a `Hex8` color, `false` if not.
167
+ */
168
+ static isHex8(color) {
169
+ return /^#[0-9A-Fa-f]{8}$/.test(color);
170
+ }
171
+ /**
172
+ * @static Checks if a color is in `RGB` format.
173
+ *
174
+ * @param color Color to check.
175
+ * @returns Boolean: `true` if it's an `RGB` color, `false` if not.
176
+ */
177
+ static isRGB(color) {
178
+ return /^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/.test(color);
179
+ }
180
+ /**
181
+ * @static Checks if a color is in `RGBA` format.
182
+ *
183
+ * @param color Color to check.
184
+ * @returns Boolean: `true` if it's an `RGBA` color, `false` if not.
185
+ */
186
+ static isRGBA(color) {
187
+ return /^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*(0|1|0?\.\d+)\)$/.test(color);
188
+ }
189
+ /**
190
+ * @static Checks if a color is in `HSL` format.
191
+ *
192
+ * @param color Color to check.
193
+ * @returns Boolean: `true` if it's an `HSL` color, `false` if not.
194
+ */
195
+ static isHSL(color) {
196
+ return /^hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%\)$/.test(color);
197
+ }
198
+ /**
199
+ * @static Checks if a color is in `HSLA` format.
200
+ *
201
+ * @param color Color to check.
202
+ * @returns Boolean: `true` if it's an `HSLA` color, `false` if not.
203
+ */
204
+ static isHSLA(color) {
205
+ return /^hsla\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%,\s*(0|1|0?\.\d+)\)$/.test(color);
206
+ }
207
+ /**
208
+ * @private Converts the given color to all other formats while preserving the original.
209
+ *
210
+ * @param color - The color to convert.
211
+ * @returns An object containing Hex, RGB, and HSL representations.
212
+ */
213
+ #convertColorToOthers(color) {
214
+ if (Color.isHex6(color)) {
215
+ const { rgb, hsl } = (0, convert_1.convertColorCode)(color);
216
+ return { hex: color, rgb, hsl };
217
+ }
218
+ else if (Color.isRGB(color)) {
219
+ const { hex, hsl } = (0, convert_1.convertColorCode)(color);
220
+ return { hex, rgb: color, hsl };
221
+ }
222
+ else if (Color.isHSL(color)) {
223
+ const { hex, rgb } = (0, convert_1.convertColorCode)(color);
224
+ return { hex, rgb, hsl: color };
225
+ }
226
+ else if (Color.isHex8(color)) {
227
+ const { rgba, hsla } = (0, convert_1.convertColorCode)(color);
228
+ return { hex8: color, rgba, hsla };
229
+ }
230
+ else if (Color.isRGBA(color)) {
231
+ const { hex8, hsla } = (0, convert_1.convertColorCode)(color);
232
+ return { hex8, rgba: color, hsla };
233
+ }
234
+ else if (Color.isHSLA(color)) {
235
+ const { hex8, rgba } = (0, convert_1.convertColorCode)(color);
236
+ return { hex8, rgba, hsla: color };
237
+ }
238
+ throw new Error(`Unrecognized color format: ${color}`);
239
+ }
240
+ }
241
+ exports.Color = Color;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.numberColorPalette = exports.alphabetColorPalette = void 0;
4
+ /** Colors based on the ASCII value of the letter. */
5
+ exports.alphabetColorPalette = [
6
+ '#00094C',
7
+ '#00376E',
8
+ '#005600',
9
+ '#024647',
10
+ '#423067',
11
+ '#55188E',
12
+ '#00453E',
13
+ '#00516C',
14
+ '#263E0D',
15
+ '#0F6F3F',
16
+ '#730073',
17
+ '#053636',
18
+ '#253654',
19
+ '#4682B4',
20
+ '#3253B6',
21
+ '#43616C',
22
+ '#036C44',
23
+ '#30784F',
24
+ '#601C1C',
25
+ '#690000',
26
+ '#005B00',
27
+ '#BF0E6C',
28
+ '#008080',
29
+ '#475F47',
30
+ '#546F1C',
31
+ '#824809',
32
+ ];
33
+ /** Colors based on the index of numbers. */
34
+ exports.numberColorPalette = [
35
+ '#893213',
36
+ '#A44C15',
37
+ '#8B4513',
38
+ '#8A1D33',
39
+ '#3B543B',
40
+ '#342656',
41
+ '#A43522',
42
+ '#04605F',
43
+ '#B5680A',
44
+ '#6437B3',
45
+ ];