@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
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
+ var _isType = require('./_isType.js');
5
+
6
+ var getLatitudeLongitude = function getLatitudeLongitude(lonLat) {
7
+ var _convertLatLng = convertLatLng(lonLat),
8
+ _convertLatLng2 = _slicedToArray(_convertLatLng, 2),
9
+ lon = _convertLatLng2[0],
10
+ lat = _convertLatLng2[1];
11
+ return {
12
+ latitude: lat,
13
+ longitude: lon
14
+ };
15
+ };
16
+
17
+ /**
18
+ * Convert various formats of longitude and latitude to a tuple of [lon, lat].
19
+ * This function supports:
20
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
21
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
22
+ * - Strings in the format "lat,lon"
23
+ * The function will return a tuple of numbers [lon, lat].
24
+ *
25
+ * @param {LngLatLike} lonLat - The input longitude and latitude in various formats.
26
+ * @returns { [lon: number, lat: number] } A tuple of [lon, lat] where lon and lat are numbers.
27
+ * @throws Error if the input is not a valid format.
28
+ */
29
+ var convertLatLng = function convertLatLng(lonLat) {
30
+ if (_isType.isEmpty(lonLat)) {
31
+ throw Error("Invalid geo coordinates");
32
+ }
33
+ if (_isType.isArray(lonLat) && lonLat.length === 2 || _isType.isArray(lonLat) && lonLat.length === 3) {
34
+ return [Number(lonLat[0]), Number(lonLat[1])];
35
+ }
36
+ if (_isType.isObject(lonLat)) {
37
+ if ("longitude" in lonLat) {
38
+ return [lonLat.longitude, lonLat.latitude];
39
+ }
40
+ return ["lon" in lonLat ? lonLat.lon : lonLat.lng, lonLat.lat];
41
+ }
42
+ if (_isType.isString(lonLat)) {
43
+ var _lonLat$split = lonLat.split(","),
44
+ _lonLat$split2 = _slicedToArray(_lonLat$split, 2),
45
+ _lat = _lonLat$split2[0],
46
+ _lon = _lonLat$split2[1];
47
+ if (!isNaN(+_lat) && !isNaN(+_lon)) return [+_lon, +_lat];
48
+ }
49
+ throw Error("Invalid geo coordinates");
50
+ };
51
+
52
+ /**
53
+ * Convert a coordinate to Degrees, Minutes, Seconds (DMS) format.
54
+ * This function takes a coordinate and returns a string in the format "degrees˚minutes'seconds" with direction (N/S or E/W).
55
+ * It handles both latitude and longitude coordinates.
56
+ * This function assumes that the input coordinate is in decimal degrees format.
57
+ * It converts the coordinate to DMS format by calculating the degrees, minutes, and seconds,
58
+ * and then formatting the result as a string.
59
+ *
60
+ * @param {number} coord - The coordinate to convert.
61
+ * @param {boolean} isLat - Whether the coordinate is latitude (default is false for longitude).
62
+ * @returns {string} A string representation of the coordinate in DMS format.
63
+ */
64
+ var convertToDMS = function convertToDMS(coord) {
65
+ var isLat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
66
+ var degree = Math.floor(Math.abs(coord));
67
+ var hour = (Math.abs(coord) - degree) * 60;
68
+ var minute = (hour - Math.floor(hour)) * 60;
69
+ return "".concat(degree, "\u02DA").concat(Math.floor(hour), "'").concat(minute.toFixed(2), "\"").concat(coord < 0 ? isLat ? "S" : "W" : isLat ? "N" : "E");
70
+ };
71
+
72
+ /**
73
+ * Convert a latitude and longitude to a human-readable text format.
74
+ * This function takes a latitude and longitude in various formats and
75
+ * returns a string in the format "degrees˚minutes'seconds" with direction (N/S, E/W).
76
+ *
77
+ * This function supports:
78
+ *
79
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
80
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
81
+ * - Strings in the format "lat,lon"
82
+ *
83
+ * @param {LngLatLike} latlon - The latitude and longitude in various formats.
84
+ * @returns {string} A string representation of the location in DMS format.
85
+ *
86
+ * @example
87
+ * ```jsx
88
+ * const location = getLocationAsText({ lat: 40.7128, lng: -74.0060 });
89
+ * console.log(location); // "40˚42'51.36"N, 74˚0'21.60"W"
90
+ * ```
91
+ */
92
+ var getLocationAsText = function getLocationAsText(latlon) {
93
+ var _convertLatLng3 = convertLatLng(latlon),
94
+ _convertLatLng4 = _slicedToArray(_convertLatLng3, 2),
95
+ lon = _convertLatLng4[0],
96
+ lat = _convertLatLng4[1];
97
+ return "".concat(convertToDMS(lat, true), ", ").concat(convertToDMS(lon));
98
+ };
99
+
100
+ exports.convertLatLng = convertLatLng;
101
+ exports.getLatitudeLongitude = getLatitudeLongitude;
102
+ exports.getLocationAsText = getLocationAsText;
@@ -25,6 +25,17 @@
25
25
 
26
26
  /* istanbul ignore file */
27
27
 
28
+ /**
29
+ * Merge multiple refs into a single ref callback.
30
+ * This function allows you to combine multiple refs (either callback refs or React refs)
31
+ * into a single ref callback that can be used in React components.
32
+ * This is particularly useful when you want to assign a single element to multiple refs,
33
+ * such as when using third-party libraries that require refs or when you want to share refs between components.
34
+ * The function iterates over the provided refs and assigns the element to each ref.
35
+ *
36
+ * @param {AnyObject[]} refs - An array of refs to merge.
37
+ * @returns {function} A function that takes an element and assigns it to all provided refs.
38
+ */
28
39
  var mergeRefs = function mergeRefs() {
29
40
  for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
30
41
  refs[_key] = arguments[_key];
@@ -27,10 +27,14 @@ var _isType = require('./_isType.js');
27
27
 
28
28
 
29
29
  /**
30
- * split text into array of tokens by word boundaries
31
- * @param str
32
- * @param keywords
33
- * @internal
30
+ * Tokenize a string based on provided keywords.
31
+ * This function splits the input string into tokens, where each token is an array containing
32
+ * the substring before the keyword and the keyword itself.
33
+ * If no keywords are provided, it returns the entire string as a single token.
34
+ *
35
+ * @param {string} str - The string to tokenize.
36
+ * @param {string | string[]} keywords - A string or an array of strings to use as keywords for tokenization.
37
+ * @returns {Array<[string, string]>} An array of tokens, where each token is an array of [substring, keyword].
34
38
  */
35
39
  var tokenize = function tokenize(str, keywords) {
36
40
  if (_isType.isEmpty(keywords)) {
@@ -56,6 +60,15 @@ var tokenize = function tokenize(str, keywords) {
56
60
  tokens.push([str.substring(lastIndex), ""]);
57
61
  return tokens;
58
62
  };
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
+ */
59
72
  var iconToken = function iconToken(str) {
60
73
  var split = str.trim().split(" ");
61
74
  var first = split[0];
@@ -25,6 +25,21 @@
25
25
 
26
26
  /* istanbul ignore file */
27
27
 
28
+ /**
29
+ * Get the color set of an image element.
30
+ * This function analyzes the pixel data of an image to determine if it is predominantly light or dark.
31
+ * It returns "light", "dark", or "light_transparent"/"dark_transparent" based on the pixel analysis.
32
+ * It uses a canvas to draw the image and extract pixel data for analysis.
33
+ * * The function calculates the brightness of each pixel using the formula:
34
+ * brightness = 0.2126 * R + 0.7152 * G + 0.0722 * B
35
+ * * It counts the number of light and dark pixels, and determines the overall color set based on the ratio of light to dark pixels.
36
+ * * If the image has transparent pixels, it appends "_transparent" to the result.
37
+ * * This function is useful for determining the color scheme of an image, which can be helpful for UI design,
38
+ * theming, and accessibility considerations.
39
+ *
40
+ * @param {HTMLImageElement} el - The HTMLImageElement to analyze.
41
+ * @returns {string} A string indicating the color set of the image.
42
+ */
28
43
  var getImageColorset = function getImageColorset(el) {
29
44
  // create canvas
30
45
  var canvas = document.createElement("canvas");
@@ -45,8 +60,8 @@ var getImageColorset = function getImageColorset(el) {
45
60
  transparent = true;
46
61
  continue;
47
62
  }
48
- if (transparent && data[x - 1] === 1) {
49
- continue;
63
+ if (data[x + 3] < 1) {
64
+ transparent = true;
50
65
  }
51
66
  r = data[x];
52
67
  g = data[x + 1];
package/dist/cjs/index.js CHANGED
@@ -13,6 +13,7 @@ var _hash = require('./_hash.js');
13
13
  var _interpolate = require('./_interpolate.js');
14
14
  var _isEqual = require('./_isEqual.js');
15
15
  var _isType = require('./_isType.js');
16
+ var _lonlat = require('./_lonlat.js');
16
17
  var _mergeRefs = require('./_mergeRefs.js');
17
18
  var _tokenize = require('./_tokenize.js');
18
19
  var getImageColorset = require('./getImageColorset.js');
@@ -95,6 +96,7 @@ exports.matchString = _isEqual.matchString;
95
96
  exports.isArray = _isType.isArray;
96
97
  exports.isBoolean = _isType.isBoolean;
97
98
  exports.isColor = _isType.isColor;
99
+ exports.isDefined = _isType.isDefined;
98
100
  exports.isEmpty = _isType.isEmpty;
99
101
  exports.isFalse = _isType.isFalse;
100
102
  exports.isNil = _isType.isNil;
@@ -106,6 +108,9 @@ exports.isString = _isType.isString;
106
108
  exports.isSvgPath = _isType.isSvgPath;
107
109
  exports.isTrue = _isType.isTrue;
108
110
  exports.isUndefined = _isType.isUndefined;
111
+ exports.convertLatLng = _lonlat.convertLatLng;
112
+ exports.getLatitudeLongitude = _lonlat.getLatitudeLongitude;
113
+ exports.getLocationAsText = _lonlat.getLocationAsText;
109
114
  exports.mergeRefs = _mergeRefs.mergeRefs;
110
115
  exports.iconToken = _tokenize.iconToken;
111
116
  exports.tokenize = _tokenize.tokenize;
@@ -207,7 +207,15 @@ var logger = function logger() {
207
207
  };
208
208
 
209
209
  /**
210
- * @internal
210
+ * useLogger is a custom hook that provides logging functionality.
211
+ * It allows you to log messages with different levels (log, debug, info, error, warning)
212
+ * and also provides a timer function to measure the duration of processes.
213
+ * The logs can include additional details such as the module name, process name, duration, and total time.
214
+ * It is designed to work in both Node.js and browser environments, adapting the log format accordingly.
215
+ * It uses ANSI escape codes for terminal colors in Node.js and CSS styles for console logs in the browser.
216
+ *
217
+ * @param mod - Optional module name to include in the log details.
218
+ * @returns An object with methods for logging messages and a timer function.
211
219
  */
212
220
  var useLogger = function useLogger(mod) {
213
221
  return {
@@ -1,8 +1,31 @@
1
1
  'use client';
2
2
 
3
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
3
  import { isEmpty } from './_isType.js';
5
4
 
5
+ /*
6
+ * React Fabric
7
+ * @version: 1.0.0
8
+ *
9
+ *
10
+ * The MIT License (MIT)
11
+ * Copyright (c) 2024 Adarsh Pastakia
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
14
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
15
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
17
+ * subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
20
+ * portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+
6
29
  /** ***************** latin-ascii conversions *******************/
7
30
  var conversions = {
8
31
  AE: "Æ|Ǽ",
@@ -58,17 +81,19 @@ var conversions = {
58
81
  };
59
82
 
60
83
  /**
61
- * latin to ascii converter for convenient searching
62
- * @internal
84
+ * Converts a string to its ASCII representation by replacing
85
+ * special characters with their ASCII equivalents.
86
+ * This function is useful for normalizing strings for search or display purposes.
87
+ * It replaces characters like "Ä" with "Ae", "ß" with "ss", etc.
88
+ *
89
+ * @param {string} str - The input string to be converted.
90
+ * @returns {string} - The ASCII representation of the input string.
63
91
  */
64
92
  var ascii = function ascii(str) {
65
93
  if (isEmpty(str)) return "";
66
- Object.entries(conversions).forEach(function (_ref) {
67
- var _ref2 = _slicedToArray(_ref, 2),
68
- a = _ref2[0],
69
- l = _ref2[1];
70
- var re = new RegExp(l, "g");
71
- str = "".concat(str).replace(re, a);
94
+ Object.entries(conversions).forEach(function (entry) {
95
+ var re = new RegExp(entry[1], "g");
96
+ str = "".concat(str).replace(re, entry[0]);
72
97
  });
73
98
  return str;
74
99
  };
@@ -18,8 +18,13 @@ var _getBox = function _getBox(box) {
18
18
  };
19
19
 
20
20
  /**
21
- * box "x,y,w,h" | [x,y,w,h]
22
- * returns canvas box [x,y,w,h]
21
+ * Get bounding box from a string or array.
22
+ * The box can be in the format "x,y,w,h" or [x,y,w,h].
23
+ * The box is adjusted to ensure that x and y are non-negative.
24
+ * The width and height are adjusted accordingly.
25
+ *
26
+ * @param box "x,y,w,h" | [x,y,w,h]
27
+ * @returns canvas box [x,y,w,h]
23
28
  */
24
29
  var getBox = function getBox() {
25
30
  var box = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "0,0,0,0";
@@ -37,8 +42,15 @@ var getBox = function getBox() {
37
42
  };
38
43
 
39
44
  /**
40
- * box "x1,y1,x2,y2" | [x1,y1,x2,y2]
41
- * returns canvas box [x,y,w,h]
45
+ * Get bounding box from a string or array.
46
+ * The box can be in the format "x1,y1,x2,y2" or [x1,y1,x2,y2].
47
+ * The box is adjusted to ensure that x1 and y1 are non-negative.
48
+ * The width and height are calculated as x2 - x1 and y2 - y1 respectively.
49
+ * If x1 or y1 are negative, they are set to 0 and the width and height are adjusted accordingly.
50
+ * This is useful for ensuring that the bounding box is always within the canvas area.
51
+ *
52
+ * @param box "x1,y1,x2,y2" | [x1,y1,x2,y2]
53
+ * @returns canvas box [x,y,w,h]
42
54
  */
43
55
  var getBoundingBox = function getBoundingBox() {
44
56
  var box = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "0,0,0,0";