@react-fabric/utilities 1.0.0-beta.9 → 1.0.2

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 (168) hide show
  1. package/dist/cjs/css/boundingBox.js +72 -0
  2. package/dist/cjs/css/cn.js +39 -0
  3. package/dist/cjs/css/imageColorset.js +96 -0
  4. package/dist/cjs/data/countries.js +98 -0
  5. package/dist/cjs/{_fileType.js → data/fileType.js} +37 -19
  6. package/dist/cjs/data/format.js +136 -0
  7. package/dist/cjs/helpers/debounce.js +63 -0
  8. package/dist/cjs/{useLogger.js → helpers/getLogger.js} +96 -96
  9. package/dist/cjs/helpers/lonlat.js +99 -0
  10. package/dist/cjs/index.js +99 -76
  11. package/dist/cjs/lists/dedupe.js +54 -0
  12. package/dist/cjs/lists/flatten.js +66 -0
  13. package/dist/cjs/{_getByPath.js → lists/getByPath.js} +58 -37
  14. package/dist/cjs/lists/groupBy.js +49 -0
  15. package/dist/cjs/matchers/compare.js +70 -0
  16. package/dist/cjs/matchers/isEmpty.js +34 -0
  17. package/dist/cjs/matchers/isEqual.js +82 -0
  18. package/dist/cjs/matchers/nullChecks.js +61 -0
  19. package/dist/cjs/matchers/typeChecks.js +127 -0
  20. package/dist/cjs/strings/ascii.js +105 -0
  21. package/dist/cjs/strings/hash.js +75 -0
  22. package/dist/cjs/strings/interpolate.js +47 -0
  23. package/dist/cjs/strings/matchString.js +54 -0
  24. package/dist/cjs/strings/template.js +48 -0
  25. package/dist/cjs/strings/tokenize.js +83 -0
  26. package/dist/cjs/utils/mergeRefs.js +56 -0
  27. package/dist/esm/css/boundingBox.js +71 -0
  28. package/dist/esm/css/cn.js +39 -0
  29. package/dist/esm/css/imageColorset.js +96 -0
  30. package/dist/esm/data/countries.js +98 -0
  31. package/dist/esm/{_fileType.js → data/fileType.js} +37 -19
  32. package/dist/esm/data/format.js +138 -0
  33. package/dist/esm/helpers/debounce.js +63 -0
  34. package/dist/esm/{useLogger.js → helpers/getLogger.js} +96 -96
  35. package/dist/esm/helpers/lonlat.js +97 -0
  36. package/dist/esm/index.js +54 -36
  37. package/dist/esm/lists/dedupe.js +54 -0
  38. package/dist/esm/lists/flatten.js +65 -0
  39. package/dist/esm/{_getByPath.js → lists/getByPath.js} +50 -29
  40. package/dist/esm/lists/groupBy.js +49 -0
  41. package/dist/esm/matchers/compare.js +70 -0
  42. package/dist/esm/matchers/isEmpty.js +34 -0
  43. package/dist/esm/matchers/isEqual.js +82 -0
  44. package/dist/esm/matchers/nullChecks.js +58 -0
  45. package/dist/esm/matchers/typeChecks.js +119 -0
  46. package/dist/esm/strings/ascii.js +105 -0
  47. package/dist/esm/strings/hash.js +73 -0
  48. package/dist/esm/strings/interpolate.js +47 -0
  49. package/dist/esm/strings/matchString.js +54 -0
  50. package/dist/esm/strings/template.js +48 -0
  51. package/dist/esm/strings/tokenize.js +82 -0
  52. package/dist/esm/utils/mergeRefs.js +56 -0
  53. package/dist/types/css/boundingBox.d.ts +46 -0
  54. package/dist/types/css/cn.d.ts +26 -0
  55. package/dist/types/css/imageColorset.d.ts +37 -0
  56. package/dist/types/data/countries.d.ts +136 -0
  57. package/dist/types/{_fileType.d.ts → data/fileType.d.ts} +41 -1
  58. package/dist/types/data/format.d.ts +122 -0
  59. package/dist/types/helpers/debounce.d.ts +38 -0
  60. package/dist/types/helpers/getLogger.d.ts +49 -0
  61. package/dist/types/helpers/lonlat.d.ts +48 -0
  62. package/dist/types/index.d.ts +54 -20
  63. package/dist/types/lists/dedupe.d.ts +36 -0
  64. package/dist/types/lists/flatten.d.ts +47 -0
  65. package/dist/types/lists/getByPath.d.ts +45 -0
  66. package/dist/types/lists/groupBy.d.ts +36 -0
  67. package/dist/types/matchers/compare.d.ts +38 -0
  68. package/dist/types/matchers/isEmpty.d.ts +38 -0
  69. package/dist/types/matchers/isEqual.d.ts +39 -0
  70. package/dist/types/matchers/nullChecks.d.ts +43 -0
  71. package/dist/types/matchers/typeChecks.d.ts +107 -0
  72. package/dist/types/strings/ascii.d.ts +34 -0
  73. package/dist/types/strings/hash.d.ts +46 -0
  74. package/dist/types/strings/interpolate.d.ts +34 -0
  75. package/dist/types/strings/matchString.d.ts +41 -0
  76. package/dist/types/strings/template.d.ts +35 -0
  77. package/dist/types/strings/tokenize.d.ts +44 -0
  78. package/dist/types/utils/mergeRefs.d.ts +36 -0
  79. package/package.json +13 -11
  80. package/src/css/boundingBox.ts +80 -0
  81. package/src/css/cn.ts +32 -0
  82. package/src/css/imageColorset.ts +97 -0
  83. package/src/data/countries.ts +126 -0
  84. package/src/{_fileType.ts → data/fileType.ts} +72 -89
  85. package/src/data/format.ts +192 -0
  86. package/src/helpers/debounce.ts +49 -0
  87. package/src/{useLogger.ts → helpers/getLogger.ts} +52 -101
  88. package/src/helpers/lonlat.ts +124 -0
  89. package/src/index.ts +57 -37
  90. package/src/lists/dedupe.ts +46 -0
  91. package/src/lists/flatten.ts +56 -0
  92. package/src/lists/getByPath.ts +122 -0
  93. package/src/lists/groupBy.ts +47 -0
  94. package/src/matchers/compare.ts +64 -0
  95. package/src/matchers/isEmpty.ts +54 -0
  96. package/src/matchers/isEqual.ts +71 -0
  97. package/src/matchers/nullChecks.ts +55 -0
  98. package/src/matchers/typeChecks.ts +146 -0
  99. package/src/strings/ascii.ts +100 -0
  100. package/src/strings/hash.ts +68 -0
  101. package/src/strings/interpolate.ts +42 -0
  102. package/src/strings/matchString.ts +49 -0
  103. package/src/strings/template.ts +43 -0
  104. package/src/strings/tokenize.ts +77 -0
  105. package/src/utils/mergeRefs.ts +47 -0
  106. package/src/{yup.ts → utils/yup.ts} +3 -3
  107. package/dist/cjs/_ascii.js +0 -76
  108. package/dist/cjs/_boundingBox.js +0 -59
  109. package/dist/cjs/_countries.js +0 -2805
  110. package/dist/cjs/_debounce.js +0 -53
  111. package/dist/cjs/_dedupe.js +0 -63
  112. package/dist/cjs/_fetch.js +0 -128
  113. package/dist/cjs/_format.js +0 -118
  114. package/dist/cjs/_hash.js +0 -67
  115. package/dist/cjs/_interpolate.js +0 -47
  116. package/dist/cjs/_isEqual.js +0 -108
  117. package/dist/cjs/_isType.js +0 -98
  118. package/dist/cjs/_mergeRefs.js +0 -40
  119. package/dist/cjs/_tokenize.js +0 -67
  120. package/dist/cjs/getImageColorset.js +0 -70
  121. package/dist/esm/_ascii.js +0 -76
  122. package/dist/esm/_boundingBox.js +0 -58
  123. package/dist/esm/_countries.js +0 -2805
  124. package/dist/esm/_debounce.js +0 -53
  125. package/dist/esm/_dedupe.js +0 -60
  126. package/dist/esm/_fetch.js +0 -128
  127. package/dist/esm/_format.js +0 -120
  128. package/dist/esm/_hash.js +0 -65
  129. package/dist/esm/_interpolate.js +0 -46
  130. package/dist/esm/_isEqual.js +0 -106
  131. package/dist/esm/_isType.js +0 -85
  132. package/dist/esm/_mergeRefs.js +0 -40
  133. package/dist/esm/_tokenize.js +0 -66
  134. package/dist/esm/getImageColorset.js +0 -70
  135. package/dist/types/_ascii.d.ts +0 -5
  136. package/dist/types/_boundingBox.d.ts +0 -10
  137. package/dist/types/_countries.d.ts +0 -85
  138. package/dist/types/_debounce.d.ts +0 -8
  139. package/dist/types/_dedupe.d.ts +0 -4
  140. package/dist/types/_fetch.d.ts +0 -23
  141. package/dist/types/_format.d.ts +0 -27
  142. package/dist/types/_getByPath.d.ts +0 -10
  143. package/dist/types/_hash.d.ts +0 -16
  144. package/dist/types/_interpolate.d.ts +0 -6
  145. package/dist/types/_isEqual.d.ts +0 -15
  146. package/dist/types/_isType.d.ts +0 -14
  147. package/dist/types/_mergeRefs.d.ts +0 -1
  148. package/dist/types/_tokenize.d.ts +0 -8
  149. package/dist/types/getImageColorset.d.ts +0 -1
  150. package/dist/types/useLogger.d.ts +0 -25
  151. package/src/_ascii.ts +0 -93
  152. package/src/_boundingBox.ts +0 -72
  153. package/src/_countries.ts +0 -3046
  154. package/src/_debounce.ts +0 -42
  155. package/src/_dedupe.ts +0 -56
  156. package/src/_fetch.ts +0 -109
  157. package/src/_format.ts +0 -141
  158. package/src/_getByPath.ts +0 -108
  159. package/src/_hash.ts +0 -62
  160. package/src/_interpolate.ts +0 -42
  161. package/src/_isEqual.ts +0 -102
  162. package/src/_isType.ts +0 -105
  163. package/src/_mergeRefs.ts +0 -33
  164. package/src/_tokenize.ts +0 -69
  165. package/src/getImageColorset.ts +0 -81
  166. /package/dist/cjs/{yup.js → utils/yup.js} +0 -0
  167. /package/dist/esm/{yup.js → utils/yup.js} +0 -0
  168. /package/dist/types/{yup.d.ts → utils/yup.d.ts} +0 -0
package/src/index.ts CHANGED
@@ -1,51 +1,71 @@
1
- /*
1
+ /**
2
2
  * React Fabric
3
- * @version: 1.0.0
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
4
6
  *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
5
13
  *
6
- * The MIT License (MIT)
7
- * Copyright (c) 2024 Adarsh Pastakia
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
8
16
  *
9
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
- * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
- * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
- * subject to the following conditions:
14
- *
15
- * The above copyright notice and this permission notice shall be included in all copies or substantial
16
- * portions of the Software.
17
- *
18
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
- * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
22
24
  */
23
25
 
24
26
  /* istanbul ignore file */
25
27
 
26
- export * from "./_ascii";
27
- export * from "./_boundingBox";
28
- export { getBoundingBox, getBox } from "./_boundingBox";
29
- export { default as Countries } from "./_countries";
30
- export type { Country } from "./_countries";
31
- export * from "./_debounce";
32
- export * from "./_dedupe";
33
- export { _fetch as fetch } from "./_fetch";
34
- export { default as FileUtil } from "./_fileType";
35
- export * from "./_format";
36
- export * from "./_getByPath";
37
- export * from "./_hash";
38
- export * from "./_interpolate";
39
- export * from "./_isEqual";
40
- export * from "./_isType";
41
- export * from "./_mergeRefs";
42
- export * from "./_tokenize";
43
- export { getImageColorset } from "./getImageColorset";
44
- export { useLogger } from "./useLogger";
45
- export * from "./yup";
28
+ export const UTILITIES_PACKAGE = "@react-fabric/utilities";
29
+ export const UTILITIES_VERSION = "1.0.0";
30
+
31
+ export { getBoundingBox, getBox } from "./css/boundingBox";
32
+ export { cn } from "./css/cn";
33
+ export { getImageColorset } from "./css/imageColorset";
34
+
35
+ export { dedupe } from "./lists/dedupe";
36
+ export { flatten, flattenAndDedupe } from "./lists/flatten";
37
+ export { getByPath, getValue } from "./lists/getByPath";
38
+ export { groupBy } from "./lists/groupBy";
39
+
40
+ export { default as Countries } from "./data/countries";
41
+ export type { Country } from "./data/countries";
42
+ export { default as FileUtil } from "./data/fileType";
43
+ export { Format } from "./data/format";
44
+
45
+ export { debounce } from "./helpers/debounce";
46
+ export { getLogger } from "./helpers/getLogger";
47
+ export { convertToLonLat, getLatitudeLongitude, getLocationAsText } from "./helpers/lonlat";
48
+ export type { LngLatLike } from "./helpers/lonlat";
49
+
50
+ export { compareValues } from "./matchers/compare";
51
+ export { isEmpty } from "./matchers/isEmpty";
52
+ export { isEqual } from "./matchers/isEqual";
53
+ export { isDefined, isNil, isNull, isUndefined } from "./matchers/nullChecks";
54
+ export { isArray, isBoolean, isColor, isFalse, isNumber, isObject, isString, isSvgPath, isTrue } from "./matchers/typeChecks";
55
+
56
+ export { hash, shortHash, uuid } from "./strings/hash";
57
+ export { interpolate } from "./strings/interpolate";
58
+ export { matchString } from "./strings/matchString";
59
+ export { renderTemplate } from "./strings/template";
60
+ export { iconToken, tokenize } from "./strings/tokenize";
61
+
62
+ export { mergeRefs } from "./utils/mergeRefs";
63
+ export { yup } from "./utils/yup";
46
64
 
47
65
  export const EMPTY_ARRAY = [];
48
66
  Object.freeze(EMPTY_ARRAY);
49
67
 
50
68
  export const EMPTY_OBJECT = {};
51
69
  Object.freeze(EMPTY_OBJECT);
70
+
71
+ export const EMPTY_FC = () => () => null;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { isEmpty } from "../matchers/isEmpty";
27
+
28
+ /**
29
+ * Remove duplicates from a list of objects based on a key or the entire object.
30
+ * If a key is provided, it will deduplicate based on the value of that key.
31
+ * If no key is provided, it will deduplicate based on the entire object.
32
+ * This is useful for normalizing data structures that may have nested arrays or objects.
33
+ *
34
+ * @param list
35
+ * @param key
36
+ * @typedef {T} AnyObject - A generic type representing list items.
37
+ * @returns {T[]} - A new array with duplicates removed.
38
+ */
39
+ export function dedupe<T extends AnyObject>(list: T[], key?: keyof T) {
40
+ const filtered = list.filter((i) => !isEmpty(i));
41
+ if (!key) {
42
+ return Array.from(new Set(filtered).values());
43
+ }
44
+
45
+ return Array.from(new Map(filtered.map((obj) => [(obj as KeyValue)[key], obj])).values());
46
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { isEmpty } from "../matchers/isEmpty";
27
+ import { dedupe } from "./dedupe";
28
+
29
+ /**
30
+ * Flatten a list of objects, removing any empty objects.
31
+ * It will flatten up to 5 levels deep and filter out any empty objects.
32
+ * This is useful for normalizing data structures that may have nested arrays or objects.
33
+ *
34
+ * @param list
35
+ * @typedef {T} AnyObject - A generic type representing list items.
36
+ * @returns {T[]} - A flattened array of objects.
37
+ */
38
+ export function flatten<T extends AnyObject>(list: T[]) {
39
+ return list.flat(5).filter((i) => !isEmpty(i));
40
+ }
41
+
42
+ /**
43
+ * Flatten a list of objects and remove duplicates based on a key or the entire object.
44
+ * If a key is provided, it will deduplicate based on the value of that key.
45
+ * If no key is provided, it will deduplicate based on the entire object.
46
+ * This is useful for normalizing data structures that may have nested arrays or objects.
47
+ * Flattening is done first, followed by deduplication.
48
+ *
49
+ * @param list
50
+ * @param key
51
+ * @typedef {T} AnyObject - A generic type representing list items.
52
+ * @returns {T[]} - A new array with duplicates removed.
53
+ */
54
+ export function flattenAndDedupe<T extends AnyObject>(list: T[][] = [], key?: keyof T) {
55
+ return dedupe(flatten(list) as T[], key);
56
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { isEmpty } from "../matchers/isEmpty";
27
+ import { isNil } from "../matchers/nullChecks";
28
+ import { isArray, isObject } from "../matchers/typeChecks";
29
+
30
+ /* eslint-disable @typescript-eslint/no-explicit-any */
31
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
32
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
33
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
34
+
35
+ function _get(hit: any, field: string): any {
36
+ /** ***************** return if object is empty *******************/
37
+ /* istanbul ignore next */
38
+ if (isNil(hit)) {
39
+ return undefined;
40
+ }
41
+ /** ***************** return object if property is empty *******************/
42
+ /* istanbul ignore next */
43
+ if (isNil(field)) {
44
+ return hit;
45
+ }
46
+
47
+ if (isObject(hit) || isArray(hit)) {
48
+ /** ***************** return value if object.property is not empty *******************/
49
+ if (!isNil(hit[field as any])) {
50
+ return hit[field as any];
51
+ }
52
+
53
+ /** ***************** if property name contains `.` separator split to multiple property names *******************/
54
+ if (field.includes(".")) {
55
+ let currentValue: any = hit;
56
+
57
+ const path = field.split(".");
58
+ for (let i = path.length - 1; i >= 0; i--) {
59
+ /** ***************** when property name is `a.b.c` iterate until property found in object. eg. `a`, `a.b` *******************/
60
+ const outerPath = path.slice(0, i).join(".");
61
+
62
+ /** ***************** join rest of property list *******************/
63
+ const innerPath = path.slice(i).join(".");
64
+
65
+ /** ***************** if current outer path found in object *******************/
66
+ if (outerPath in currentValue) {
67
+ currentValue = currentValue[outerPath];
68
+
69
+ /** ***************** check for type array *******************/
70
+ if (isArray(currentValue)) {
71
+ /** ***************** return value if remaining path found *******************/
72
+ if (currentValue[innerPath as any]) {
73
+ return currentValue[innerPath as any];
74
+ }
75
+ /** ***************** return array map of inner propeties *******************/
76
+ return currentValue.map((val) => _get(val, innerPath)).filter((val) => !isEmpty(val));
77
+ } else {
78
+ /** ***************** return value of remaining path *******************/
79
+ return _get(currentValue, innerPath);
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ return undefined;
87
+ }
88
+
89
+ /**
90
+ * Get a value from an object by a given path.
91
+ * This function allows you to retrieve a value from a nested object structure using a dot-separated path.
92
+ * If the path does not exist or the object is empty, it returns a default value.
93
+ * @param {KeyValue} obj - The object from which to retrieve the value.
94
+ * @param {string} path - The dot-separated path to the value within the object.
95
+ * @param {any} defaultValue - The value to return if the path does not exist or the object is empty.
96
+ * @typedef {T} AnyObject - The type of the value to be returned.
97
+ * @returns {T} The value at the specified path, or the default value if the path does not exist.
98
+ */
99
+ export function getByPath<T = unknown>(obj: KeyValue, path: string, defaultValue: any = undefined): T {
100
+ /** ***************** if empty object/path return defaultValue *******************/
101
+ if (isEmpty(obj) || isEmpty(path)) {
102
+ return defaultValue;
103
+ }
104
+ const value = _get(obj, path);
105
+ if (!isNil(value)) {
106
+ return value;
107
+ }
108
+ return defaultValue;
109
+ }
110
+
111
+ /**
112
+ * Get the first non-empty value from the provided arguments.
113
+ * This function is useful for finding a default value or fallback when multiple values are provided.
114
+ * It checks each argument in order and returns the first one that is not empty.
115
+ * If all arguments are empty, it returns `undefined`.
116
+ *
117
+ * @param {...any} args - The values to check for non-emptiness.
118
+ * @returns {any} The first non-empty value, or `undefined` if all are empty.
119
+ */
120
+ export function getValue<T = string>(...args: T[]) {
121
+ return args.find((a) => !isEmpty(a));
122
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ /**
27
+ * Group a list of objects by a specific property.
28
+ * If the property is not present in an object, it will use the `missingKey` value as the key.
29
+ * This is useful for organizing data into categories based on a specific property.
30
+ *
31
+ * @param list
32
+ * @param prop
33
+ * @param missingKey
34
+ * @typedef {T} KeyValue - A generic type representing list items with key-value pairs.
35
+ * @returns {KeyValue<T[]>} - An object mapping keys to arrays of items.
36
+ */
37
+ export function groupBy<T extends KeyValue>(list: T[], prop: keyof T, missingKey?: string): KeyValue<T[]> {
38
+ return list.reduce<KeyValue<T[]>>(
39
+ (r, i) => {
40
+ const key = (i[prop] as string) ?? missingKey;
41
+ if (!r[key]) r[key] = [];
42
+ r[key].push(i);
43
+ return r;
44
+ },
45
+ {} as KeyValue<T[]>,
46
+ );
47
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { getByPath } from "../lists/getByPath";
27
+ import { ascii } from "../strings/ascii";
28
+ import { isBoolean, isNumber, isObject } from "./typeChecks";
29
+
30
+ /**
31
+ * Compare two values for sorting.
32
+ * This function compares two values based on the specified order (ascending or descending).
33
+ * If a key is provided, it compares the values of that key in the objects.
34
+ * It handles various data types such as strings, numbers, booleans, and objects.
35
+ * It returns a comparison function that can be used with array sorting methods.
36
+ * * The comparison is done in a case-insensitive manner for strings,
37
+ * treating special characters as their ASCII equivalents.
38
+ *
39
+ * @param {("asc" | "desc")} order - The order of comparison, either "asc" for ascending or "desc" for descending.
40
+ * @param {string} key - An optional key to compare values within objects.
41
+ * @returns {Function} A comparison function that can be used with array sorting methods.
42
+ */
43
+ export function compareValues(order: "asc" | "desc" = "asc", key?: string) {
44
+ return (a: unknown, b: unknown) => {
45
+ const aValue: unknown = key && isObject(a) ? getByPath(a, key, "") : a;
46
+ const bValue: unknown = key && isObject(b) ? getByPath(b, key, "") : b;
47
+
48
+ const bigger = order === "asc" ? 1 : -1;
49
+ const smaller = order === "desc" ? 1 : -1;
50
+ // return 0 when equal, allowing for multiple sorting properties
51
+ if (aValue === bValue) return 0;
52
+ // if numbers check greater
53
+ if (isNumber(aValue) && isNumber(bValue)) {
54
+ return aValue > bValue ? bigger : smaller;
55
+ }
56
+ // if boolean sort true first
57
+ if (isBoolean(aValue) && isBoolean(bValue)) {
58
+ return aValue ? smaller : bigger;
59
+ }
60
+ const _a = ascii(aValue as string).toLowerCase();
61
+ const _b = ascii(bValue as string).toLowerCase();
62
+ return _a.localeCompare(_b) === 1 ? bigger : smaller;
63
+ };
64
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { isDefined, isNil } from "./nullChecks";
27
+
28
+ /**
29
+ * Check if a value is empty.
30
+ * A value is considered empty if it is:
31
+ * - undefined
32
+ * - null
33
+ * - an empty string
34
+ * - an empty array
35
+ * - an empty object
36
+ * - an empty Map or Set
37
+ * - a boolean, number, or bigint (not empty)
38
+ * @param val - The value to check.
39
+ * @returns true if the value is empty, false otherwise.
40
+ */
41
+ export function isEmpty(val: AnyObject): val is undefined {
42
+ if (val instanceof Date) return false;
43
+ if (isNil(val) || val === "") {
44
+ return true;
45
+ }
46
+ if (Array.isArray(val) && val.filter(isDefined).length === 0) {
47
+ return true;
48
+ }
49
+ if (val instanceof Map || val instanceof Set) return val.size === 0;
50
+ if (["boolean", "number", "bigint", "function"].includes(typeof val)) {
51
+ return false;
52
+ }
53
+ return !!val && typeof val === "object" && Object.entries(val as object).length === 0;
54
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import { isNil } from "./nullChecks";
27
+ import { isBoolean, isNumber, isObject, isString } from "./typeChecks";
28
+
29
+ /**
30
+ * Compare two objects or values for equality.
31
+ * This function checks if two values are equal, including nested objects and arrays.
32
+ * It handles various data types such as strings, numbers, booleans, arrays, and objects.
33
+ * If both values are `null` or `undefined`, they are considered equal.
34
+ * If both values are primitive types (string, number, boolean), they are compared directly.
35
+ * If both values are arrays, they are compared element by element.
36
+ * If both values are objects, they are compared by their keys and values.
37
+ * If the values are of different types or structures, they are considered not equal.
38
+ *
39
+ * @param {unknown} obj - The first value to compare.
40
+ * @param {unknown} test - The second value to compare against the first.
41
+ * @returns {boolean} - Returns true if the values are equal, false otherwise.
42
+ */
43
+ export function isEqual(obj: unknown, test: unknown): boolean {
44
+ if (isNil(obj) && isNil(test)) {
45
+ return true;
46
+ }
47
+ if (isString(obj) || isNumber(obj) || isBoolean(obj)) {
48
+ return obj === test;
49
+ } else if (Array.isArray(obj) && Array.isArray(test)) {
50
+ // if length dont match return false
51
+ if (obj.length !== test.length) {
52
+ return false;
53
+ }
54
+ // check if some value is false
55
+ return !obj.some((o, i) => !isEqual(o, test[i]));
56
+ } else if (isObject(obj) && isObject(test)) {
57
+ const okeys = Object.keys(obj);
58
+ const tkeys = Object.keys(test);
59
+ // if keys length dont match return false
60
+ if (okeys.length !== tkeys.length) {
61
+ return false;
62
+ }
63
+ // if keys dont match return false
64
+ if (okeys.some((k) => !tkeys.includes(k))) {
65
+ return false;
66
+ }
67
+ // check if some value is false
68
+ return !Object.keys(obj).some((k) => !isEqual(obj[k], test[k]));
69
+ }
70
+ return false;
71
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * React Fabric
3
+ * @version 1.0.0
4
+ * @license MIT
5
+ * @copyright 2024 Adarsh Pastakia
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ /**
27
+ * Check if a value is undefined.
28
+ * @param value - The value to check.
29
+ * @returns true if the value is undefined, false otherwise.
30
+ */
31
+ export function isUndefined(value: AnyObject): value is undefined {
32
+ return value === undefined;
33
+ }
34
+
35
+ /**
36
+ * Check if a value is null.
37
+ * @param value - The value to check.
38
+ * @returns true if the value is null, false otherwise.
39
+ */
40
+ export function isNull(value: AnyObject): value is null {
41
+ return value === null;
42
+ }
43
+
44
+ /**
45
+ * Check if a value is either null or undefined.
46
+ * @param val - The value to check.
47
+ * @returns true if the value is null or undefined, false otherwise.
48
+ */
49
+ export function isNil(val: AnyObject): val is null | undefined {
50
+ return val === undefined || val === null;
51
+ }
52
+
53
+ export function isDefined(val: AnyObject): boolean {
54
+ return !isNil(val) && val !== "";
55
+ }