@react-fabric/utilities 1.0.0-beta.50 → 1.0.0-beta.52

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 (73) hide show
  1. package/dist/cjs/_ascii.js +7 -2
  2. package/dist/cjs/_boundingBox.js +16 -4
  3. package/dist/cjs/_countries.js +39 -21
  4. package/dist/cjs/_debounce.js +8 -2
  5. package/dist/cjs/_dedupe.js +47 -0
  6. package/dist/cjs/_fetch.js +12 -7
  7. package/dist/cjs/_fileType.js +16 -0
  8. package/dist/cjs/_format.js +19 -6
  9. package/dist/cjs/_getByPath.js +15 -4
  10. package/dist/cjs/_hash.js +12 -6
  11. package/dist/cjs/_interpolate.js +18 -2
  12. package/dist/cjs/_isEqual.js +37 -6
  13. package/dist/cjs/_isType.js +121 -0
  14. package/dist/cjs/_lonlat.js +46 -0
  15. package/dist/cjs/_mergeRefs.js +11 -0
  16. package/dist/cjs/_tokenize.js +17 -4
  17. package/dist/cjs/getImageColorset.js +15 -0
  18. package/dist/cjs/useLogger.js +9 -1
  19. package/dist/esm/_ascii.js +7 -2
  20. package/dist/esm/_boundingBox.js +16 -4
  21. package/dist/esm/_countries.js +39 -21
  22. package/dist/esm/_debounce.js +8 -2
  23. package/dist/esm/_dedupe.js +47 -0
  24. package/dist/esm/_fetch.js +12 -7
  25. package/dist/esm/_fileType.js +16 -0
  26. package/dist/esm/_format.js +19 -6
  27. package/dist/esm/_getByPath.js +15 -4
  28. package/dist/esm/_hash.js +12 -6
  29. package/dist/esm/_interpolate.js +18 -2
  30. package/dist/esm/_isEqual.js +37 -6
  31. package/dist/esm/_isType.js +121 -0
  32. package/dist/esm/_lonlat.js +46 -0
  33. package/dist/esm/_mergeRefs.js +11 -0
  34. package/dist/esm/_tokenize.js +17 -4
  35. package/dist/esm/getImageColorset.js +15 -0
  36. package/dist/esm/useLogger.js +9 -1
  37. package/dist/types/_ascii.d.ts +7 -2
  38. package/dist/types/_boundingBox.d.ts +16 -4
  39. package/dist/types/_countries.d.ts +39 -21
  40. package/dist/types/_debounce.d.ts +8 -2
  41. package/dist/types/_dedupe.d.ts +47 -4
  42. package/dist/types/_fetch.d.ts +12 -9
  43. package/dist/types/_fileType.d.ts +16 -0
  44. package/dist/types/_format.d.ts +84 -13
  45. package/dist/types/_getByPath.d.ts +15 -4
  46. package/dist/types/_hash.d.ts +12 -6
  47. package/dist/types/_interpolate.d.ts +17 -2
  48. package/dist/types/_isEqual.d.ts +37 -6
  49. package/dist/types/_isType.d.ts +110 -2
  50. package/dist/types/_lonlat.d.ts +32 -0
  51. package/dist/types/_mergeRefs.d.ts +11 -0
  52. package/dist/types/_tokenize.d.ts +16 -4
  53. package/dist/types/getImageColorset.d.ts +15 -0
  54. package/dist/types/useLogger.d.ts +9 -1
  55. package/package.json +2 -2
  56. package/src/_ascii.ts +7 -2
  57. package/src/_boundingBox.ts +16 -4
  58. package/src/_countries.ts +39 -21
  59. package/src/_debounce.ts +8 -2
  60. package/src/_dedupe.ts +43 -0
  61. package/src/_fetch.ts +12 -7
  62. package/src/_fileType.ts +16 -0
  63. package/src/_format.ts +87 -18
  64. package/src/_getByPath.ts +15 -4
  65. package/src/_hash.ts +12 -6
  66. package/src/_interpolate.ts +17 -2
  67. package/src/_isEqual.ts +37 -6
  68. package/src/_isType.ts +112 -0
  69. package/src/_lonlat.ts +44 -0
  70. package/src/_mergeRefs.ts +11 -0
  71. package/src/_tokenize.ts +16 -4
  72. package/src/getImageColorset.ts +15 -0
  73. package/src/useLogger.ts +9 -1
package/src/_isEqual.ts CHANGED
@@ -25,8 +25,18 @@ import { ascii } from "./_ascii";
25
25
  import { isBoolean, isNil, isNumber, isObject, isString } from "./_isType";
26
26
 
27
27
  /**
28
- * check equality of objects
29
- * @internal
28
+ * Compare two objects or values for equality.
29
+ * This function checks if two values are equal, including nested objects and arrays.
30
+ * It handles various data types such as strings, numbers, booleans, arrays, and objects.
31
+ * If both values are `null` or `undefined`, they are considered equal.
32
+ * If both values are primitive types (string, number, boolean), they are compared directly.
33
+ * If both values are arrays, they are compared element by element.
34
+ * If both values are objects, they are compared by their keys and values.
35
+ * If the values are of different types or structures, they are considered not equal.
36
+ *
37
+ * @param {unknown} obj - The first value to compare.
38
+ * @param {unknown} test - The second value to compare against the first.
39
+ * @returns {boolean} - Returns true if the values are equal, false otherwise.
30
40
  */
31
41
  export const isEqual = (obj: unknown, test: unknown): boolean => {
32
42
  if (isNil(obj) && isNil(test)) {
@@ -59,8 +69,20 @@ export const isEqual = (obj: unknown, test: unknown): boolean => {
59
69
  };
60
70
 
61
71
  /**
62
- * match strings
63
- * @internal
72
+ * Match a string against another string with options for equality.
73
+ * This function checks if the base string starts with, includes, or is equal to the match string,
74
+ * based on the specified equality condition.
75
+ * It normalizes both strings to lowercase and ASCII format before comparison.
76
+ * This is useful for case-insensitive and ASCII-only comparisons,
77
+ * such as when filtering or searching through text data.
78
+ *
79
+ * @param {string} base - The base string to match against.
80
+ * @param {string} match - The string to match with the base string.
81
+ * @param {("start" | boolean)} equality - The type of match to perform:
82
+ * - "start": checks if the base string starts with the match string.
83
+ * - true: checks for exact equality.
84
+ * - false (default): checks if the base string includes the match string.
85
+ * @returns {boolean} - Returns true if the base string matches the conditions specified by the match string and equality.
64
86
  */
65
87
  export const matchString = (
66
88
  base: string,
@@ -78,8 +100,17 @@ export const matchString = (
78
100
  };
79
101
 
80
102
  /**
81
- * compare values
82
- * @internal
103
+ * Compare two values for sorting.
104
+ * This function compares two values based on the specified order (ascending or descending).
105
+ * If a key is provided, it compares the values of that key in the objects.
106
+ * It handles various data types such as strings, numbers, booleans, and objects.
107
+ * It returns a comparison function that can be used with array sorting methods.
108
+ * * The comparison is done in a case-insensitive manner for strings,
109
+ * treating special characters as their ASCII equivalents.
110
+ *
111
+ * @param {("asc" | "desc")} order - The order of comparison, either "asc" for ascending or "desc" for descending.
112
+ * @param {string} key - An optional key to compare values within objects.
113
+ * @returns {Function} A comparison function that can be used with array sorting methods.
83
114
  */
84
115
  export const compareValues =
85
116
  (order: "asc" | "desc" = "asc", key?: string) =>
package/src/_isType.ts CHANGED
@@ -21,17 +21,46 @@
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
 
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
+ */
32
47
  export const isNil = (val: AnyObject): val is null | undefined => {
33
48
  return val === undefined || val === null;
34
49
  };
50
+
51
+ /**
52
+ * Check if a value is empty.
53
+ * A value is considered empty if it is:
54
+ * - undefined
55
+ * - null
56
+ * - an empty string
57
+ * - an empty array
58
+ * - an empty object
59
+ * - an empty Map or Set
60
+ * - a boolean, number, or bigint (not empty)
61
+ * @param val - The value to check.
62
+ * @returns true if the value is empty, false otherwise.
63
+ */
35
64
  export const isEmpty = (val: AnyObject): val is undefined => {
36
65
  if (val instanceof Date) return false;
37
66
  if (isNil(val) || val === "") {
@@ -47,6 +76,16 @@ export const isEmpty = (val: AnyObject): val is undefined => {
47
76
  return !!val && typeof val === "object" && Object.entries(val).length === 0;
48
77
  };
49
78
 
79
+ /**
80
+ * Check if a value is an object.
81
+ * An object is considered valid if it is:
82
+ * - not null or undefined
83
+ * - not a Date instance
84
+ * - of type 'object'
85
+ * - not an array
86
+ * @param val - The value to check.
87
+ * @returns true if the value is an object, false otherwise.
88
+ */
50
89
  export const isObject = (val: AnyObject): val is KeyValue => {
51
90
  return (
52
91
  !isNil(val) &&
@@ -55,21 +94,60 @@ export const isObject = (val: AnyObject): val is KeyValue => {
55
94
  !Array.isArray(val)
56
95
  );
57
96
  };
97
+
98
+ /**
99
+ * Check if a value is an array.
100
+ * An array is considered valid if it is:
101
+ * - not null or undefined
102
+ * - an instance of Array
103
+ * @param val - The value to check.
104
+ * @returns true if the value is an array, false otherwise.
105
+ */
58
106
  export const isArray = <T = AnyObject>(val: AnyObject): val is T[] => {
59
107
  return Array.isArray(val);
60
108
  };
61
109
 
110
+ /**
111
+ * Check if a value is a string.
112
+ * A string is considered valid if it is:
113
+ * - not null or undefined
114
+ * - of type 'string'
115
+ * @param value - The value to check.
116
+ * @returns true if the value is a string, false otherwise.
117
+ */
62
118
  export const isString = (value: AnyObject): value is string => {
63
119
  return typeof value === "string";
64
120
  };
65
121
 
122
+ /**
123
+ * Check if a value is a number.
124
+ * A number is considered valid if it is:
125
+ * - not null or undefined
126
+ * - of type 'number'
127
+ * @param value - The value to check.
128
+ * @returns true if the value is a number, false otherwise.
129
+ */
66
130
  export const isNumber = (value: AnyObject): value is number => {
67
131
  return typeof value === "number";
68
132
  };
69
133
 
134
+ /**
135
+ * Check if a value is a boolean.
136
+ * A boolean is considered valid if it is:
137
+ * - not null or undefined
138
+ * - of type 'boolean'
139
+ */
70
140
  export const isBoolean = (value: AnyObject): value is boolean => {
71
141
  return typeof value === "boolean";
72
142
  };
143
+
144
+ /**
145
+ * Check if a value is a true.
146
+ * A value is considered true if it is:
147
+ * - strictly equal to true
148
+ * - equal to 1
149
+ * - a string that is "yes" or "true" (case insensitive)
150
+ */
73
151
  export const isTrue = (value: AnyObject = ""): value is true => {
74
152
  return (
75
153
  value === true ||
@@ -78,6 +156,14 @@ export const isTrue = (value: AnyObject = ""): value is true => {
78
156
  value?.toString().toLowerCase() === "true"
79
157
  );
80
158
  };
159
+
160
+ /**
161
+ * Check if a value is false.
162
+ * A value is considered false if it is:
163
+ * - strictly equal to false
164
+ * - equal to 0
165
+ * - a string that is "no" or "false" (case insensitive)
166
+ */
81
167
  export const isFalse = (value: AnyObject = ""): value is false => {
82
168
  return (
83
169
  value === false ||
@@ -87,6 +173,17 @@ export const isFalse = (value: AnyObject = ""): value is false => {
87
173
  );
88
174
  };
89
175
 
176
+ /**
177
+ * Check if a value is a valid color.
178
+ * A color is considered valid if it:
179
+ * - starts with a '#' (hex color)
180
+ * - starts with 'rgb' (RGB color)
181
+ * - starts with 'hsl' (HSL color)
182
+ * - starts with 'lab' (Lab color)
183
+ * - is equal to 'transparent'
184
+ * @param color - The value to check.
185
+ * @returns true if the value is a valid color, false otherwise.
186
+ */
90
187
  export const isColor = (color: string) =>
91
188
  color.startsWith?.("#") ||
92
189
  color.startsWith?.("rgb") ||
@@ -94,12 +191,27 @@ export const isColor = (color: string) =>
94
191
  color.startsWith?.("lab") ||
95
192
  color === "transparent";
96
193
 
194
+ /**
195
+ * Check if a value is a valid SVG path.
196
+ * A valid SVG path is a string that:
197
+ * - starts with 'M' or 'm' (move to command)
198
+ * - contains at least one digit or decimal point
199
+ * - ends with 'Z' or 'z' (close path command)
200
+ * @param value - The value to check.
201
+ * @returns true if the value is a valid SVG path, false otherwise.
202
+ */
97
203
  export const isSvgPath = (value: AnyObject): value is string => {
98
204
  return (
99
205
  typeof value === "string" && value.match(/^[Mm][\d.].*[\dzZ]$/) !== null
100
206
  );
101
207
  };
102
208
 
209
+ /**
210
+ * Check if the document is in RTL (Right-to-Left) mode.
211
+ * This function checks the computed style of the document's root element
212
+ * to determine if the direction is set to 'rtl'.
213
+ * @returns true if the document is in RTL mode, false otherwise.
214
+ */
103
215
  export const isRtl = () => {
104
216
  return getComputedStyle(document.documentElement).direction === "rtl";
105
217
  };
package/src/_lonlat.ts CHANGED
@@ -39,6 +39,18 @@ type LngLatLike =
39
39
  | [lon: number | string, lat: number | string]
40
40
  | string;
41
41
 
42
+ /**
43
+ * Convert various formats of longitude and latitude to a tuple of [lon, lat].
44
+ * This function supports:
45
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
46
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
47
+ * - Strings in the format "lat,lon"
48
+ * The function will return a tuple of numbers [lon, lat].
49
+ *
50
+ * @param {LngLatLike} lonLat - The input longitude and latitude in various formats.
51
+ * @returns { [lon: number, lat: number] } A tuple of [lon, lat] where lon and lat are numbers.
52
+ * @throws Error if the input is not a valid format.
53
+ */
42
54
  export const convertLatLng = (
43
55
  lonLat: LngLatLike,
44
56
  ): [lon: number, lat: number] => {
@@ -64,6 +76,18 @@ export const convertLatLng = (
64
76
  throw Error("Invalid geo coordinates");
65
77
  };
66
78
 
79
+ /**
80
+ * Convert a coordinate to Degrees, Minutes, Seconds (DMS) format.
81
+ * This function takes a coordinate and returns a string in the format "degrees˚minutes'seconds" with direction (N/S or E/W).
82
+ * It handles both latitude and longitude coordinates.
83
+ * This function assumes that the input coordinate is in decimal degrees format.
84
+ * It converts the coordinate to DMS format by calculating the degrees, minutes, and seconds,
85
+ * and then formatting the result as a string.
86
+ *
87
+ * @param {number} coord - The coordinate to convert.
88
+ * @param {boolean} isLat - Whether the coordinate is latitude (default is false for longitude).
89
+ * @returns {string} A string representation of the coordinate in DMS format.
90
+ */
67
91
  const convertToDMS = (coord: number, isLat = false) => {
68
92
  const degree = Math.floor(coord);
69
93
  const hour = (coord - degree) * 60;
@@ -74,6 +98,26 @@ const convertToDMS = (coord: number, isLat = false) => {
74
98
  }`;
75
99
  };
76
100
 
101
+ /**
102
+ * Convert a latitude and longitude to a human-readable text format.
103
+ * This function takes a latitude and longitude in various formats and
104
+ * returns a string in the format "degrees˚minutes'seconds" with direction (N/S, E/W).
105
+ *
106
+ * This function supports:
107
+ *
108
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
109
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
110
+ * - Strings in the format "lat,lon"
111
+ *
112
+ * @param {LngLatLike} latlon - The latitude and longitude in various formats.
113
+ * @returns {string} A string representation of the location in DMS format.
114
+ *
115
+ * @example
116
+ * ```jsx
117
+ * const location = getLocationAsText({ lat: 40.7128, lng: -74.0060 });
118
+ * console.log(location); // "40˚42'51.36"N, 74˚0'21.60"W"
119
+ * ```
120
+ */
77
121
  export const getLocationAsText = (latlon: LngLatLike) => {
78
122
  const [lon, lat] = convertLatLng(latlon);
79
123
  return `${convertToDMS(lat, true)}, ${convertToDMS(lon)}`;
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");
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) {