@react-fabric/utilities 1.0.0-beta.6 → 1.0.0-beta.61

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 (77) hide show
  1. package/dist/cjs/_ascii.js +34 -9
  2. package/dist/cjs/_boundingBox.js +16 -4
  3. package/dist/cjs/_countries.js +61 -2769
  4. package/dist/cjs/_debounce.js +8 -2
  5. package/dist/cjs/_dedupe.js +48 -2
  6. package/dist/cjs/_fetch.js +12 -7
  7. package/dist/cjs/_fileType.js +16 -0
  8. package/dist/cjs/_format.js +20 -8
  9. package/dist/cjs/_getByPath.js +16 -5
  10. package/dist/cjs/_hash.js +13 -7
  11. package/dist/cjs/_interpolate.js +18 -2
  12. package/dist/cjs/_isEqual.js +40 -6
  13. package/dist/cjs/_isType.js +126 -1
  14. package/dist/cjs/_lonlat.js +102 -0
  15. package/dist/cjs/_mergeRefs.js +11 -0
  16. package/dist/cjs/_tokenize.js +17 -4
  17. package/dist/cjs/getImageColorset.js +17 -2
  18. package/dist/cjs/index.js +5 -0
  19. package/dist/cjs/useLogger.js +9 -1
  20. package/dist/esm/_ascii.js +34 -9
  21. package/dist/esm/_boundingBox.js +16 -4
  22. package/dist/esm/_countries.js +61 -2769
  23. package/dist/esm/_debounce.js +8 -2
  24. package/dist/esm/_dedupe.js +48 -2
  25. package/dist/esm/_fetch.js +12 -7
  26. package/dist/esm/_fileType.js +16 -0
  27. package/dist/esm/_format.js +20 -8
  28. package/dist/esm/_getByPath.js +16 -5
  29. package/dist/esm/_hash.js +13 -7
  30. package/dist/esm/_interpolate.js +18 -2
  31. package/dist/esm/_isEqual.js +41 -7
  32. package/dist/esm/_isType.js +126 -2
  33. package/dist/esm/_lonlat.js +100 -0
  34. package/dist/esm/_mergeRefs.js +11 -0
  35. package/dist/esm/_tokenize.js +17 -4
  36. package/dist/esm/getImageColorset.js +17 -2
  37. package/dist/esm/index.js +2 -1
  38. package/dist/esm/useLogger.js +9 -1
  39. package/dist/types/_ascii.d.ts +7 -2
  40. package/dist/types/_boundingBox.d.ts +16 -4
  41. package/dist/types/_countries.d.ts +87 -60
  42. package/dist/types/_debounce.d.ts +8 -2
  43. package/dist/types/_dedupe.d.ts +47 -4
  44. package/dist/types/_fetch.d.ts +12 -9
  45. package/dist/types/_fileType.d.ts +16 -0
  46. package/dist/types/_format.d.ts +84 -13
  47. package/dist/types/_getByPath.d.ts +15 -4
  48. package/dist/types/_hash.d.ts +12 -6
  49. package/dist/types/_interpolate.d.ts +17 -2
  50. package/dist/types/_isEqual.d.ts +37 -6
  51. package/dist/types/_isType.d.ts +112 -3
  52. package/dist/types/_lonlat.d.ts +48 -0
  53. package/dist/types/_mergeRefs.d.ts +11 -0
  54. package/dist/types/_tokenize.d.ts +16 -4
  55. package/dist/types/getImageColorset.d.ts +15 -0
  56. package/dist/types/index.d.ts +1 -0
  57. package/dist/types/useLogger.d.ts +9 -1
  58. package/package.json +8 -7
  59. package/src/_ascii.ts +10 -5
  60. package/src/_boundingBox.ts +16 -4
  61. package/src/_countries.ts +63 -2986
  62. package/src/_debounce.ts +8 -2
  63. package/src/_dedupe.ts +51 -5
  64. package/src/_fetch.ts +12 -7
  65. package/src/_fileType.ts +16 -0
  66. package/src/_format.ts +89 -20
  67. package/src/_getByPath.ts +18 -7
  68. package/src/_hash.ts +13 -7
  69. package/src/_interpolate.ts +17 -2
  70. package/src/_isEqual.ts +41 -7
  71. package/src/_isType.ts +116 -2
  72. package/src/_lonlat.ts +135 -0
  73. package/src/_mergeRefs.ts +11 -0
  74. package/src/_tokenize.ts +16 -4
  75. package/src/getImageColorset.ts +17 -2
  76. package/src/index.ts +1 -0
  77. package/src/useLogger.ts +9 -1
package/src/_isType.ts CHANGED
@@ -21,23 +21,54 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
+ /**
25
+ * Check if a value is undefined.
26
+ * @param value - The value to check.
27
+ * @returns true if the value is undefined, false otherwise.
28
+ */
24
29
  export const isUndefined = (value: AnyObject): value is undefined => {
25
30
  return value === undefined;
26
31
  };
27
32
 
33
+ /**
34
+ * Check if a value is null.
35
+ * @param value - The value to check.
36
+ * @returns true if the value is null, false otherwise.
37
+ */
28
38
  export const isNull = (value: AnyObject): value is null => {
29
39
  return value === null;
30
40
  };
31
41
 
32
- export const isNil = (val: AnyObject): val is undefined => {
42
+ /**
43
+ * Check if a value is either null or undefined.
44
+ * @param val - The value to check.
45
+ * @returns true if the value is null or undefined, false otherwise.
46
+ */
47
+ export const isNil = (val: AnyObject): val is null | undefined => {
33
48
  return val === undefined || val === null;
34
49
  };
50
+
51
+ export const isDefined = (val: AnyObject): boolean => !isNil(val) && val !== "";
52
+
53
+ /**
54
+ * Check if a value is empty.
55
+ * A value is considered empty if it is:
56
+ * - undefined
57
+ * - null
58
+ * - an empty string
59
+ * - an empty array
60
+ * - an empty object
61
+ * - an empty Map or Set
62
+ * - a boolean, number, or bigint (not empty)
63
+ * @param val - The value to check.
64
+ * @returns true if the value is empty, false otherwise.
65
+ */
35
66
  export const isEmpty = (val: AnyObject): val is undefined => {
36
67
  if (val instanceof Date) return false;
37
68
  if (isNil(val) || val === "") {
38
69
  return true;
39
70
  }
40
- if (Array.isArray(val) && val.filter(Boolean).length === 0) {
71
+ if (Array.isArray(val) && val.filter(isDefined).length === 0) {
41
72
  return true;
42
73
  }
43
74
  if (val instanceof Map || val instanceof Set) return val.size === 0;
@@ -47,6 +78,16 @@ export const isEmpty = (val: AnyObject): val is undefined => {
47
78
  return !!val && typeof val === "object" && Object.entries(val).length === 0;
48
79
  };
49
80
 
81
+ /**
82
+ * Check if a value is an object.
83
+ * An object is considered valid if it is:
84
+ * - not null or undefined
85
+ * - not a Date instance
86
+ * - of type 'object'
87
+ * - not an array
88
+ * @param val - The value to check.
89
+ * @returns true if the value is an object, false otherwise.
90
+ */
50
91
  export const isObject = (val: AnyObject): val is KeyValue => {
51
92
  return (
52
93
  !isNil(val) &&
@@ -55,21 +96,60 @@ export const isObject = (val: AnyObject): val is KeyValue => {
55
96
  !Array.isArray(val)
56
97
  );
57
98
  };
99
+
100
+ /**
101
+ * Check if a value is an array.
102
+ * An array is considered valid if it is:
103
+ * - not null or undefined
104
+ * - an instance of Array
105
+ * @param val - The value to check.
106
+ * @returns true if the value is an array, false otherwise.
107
+ */
58
108
  export const isArray = <T = AnyObject>(val: AnyObject): val is T[] => {
59
109
  return Array.isArray(val);
60
110
  };
61
111
 
112
+ /**
113
+ * Check if a value is a string.
114
+ * A string is considered valid if it is:
115
+ * - not null or undefined
116
+ * - of type 'string'
117
+ * @param value - The value to check.
118
+ * @returns true if the value is a string, false otherwise.
119
+ */
62
120
  export const isString = (value: AnyObject): value is string => {
63
121
  return typeof value === "string";
64
122
  };
65
123
 
124
+ /**
125
+ * Check if a value is a number.
126
+ * A number is considered valid if it is:
127
+ * - not null or undefined
128
+ * - of type 'number'
129
+ * @param value - The value to check.
130
+ * @returns true if the value is a number, false otherwise.
131
+ */
66
132
  export const isNumber = (value: AnyObject): value is number => {
67
133
  return typeof value === "number";
68
134
  };
69
135
 
136
+ /**
137
+ * Check if a value is a boolean.
138
+ * A boolean is considered valid if it is:
139
+ * - not null or undefined
140
+ * - of type 'boolean'
141
+ */
70
142
  export const isBoolean = (value: AnyObject): value is boolean => {
71
143
  return typeof value === "boolean";
72
144
  };
145
+
146
+ /**
147
+ * Check if a value is a true.
148
+ * A value is considered true if it is:
149
+ * - strictly equal to true
150
+ * - equal to 1
151
+ * - a string that is "yes" or "true" (case insensitive)
152
+ */
73
153
  export const isTrue = (value: AnyObject = ""): value is true => {
74
154
  return (
75
155
  value === true ||
@@ -78,6 +158,14 @@ export const isTrue = (value: AnyObject = ""): value is true => {
78
158
  value?.toString().toLowerCase() === "true"
79
159
  );
80
160
  };
161
+
162
+ /**
163
+ * Check if a value is false.
164
+ * A value is considered false if it is:
165
+ * - strictly equal to false
166
+ * - equal to 0
167
+ * - a string that is "no" or "false" (case insensitive)
168
+ */
81
169
  export const isFalse = (value: AnyObject = ""): value is false => {
82
170
  return (
83
171
  value === false ||
@@ -87,6 +175,17 @@ export const isFalse = (value: AnyObject = ""): value is false => {
87
175
  );
88
176
  };
89
177
 
178
+ /**
179
+ * Check if a value is a valid color.
180
+ * A color is considered valid if it:
181
+ * - starts with a '#' (hex color)
182
+ * - starts with 'rgb' (RGB color)
183
+ * - starts with 'hsl' (HSL color)
184
+ * - starts with 'lab' (Lab color)
185
+ * - is equal to 'transparent'
186
+ * @param color - The value to check.
187
+ * @returns true if the value is a valid color, false otherwise.
188
+ */
90
189
  export const isColor = (color: string) =>
91
190
  color.startsWith?.("#") ||
92
191
  color.startsWith?.("rgb") ||
@@ -94,12 +193,27 @@ export const isColor = (color: string) =>
94
193
  color.startsWith?.("lab") ||
95
194
  color === "transparent";
96
195
 
196
+ /**
197
+ * Check if a value is a valid SVG path.
198
+ * A valid SVG path is a string that:
199
+ * - starts with 'M' or 'm' (move to command)
200
+ * - contains at least one digit or decimal point
201
+ * - ends with 'Z' or 'z' (close path command)
202
+ * @param value - The value to check.
203
+ * @returns true if the value is a valid SVG path, false otherwise.
204
+ */
97
205
  export const isSvgPath = (value: AnyObject): value is string => {
98
206
  return (
99
207
  typeof value === "string" && value.match(/^[Mm][\d.].*[\dzZ]$/) !== null
100
208
  );
101
209
  };
102
210
 
211
+ /**
212
+ * Check if the document is in RTL (Right-to-Left) mode.
213
+ * This function checks the computed style of the document's root element
214
+ * to determine if the direction is set to 'rtl'.
215
+ * @returns true if the document is in RTL mode, false otherwise.
216
+ */
103
217
  export const isRtl = () => {
104
218
  return getComputedStyle(document.documentElement).direction === "rtl";
105
219
  };
package/src/_lonlat.ts ADDED
@@ -0,0 +1,135 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2025 Adarsh Pastakia
8
+ *
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.
22
+ */
23
+
24
+ import { isArray, isEmpty, isObject, isString } from "./_isType";
25
+
26
+ export type LngLatLike =
27
+ | {
28
+ lng: number;
29
+ lat: number;
30
+ }
31
+ | {
32
+ lon: number;
33
+ lat: number;
34
+ }
35
+ | {
36
+ longitude: number;
37
+ latitude: number;
38
+ }
39
+ | [lon: number | string, lat: number | string]
40
+ | [lon: number | string, lat: number | string, z: number | string]
41
+ | string;
42
+
43
+ export const getLatitudeLongitude = (
44
+ lonLat: LngLatLike,
45
+ ): { latitude: number; longitude: number } => {
46
+ const [lon, lat] = convertLatLng(lonLat);
47
+ return { latitude: lat, longitude: lon };
48
+ };
49
+
50
+ /**
51
+ * Convert various formats of longitude and latitude to a tuple of [lon, lat].
52
+ * This function supports:
53
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
54
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
55
+ * - Strings in the format "lat,lon"
56
+ * The function will return a tuple of numbers [lon, lat].
57
+ *
58
+ * @param {LngLatLike} lonLat - The input longitude and latitude in various formats.
59
+ * @returns { [lon: number, lat: number] } A tuple of [lon, lat] where lon and lat are numbers.
60
+ * @throws Error if the input is not a valid format.
61
+ */
62
+ export const convertLatLng = (
63
+ lonLat: LngLatLike,
64
+ ): [lon: number, lat: number] => {
65
+ if (isEmpty(lonLat)) {
66
+ throw Error("Invalid geo coordinates");
67
+ }
68
+ if (
69
+ (isArray(lonLat) && lonLat.length === 2) ||
70
+ (isArray(lonLat) && lonLat.length === 3)
71
+ ) {
72
+ return [Number(lonLat[0]), Number(lonLat[1])];
73
+ }
74
+
75
+ if (isObject(lonLat)) {
76
+ if ("longitude" in lonLat) {
77
+ return [lonLat.longitude, lonLat.latitude];
78
+ }
79
+ return ["lon" in lonLat ? lonLat.lon : lonLat.lng, lonLat.lat];
80
+ }
81
+
82
+ if (isString(lonLat)) {
83
+ const [lat, lon] = lonLat.split(",");
84
+ if (!isNaN(+lat) && !isNaN(+lon)) return [+lon, +lat];
85
+ }
86
+
87
+ throw Error("Invalid geo coordinates");
88
+ };
89
+
90
+ /**
91
+ * Convert a coordinate to Degrees, Minutes, Seconds (DMS) format.
92
+ * This function takes a coordinate and returns a string in the format "degrees˚minutes'seconds" with direction (N/S or E/W).
93
+ * It handles both latitude and longitude coordinates.
94
+ * This function assumes that the input coordinate is in decimal degrees format.
95
+ * It converts the coordinate to DMS format by calculating the degrees, minutes, and seconds,
96
+ * and then formatting the result as a string.
97
+ *
98
+ * @param {number} coord - The coordinate to convert.
99
+ * @param {boolean} isLat - Whether the coordinate is latitude (default is false for longitude).
100
+ * @returns {string} A string representation of the coordinate in DMS format.
101
+ */
102
+ const convertToDMS = (coord: number, isLat = false) => {
103
+ const degree = Math.floor(Math.abs(coord));
104
+ const hour = (Math.abs(coord) - degree) * 60;
105
+ const minute = (hour - Math.floor(hour)) * 60;
106
+
107
+ return `${degree}˚${Math.floor(hour)}'${minute.toFixed(2)}"${
108
+ coord < 0 ? (isLat ? "S" : "W") : isLat ? "N" : "E"
109
+ }`;
110
+ };
111
+
112
+ /**
113
+ * Convert a latitude and longitude to a human-readable text format.
114
+ * This function takes a latitude and longitude in various formats and
115
+ * returns a string in the format "degrees˚minutes'seconds" with direction (N/S, E/W).
116
+ *
117
+ * This function supports:
118
+ *
119
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
120
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
121
+ * - Strings in the format "lat,lon"
122
+ *
123
+ * @param {LngLatLike} latlon - The latitude and longitude in various formats.
124
+ * @returns {string} A string representation of the location in DMS format.
125
+ *
126
+ * @example
127
+ * ```jsx
128
+ * const location = getLocationAsText({ lat: 40.7128, lng: -74.0060 });
129
+ * console.log(location); // "40˚42'51.36"N, 74˚0'21.60"W"
130
+ * ```
131
+ */
132
+ export const getLocationAsText = (latlon: LngLatLike) => {
133
+ const [lon, lat] = convertLatLng(latlon);
134
+ return `${convertToDMS(lat, true)}, ${convertToDMS(lon)}`;
135
+ };
package/src/_mergeRefs.ts CHANGED
@@ -23,6 +23,17 @@
23
23
 
24
24
  /* istanbul ignore file */
25
25
 
26
+ /**
27
+ * Merge multiple refs into a single ref callback.
28
+ * This function allows you to combine multiple refs (either callback refs or React refs)
29
+ * into a single ref callback that can be used in React components.
30
+ * This is particularly useful when you want to assign a single element to multiple refs,
31
+ * such as when using third-party libraries that require refs or when you want to share refs between components.
32
+ * The function iterates over the provided refs and assigns the element to each ref.
33
+ *
34
+ * @param {AnyObject[]} refs - An array of refs to merge.
35
+ * @returns {function} A function that takes an element and assigns it to all provided refs.
36
+ */
26
37
  export const mergeRefs =
27
38
  (...refs: AnyObject[]) =>
28
39
  (el: AnyObject) => {
package/src/_tokenize.ts CHANGED
@@ -24,10 +24,14 @@
24
24
  import { isArray, isEmpty } from "./_isType";
25
25
 
26
26
  /**
27
- * split text into array of tokens by word boundaries
28
- * @param str
29
- * @param keywords
30
- * @internal
27
+ * Tokenize a string based on provided keywords.
28
+ * This function splits the input string into tokens, where each token is an array containing
29
+ * the substring before the keyword and the keyword itself.
30
+ * If no keywords are provided, it returns the entire string as a single token.
31
+ *
32
+ * @param {string} str - The string to tokenize.
33
+ * @param {string | string[]} keywords - A string or an array of strings to use as keywords for tokenization.
34
+ * @returns {Array<[string, string]>} An array of tokens, where each token is an array of [substring, keyword].
31
35
  */
32
36
  export const tokenize = (str: string, keywords?: string | string[]) => {
33
37
  if (isEmpty(keywords)) {
@@ -57,6 +61,14 @@ export const tokenize = (str: string, keywords?: string | string[]) => {
57
61
  return tokens;
58
62
  };
59
63
 
64
+ /**
65
+ * Generate a token from a string.
66
+ * This function creates a token by taking the first letter of the first word and the first letter of the last word,
67
+ * or the first two letters of the first word if there is no last word.
68
+ *
69
+ * @param {string} str - The string to tokenize.
70
+ * @returns {string} A string token consisting of the first letters of the first and last words, or the first two letters of the first word.
71
+ */
60
72
  export const iconToken = (str: string) => {
61
73
  const split = str.trim().split(" ");
62
74
  const first = split[0];
@@ -23,6 +23,21 @@
23
23
 
24
24
  /* istanbul ignore file */
25
25
 
26
+ /**
27
+ * Get the color set of an image element.
28
+ * This function analyzes the pixel data of an image to determine if it is predominantly light or dark.
29
+ * It returns "light", "dark", or "light_transparent"/"dark_transparent" based on the pixel analysis.
30
+ * It uses a canvas to draw the image and extract pixel data for analysis.
31
+ * * The function calculates the brightness of each pixel using the formula:
32
+ * brightness = 0.2126 * R + 0.7152 * G + 0.0722 * B
33
+ * * It counts the number of light and dark pixels, and determines the overall color set based on the ratio of light to dark pixels.
34
+ * * If the image has transparent pixels, it appends "_transparent" to the result.
35
+ * * This function is useful for determining the color scheme of an image, which can be helpful for UI design,
36
+ * theming, and accessibility considerations.
37
+ *
38
+ * @param {HTMLImageElement} el - The HTMLImageElement to analyze.
39
+ * @returns {string} A string indicating the color set of the image.
40
+ */
26
41
  export const getImageColorset = (el: HTMLImageElement) => {
27
42
  // create canvas
28
43
  const canvas = document.createElement("canvas");
@@ -56,8 +71,8 @@ export const getImageColorset = (el: HTMLImageElement) => {
56
71
  transparent = true;
57
72
  continue;
58
73
  }
59
- if (transparent && data[x - 1] === 1) {
60
- continue;
74
+ if (data[x + 3] < 1) {
75
+ transparent = true;
61
76
  }
62
77
  r = data[x];
63
78
  g = data[x + 1];
package/src/index.ts CHANGED
@@ -38,6 +38,7 @@ export * from "./_hash";
38
38
  export * from "./_interpolate";
39
39
  export * from "./_isEqual";
40
40
  export * from "./_isType";
41
+ export * from "./_lonlat";
41
42
  export * from "./_mergeRefs";
42
43
  export * from "./_tokenize";
43
44
  export { getImageColorset } from "./getImageColorset";
package/src/useLogger.ts CHANGED
@@ -198,7 +198,15 @@ const logger = () => {
198
198
  };
199
199
 
200
200
  /**
201
- * @internal
201
+ * useLogger is a custom hook that provides logging functionality.
202
+ * It allows you to log messages with different levels (log, debug, info, error, warning)
203
+ * and also provides a timer function to measure the duration of processes.
204
+ * The logs can include additional details such as the module name, process name, duration, and total time.
205
+ * It is designed to work in both Node.js and browser environments, adapting the log format accordingly.
206
+ * It uses ANSI escape codes for terminal colors in Node.js and CSS styles for console logs in the browser.
207
+ *
208
+ * @param mod - Optional module name to include in the log details.
209
+ * @returns An object with methods for logging messages and a timer function.
202
210
  */
203
211
  export const useLogger = (mod?: string) => ({
204
212
  timer(process: string) {