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

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 +39 -21
  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 +19 -7
  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 +39 -21
  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 +19 -7
  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 +39 -21
  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 +7 -7
  59. package/src/_ascii.ts +10 -5
  60. package/src/_boundingBox.ts +16 -4
  61. package/src/_countries.ts +39 -21
  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 +88 -19
  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/_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) {