@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.
- 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 -7
- 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 +126 -1
- package/dist/cjs/_lonlat.js +102 -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 +5 -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 -7
- 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 +126 -2
- package/dist/esm/_lonlat.js +100 -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 +2 -1
- 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 +112 -3
- package/dist/types/_lonlat.d.ts +48 -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 +7 -7
- 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 +88 -19
- 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 +116 -2
- package/src/_lonlat.ts +135 -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/_format.ts
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
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
|
-
import { differenceInDays, format, parseISO } from "date-fns";
|
|
24
|
+
import { differenceInDays, format as dtformat, parseISO } from "date-fns";
|
|
25
25
|
import {
|
|
26
26
|
parsePhoneNumberFromString,
|
|
27
27
|
type CountryCode,
|
|
@@ -30,9 +30,12 @@ import numeral from "numeral";
|
|
|
30
30
|
import Countries from "./_countries";
|
|
31
31
|
import { isEmpty, isNil } from "./_isType";
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Format utility functions for various data types.
|
|
35
|
+
* This namespace provides methods to format phone numbers, numbers, bytes, percentages, dates, and durations.
|
|
36
|
+
*/
|
|
37
|
+
|
|
34
38
|
export namespace Format {
|
|
35
|
-
/** ***************** get phone number object using `libphonenumber-js` *******************/
|
|
36
39
|
const getPhone = (value: string, country: string) => {
|
|
37
40
|
const phone =
|
|
38
41
|
value.match(/^[0+]/) != null ? value.replace(/^00/, "+") : `+${value}`;
|
|
@@ -46,7 +49,6 @@ export namespace Format {
|
|
|
46
49
|
);
|
|
47
50
|
};
|
|
48
51
|
|
|
49
|
-
/** ***************** common number format method using `numeral` *******************/
|
|
50
52
|
const numberFormat = (number?: string | number, format = "0,0[.]00a") => {
|
|
51
53
|
if (isNil(number)) {
|
|
52
54
|
return "";
|
|
@@ -59,7 +61,7 @@ export namespace Format {
|
|
|
59
61
|
if (!isNaN(value)) {
|
|
60
62
|
const days = differenceInDays(value, 0);
|
|
61
63
|
const prefix = days > 0 ? `${days}d:` : "";
|
|
62
|
-
let time =
|
|
64
|
+
let time = dtformat(
|
|
63
65
|
parseISO(new Date(value).toISOString().replace("Z", "")),
|
|
64
66
|
fmt,
|
|
65
67
|
);
|
|
@@ -68,7 +70,19 @@ export namespace Format {
|
|
|
68
70
|
}
|
|
69
71
|
};
|
|
70
72
|
|
|
71
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Format phone number with country flag or emoji.
|
|
75
|
+
* It takes a phone number string, a country code, and a boolean to indicate whether to use CSS classes for flags.
|
|
76
|
+
* If the phone number is empty, less than 6 characters, or does not match the regex for valid phone numbers,
|
|
77
|
+
* it returns the original value.
|
|
78
|
+
* If the phone number is valid, it formats the phone number using `libphonenumber-js` and returns it with the country flag or emoji.
|
|
79
|
+
* The country flag is represented as a CSS class, and the emoji is obtained from the `Countries` module.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} value - The phone number to format.
|
|
82
|
+
* @param {string} country - The country code to use for formatting.
|
|
83
|
+
* @param {boolean} useCss - Whether to use CSS classes for flags.
|
|
84
|
+
* @returns {string} The formatted phone number with country flag or emoji.
|
|
85
|
+
*/
|
|
72
86
|
export const phone = (value?: string, country = "AE", useCss = false) => {
|
|
73
87
|
if (isEmpty(value) || value.length < 6 || !/^[\d+\s\-()]+$/.test(value))
|
|
74
88
|
return value;
|
|
@@ -80,28 +94,74 @@ export namespace Format {
|
|
|
80
94
|
: Countries.emoji(phone.country ?? "")
|
|
81
95
|
} ${phone.formatInternational()}`;
|
|
82
96
|
};
|
|
83
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Format number using `numeral`.
|
|
99
|
+
* It takes a number or string and a format string, and returns the formatted number.
|
|
100
|
+
* If the number is undefined or null, it returns an empty string.
|
|
101
|
+
* If the number starts with a '+', it retains the '+' sign in the formatted output.
|
|
102
|
+
* This function is useful for displaying numbers in a human-readable format,
|
|
103
|
+
* such as for financial data, statistics, or any numerical representation.
|
|
104
|
+
*
|
|
105
|
+
* @param {string | number} number - number or string to format
|
|
106
|
+
* @param {string} [format] - optional format string, defaults to "0,0[.]00a"
|
|
107
|
+
* @returns {string} formatted number
|
|
108
|
+
*/
|
|
84
109
|
export const number = (number?: string | number, format?: string) => {
|
|
85
110
|
return numberFormat(number, format);
|
|
86
111
|
};
|
|
87
|
-
/**
|
|
112
|
+
/**
|
|
113
|
+
* Format bytes using `numeral`.
|
|
114
|
+
* It takes a number or string and returns the formatted bytes.
|
|
115
|
+
* If the number is undefined or null, it returns an empty string.
|
|
116
|
+
* If the number starts with a '+', it retains the '+' sign in the formatted output.
|
|
117
|
+
* This function is useful for displaying file sizes or data transfer amounts in a human-readable format.
|
|
118
|
+
*
|
|
119
|
+
* @param {string | number} number - number or string to format
|
|
120
|
+
* @returns {string} formatted bytes
|
|
121
|
+
*/
|
|
88
122
|
export const bytes = (number?: string | number) => {
|
|
89
123
|
return numberFormat(number, "0,0[.]00b");
|
|
90
124
|
};
|
|
91
|
-
/**
|
|
125
|
+
/**
|
|
126
|
+
* Format percentage using `numeral`.
|
|
127
|
+
* It takes a number or string and returns the formatted percentage.
|
|
128
|
+
* If the number is undefined or null, it returns an empty string.
|
|
129
|
+
* If the number starts with a '+', it retains the '+' sign in the formatted output.
|
|
130
|
+
*
|
|
131
|
+
* @param {string | number} number - number or string to format
|
|
132
|
+
* @returns {string} formatted percentage
|
|
133
|
+
*/
|
|
92
134
|
export const percent = (number?: string | number) => {
|
|
93
135
|
return numberFormat(number, "0,0[.]00%");
|
|
94
136
|
};
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Format date using `date-fns`.
|
|
139
|
+
* It takes a date (as a Date object, string, or number) and a format string.
|
|
140
|
+
* If the date is undefined or null, it returns an empty string.
|
|
141
|
+
* If the date is a valid date, it formats it using the provided format string.
|
|
142
|
+
* Format defaults to "d-M-y" if not provided.
|
|
143
|
+
*
|
|
144
|
+
* @param {date | string | number} date - Date object, string, or number to format
|
|
145
|
+
* @typedef {string} format - Optional format string, defaults to "d-M-y"
|
|
146
|
+
* @returns {string} - Formatted date string
|
|
147
|
+
*/
|
|
148
|
+
export const date = (date?: Date | string | number, format = "d-M-y") => {
|
|
149
|
+
return date ? dtformat(new Date(date), format) : "";
|
|
98
150
|
};
|
|
99
151
|
/** ***************** format time duration from total seconds/milliseconds *******************/
|
|
100
152
|
/**
|
|
101
153
|
* Format time duration with milliseconds
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
154
|
+
* This function takes a number or string representing a duration in seconds or milliseconds,
|
|
155
|
+
* and returns a formatted string in the format "HH:mm:ss.SSS".
|
|
156
|
+
* If the input is undefined or null, it returns "00:00.000".
|
|
157
|
+
* If the input is Infinity, it returns "∞".
|
|
158
|
+
* The function also accepts an optional boolean parameter `isFraction` to indicate whether the input is in seconds (true) or milliseconds (false).
|
|
159
|
+
* If `isFraction` is true, the input is multiplied by 1000 to convert seconds to milliseconds.
|
|
160
|
+
* The function uses the `makeDuration` helper function to format the duration.
|
|
161
|
+
*
|
|
162
|
+
* @param {string | number} number - The duration to format, can be a string or number.
|
|
163
|
+
* @param {boolean} isFraction - Optional parameter to indicate if the input is in seconds (true) or milliseconds (false).
|
|
164
|
+
* @returns {string} - The formatted duration string.
|
|
105
165
|
*/
|
|
106
166
|
export const duration = (number?: string | number, isFraction?: boolean) => {
|
|
107
167
|
if (isNil(number)) {
|
|
@@ -117,10 +177,19 @@ export namespace Format {
|
|
|
117
177
|
return "00:00.000";
|
|
118
178
|
};
|
|
119
179
|
/**
|
|
120
|
-
* Format time duration
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
180
|
+
* Format time duration in seconds
|
|
181
|
+
* This function takes a number or string representing a duration in seconds,
|
|
182
|
+
* and returns a formatted string in the format "HH:mm:ss".
|
|
183
|
+
* If the input is undefined or null, it returns "00:00".
|
|
184
|
+
* If the input is Infinity, it returns "∞".
|
|
185
|
+
* The function also accepts an optional boolean parameter `isFraction` to indicate whether the input is in seconds (true) or milliseconds (false).
|
|
186
|
+
* If `isFraction` is true, the input is multiplied by 1000 to convert seconds to milliseconds.
|
|
187
|
+
* The function uses the `makeDuration` helper function to format the duration.
|
|
188
|
+
* This function is useful for displaying durations in a human-readable format, such as for timers or elapsed time.
|
|
189
|
+
*
|
|
190
|
+
* @param {string | number} number - The duration to format, can be a string or number.
|
|
191
|
+
* @param {boolean} isFraction - Optional parameter to indicate if the input is in seconds (true) or milliseconds (false).
|
|
192
|
+
* @returns {string} - The formatted duration string.
|
|
124
193
|
*/
|
|
125
194
|
export const durationSeconds = (
|
|
126
195
|
number?: string | number,
|
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,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
|
-
|
|
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(
|
|
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
|
};
|