@react-fabric/utilities 1.0.0-beta.5 → 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.
- package/dist/cjs/_ascii.js +34 -9
- package/dist/cjs/_boundingBox.js +16 -4
- package/dist/cjs/_countries.js +39 -21
- package/dist/cjs/_debounce.js +8 -2
- package/dist/cjs/_dedupe.js +48 -2
- package/dist/cjs/_fetch.js +12 -7
- package/dist/cjs/_fileType.js +16 -0
- package/dist/cjs/_format.js +19 -6
- package/dist/cjs/_getByPath.js +16 -5
- package/dist/cjs/_hash.js +13 -7
- package/dist/cjs/_interpolate.js +18 -2
- package/dist/cjs/_isEqual.js +40 -6
- package/dist/cjs/_isType.js +121 -0
- package/dist/cjs/_lonlat.js +87 -0
- package/dist/cjs/_mergeRefs.js +11 -0
- package/dist/cjs/_tokenize.js +17 -4
- package/dist/cjs/getImageColorset.js +17 -2
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/useLogger.js +9 -1
- package/dist/esm/_ascii.js +34 -9
- package/dist/esm/_boundingBox.js +16 -4
- package/dist/esm/_countries.js +39 -21
- package/dist/esm/_debounce.js +8 -2
- package/dist/esm/_dedupe.js +48 -2
- package/dist/esm/_fetch.js +12 -7
- package/dist/esm/_fileType.js +16 -0
- package/dist/esm/_format.js +19 -6
- package/dist/esm/_getByPath.js +16 -5
- package/dist/esm/_hash.js +13 -7
- package/dist/esm/_interpolate.js +18 -2
- package/dist/esm/_isEqual.js +41 -7
- package/dist/esm/_isType.js +121 -0
- package/dist/esm/_lonlat.js +86 -0
- package/dist/esm/_mergeRefs.js +11 -0
- package/dist/esm/_tokenize.js +17 -4
- package/dist/esm/getImageColorset.js +17 -2
- package/dist/esm/index.js +1 -0
- package/dist/esm/useLogger.js +9 -1
- package/dist/types/_ascii.d.ts +7 -2
- package/dist/types/_boundingBox.d.ts +16 -4
- package/dist/types/_countries.d.ts +39 -21
- package/dist/types/_debounce.d.ts +8 -2
- package/dist/types/_dedupe.d.ts +47 -4
- package/dist/types/_fetch.d.ts +12 -9
- package/dist/types/_fileType.d.ts +16 -0
- package/dist/types/_format.d.ts +84 -13
- package/dist/types/_getByPath.d.ts +15 -4
- package/dist/types/_hash.d.ts +12 -6
- package/dist/types/_interpolate.d.ts +17 -2
- package/dist/types/_isEqual.d.ts +37 -6
- package/dist/types/_isType.d.ts +111 -3
- package/dist/types/_lonlat.d.ts +45 -0
- package/dist/types/_mergeRefs.d.ts +11 -0
- package/dist/types/_tokenize.d.ts +16 -4
- package/dist/types/getImageColorset.d.ts +15 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/useLogger.d.ts +9 -1
- package/package.json +4 -4
- package/src/_ascii.ts +10 -5
- package/src/_boundingBox.ts +16 -4
- package/src/_countries.ts +39 -21
- package/src/_debounce.ts +8 -2
- package/src/_dedupe.ts +51 -5
- package/src/_fetch.ts +12 -7
- package/src/_fileType.ts +16 -0
- package/src/_format.ts +87 -18
- package/src/_getByPath.ts +18 -7
- package/src/_hash.ts +13 -7
- package/src/_interpolate.ts +17 -2
- package/src/_isEqual.ts +41 -7
- package/src/_isType.ts +113 -1
- package/src/_lonlat.ts +124 -0
- package/src/_mergeRefs.ts +11 -0
- package/src/_tokenize.ts +16 -4
- package/src/getImageColorset.ts +17 -2
- package/src/index.ts +1 -0
- package/src/useLogger.ts +9 -1
package/src/_getByPath.ts
CHANGED
|
@@ -35,10 +35,10 @@ const _get = (hit: any, field: string): any => {
|
|
|
35
35
|
return hit;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
if (isObject(hit)) {
|
|
38
|
+
if (isObject(hit) || isArray(hit)) {
|
|
39
39
|
/** ***************** return value if object.property is not empty *******************/
|
|
40
|
-
if (!isNil(hit[field])) {
|
|
41
|
-
return hit[field];
|
|
40
|
+
if (!isNil(hit[field as any])) {
|
|
41
|
+
return hit[field as any];
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/** ***************** if property name contains `.` separator split to multiple property names *******************/
|
|
@@ -80,8 +80,14 @@ const _get = (hit: any, field: string): any => {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
83
|
+
* Get a value from an object by a given path.
|
|
84
|
+
* This function allows you to retrieve a value from a nested object structure using a dot-separated path.
|
|
85
|
+
* If the path does not exist or the object is empty, it returns a default value.
|
|
86
|
+
* @param {KeyValue} obj - The object from which to retrieve the value.
|
|
87
|
+
* @param {string} path - The dot-separated path to the value within the object.
|
|
88
|
+
* @param {any} defaultValue - The value to return if the path does not exist or the object is empty.
|
|
89
|
+
* @typedef {T} AnyObject - The type of the value to be returned.
|
|
90
|
+
* @returns {T} The value at the specified path, or the default value if the path does not exist.
|
|
85
91
|
*/
|
|
86
92
|
export const getByPath = <T = any>(
|
|
87
93
|
obj: KeyValue,
|
|
@@ -100,8 +106,13 @@ export const getByPath = <T = any>(
|
|
|
100
106
|
};
|
|
101
107
|
|
|
102
108
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
109
|
+
* Get the first non-empty value from the provided arguments.
|
|
110
|
+
* This function is useful for finding a default value or fallback when multiple values are provided.
|
|
111
|
+
* It checks each argument in order and returns the first one that is not empty.
|
|
112
|
+
* If all arguments are empty, it returns `undefined`.
|
|
113
|
+
*
|
|
114
|
+
* @param {...any} args - The values to check for non-emptiness.
|
|
115
|
+
* @returns {any} The first non-empty value, or `undefined` if all are empty.
|
|
105
116
|
*/
|
|
106
117
|
export const getValue = (...args: any[]) => {
|
|
107
118
|
return args.find((a) => !isEmpty(a));
|
package/src/_hash.ts
CHANGED
|
@@ -24,9 +24,11 @@
|
|
|
24
24
|
import { isEmpty } from "./_isType";
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* Generate a hash from a string.
|
|
28
|
+
* This function computes a simple hash by iterating over each character in the string,
|
|
29
|
+
* performing bitwise operations to produce a 32-bit integer hash.
|
|
28
30
|
* @param str
|
|
29
|
-
* @
|
|
31
|
+
* @returns {number} A 32-bit integer hash of the input string.
|
|
30
32
|
*/
|
|
31
33
|
export const hash = (str: string) => {
|
|
32
34
|
let hash = 0;
|
|
@@ -42,16 +44,20 @@ export const hash = (str: string) => {
|
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
+
* Generate a short hash using the current timestamp and a random number.
|
|
48
|
+
* This function creates a unique identifier by combining the current time in milliseconds
|
|
49
|
+
* with a random number, and then converting it to a base-36 string.
|
|
50
|
+
* @returns {string} A short hash string.
|
|
47
51
|
*/
|
|
48
52
|
export const shortHash = () => {
|
|
49
|
-
return (new Date().getTime()
|
|
53
|
+
return (new Date().getTime() * Math.random()).toString(36);
|
|
50
54
|
};
|
|
51
55
|
|
|
52
56
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
57
|
+
* Generate a UUID (Universally Unique Identifier).
|
|
58
|
+
* This function creates a version 4 UUID by generating random numbers
|
|
59
|
+
* and formatting them according to the UUID standard.
|
|
60
|
+
* @returns {string} A UUID string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.
|
|
55
61
|
*/
|
|
56
62
|
export const uuid = () => {
|
|
57
63
|
return [1e7, 1e3, 4e3, 8e3, 1e11]
|
package/src/_interpolate.ts
CHANGED
|
@@ -24,8 +24,13 @@
|
|
|
24
24
|
import { getByPath } from "./_getByPath";
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
27
|
+
* Interpolate a string with values from a model.
|
|
28
|
+
* This function replaces placeholders in the format ${key} with corresponding values from the model object.
|
|
29
|
+
* If the value is not a string or number, it returns an empty string.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} str - The string to interpolate.
|
|
32
|
+
* @param {KeyValue} model - The model object containing key-value pairs for interpolation.
|
|
33
|
+
* @returns {string} The interpolated string with placeholders replaced by values from the model.
|
|
29
34
|
*/
|
|
30
35
|
export const interpolate = (str: string, model: KeyValue) => {
|
|
31
36
|
return str.replace(/\${([^{}]*)}/g, (a: string, b: string): string => {
|
|
@@ -34,6 +39,16 @@ export const interpolate = (str: string, model: KeyValue) => {
|
|
|
34
39
|
});
|
|
35
40
|
};
|
|
36
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Render a template string with values from a model.
|
|
44
|
+
* This function replaces placeholders in the format {{key}} with corresponding values from the model object.
|
|
45
|
+
* If the value is not a string or number, it returns an empty string.
|
|
46
|
+
* This is similar to the interpolate function but for handlebar templates.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} str - The template string to render.
|
|
49
|
+
* @param {KeyValue} model - The model object containing key-value pairs for rendering the template.
|
|
50
|
+
* @returns {string} The rendered template string with placeholders replaced by values from the model.
|
|
51
|
+
*/
|
|
37
52
|
export const renderTemplate = (str: string, model: KeyValue) => {
|
|
38
53
|
return str.replace(/{{([^{}]*)}}/g, (a: string, b: string): string => {
|
|
39
54
|
const r = getByPath(model, b);
|
package/src/_isEqual.ts
CHANGED
|
@@ -22,13 +22,26 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import { ascii } from "./_ascii";
|
|
25
|
-
import { isBoolean, isNumber, isObject, isString } from "./_isType";
|
|
25
|
+
import { isBoolean, isNil, isNumber, isObject, isString } from "./_isType";
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
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 => {
|
|
42
|
+
if (isNil(obj) && isNil(test)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
32
45
|
if (isString(obj) || isNumber(obj) || isBoolean(obj)) {
|
|
33
46
|
return obj === test;
|
|
34
47
|
} else if (Array.isArray(obj) && Array.isArray(test)) {
|
|
@@ -56,8 +69,20 @@ export const isEqual = (obj: unknown, test: unknown): boolean => {
|
|
|
56
69
|
};
|
|
57
70
|
|
|
58
71
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
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.
|
|
61
86
|
*/
|
|
62
87
|
export const matchString = (
|
|
63
88
|
base: string,
|
|
@@ -75,8 +100,17 @@ export const matchString = (
|
|
|
75
100
|
};
|
|
76
101
|
|
|
77
102
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
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.
|
|
80
114
|
*/
|
|
81
115
|
export const compareValues =
|
|
82
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
|
|
|
32
|
-
|
|
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
|
+
/**
|
|
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
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
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, isObject, isString } from "./_isType";
|
|
25
|
+
|
|
26
|
+
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
|
+
| string;
|
|
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
|
+
*/
|
|
54
|
+
export const convertLatLng = (
|
|
55
|
+
lonLat: LngLatLike,
|
|
56
|
+
): [lon: number, lat: number] => {
|
|
57
|
+
if (
|
|
58
|
+
(isArray(lonLat) && lonLat.length === 2) ||
|
|
59
|
+
(isArray(lonLat) && lonLat.length === 3)
|
|
60
|
+
) {
|
|
61
|
+
return [Number(lonLat[0]), Number(lonLat[1])];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (isObject(lonLat)) {
|
|
65
|
+
if ("longitude" in lonLat) {
|
|
66
|
+
return [lonLat.longitude, lonLat.latitude];
|
|
67
|
+
}
|
|
68
|
+
return ["lon" in lonLat ? lonLat.lon : lonLat.lng, lonLat.lat];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (isString(lonLat)) {
|
|
72
|
+
const [lat, lon] = lonLat.split(",");
|
|
73
|
+
return [+lon, +lat];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw Error("Invalid geo coordinates");
|
|
77
|
+
};
|
|
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
|
+
*/
|
|
91
|
+
const convertToDMS = (coord: number, isLat = false) => {
|
|
92
|
+
const degree = Math.floor(coord);
|
|
93
|
+
const hour = (coord - degree) * 60;
|
|
94
|
+
const minute = (hour - Math.floor(hour)) * 60;
|
|
95
|
+
|
|
96
|
+
return `${degree}˚${Math.floor(hour)}'${minute.toFixed(2)}"${
|
|
97
|
+
coord < 0 ? (isLat ? "S" : "W") : isLat ? "N" : "E"
|
|
98
|
+
}`;
|
|
99
|
+
};
|
|
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
|
+
*/
|
|
121
|
+
export const getLocationAsText = (latlon: LngLatLike) => {
|
|
122
|
+
const [lon, lat] = convertLatLng(latlon);
|
|
123
|
+
return `${convertToDMS(lat, true)}, ${convertToDMS(lon)}`;
|
|
124
|
+
};
|
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
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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];
|
package/src/getImageColorset.ts
CHANGED
|
@@ -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 (
|
|
60
|
-
|
|
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
|
-
*
|
|
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) {
|