@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.
- package/dist/cjs/_ascii.js +34 -9
- package/dist/cjs/_boundingBox.js +16 -4
- package/dist/cjs/_countries.js +61 -2769
- 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 +20 -8
- 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 +61 -2769
- 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 +20 -8
- 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 +87 -60
- 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 +8 -7
- package/src/_ascii.ts +10 -5
- package/src/_boundingBox.ts +16 -4
- package/src/_countries.ts +63 -2986
- 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 +89 -20
- 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/_debounce.ts
CHANGED
|
@@ -24,8 +24,14 @@
|
|
|
24
24
|
/* istanbul ignore file */
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
27
|
+
* Debounce function to limit the rate at which a function can fire.
|
|
28
|
+
* It will delay the execution of the function until after a specified timeout
|
|
29
|
+
* has elapsed since the last time the function was invoked.
|
|
30
|
+
*
|
|
31
|
+
* @param fn - The function to debounce.
|
|
32
|
+
* @param timeout - The number of milliseconds to wait before invoking the function.
|
|
33
|
+
* @returns A debounced version of the function that can be called with any arguments.
|
|
34
|
+
* The debounced function also has a `cancel` method to clear the timeout.
|
|
29
35
|
*/
|
|
30
36
|
export const debounce = (fn: (...rest: any) => any, timeout = 500) => {
|
|
31
37
|
/** ***************** create debounce timer id using callback hash *******************/
|
package/src/_dedupe.ts
CHANGED
|
@@ -23,25 +23,71 @@
|
|
|
23
23
|
|
|
24
24
|
import { isEmpty } from "./_isType";
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Remove duplicates from a list of objects based on a key or the entire object.
|
|
28
|
+
* If a key is provided, it will deduplicate based on the value of that key.
|
|
29
|
+
* If no key is provided, it will deduplicate based on the entire object.
|
|
30
|
+
* This is useful for normalizing data structures that may have nested arrays or objects.
|
|
31
|
+
*
|
|
32
|
+
* @param list
|
|
33
|
+
* @param key
|
|
34
|
+
* @typedef {T} AnyObject - A generic type representing list items.
|
|
35
|
+
* @returns {T[]} - A new array with duplicates removed.
|
|
36
|
+
*/
|
|
37
|
+
export const dedupe = <T extends AnyObject>(list: T[], key?: keyof T) => {
|
|
27
38
|
const filtered = list.filter((i) => !isEmpty(i));
|
|
28
39
|
if (!key) {
|
|
29
40
|
return Array.from(new Set(filtered).values());
|
|
30
41
|
}
|
|
31
42
|
|
|
32
43
|
return Array.from(
|
|
33
|
-
new Map(filtered.map((obj) => [obj[key]
|
|
44
|
+
new Map(filtered.map((obj) => [(obj as KeyValue)[key], obj])).values(),
|
|
34
45
|
);
|
|
35
46
|
};
|
|
36
47
|
|
|
37
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Flatten a list of objects, removing any empty objects.
|
|
50
|
+
* It will flatten up to 5 levels deep and filter out any empty objects.
|
|
51
|
+
* This is useful for normalizing data structures that may have nested arrays or objects.
|
|
52
|
+
*
|
|
53
|
+
* @param list
|
|
54
|
+
* @typedef {T} AnyObject - A generic type representing list items.
|
|
55
|
+
* @returns {T[]} - A flattened array of objects.
|
|
56
|
+
*/
|
|
57
|
+
export const flatten = <T extends AnyObject>(list: T[]) => {
|
|
38
58
|
return list.flat(5).filter((i) => !isEmpty(i));
|
|
39
59
|
};
|
|
40
60
|
|
|
41
|
-
|
|
42
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Flatten a list of objects and remove duplicates based on a key or the entire object.
|
|
63
|
+
* If a key is provided, it will deduplicate based on the value of that key.
|
|
64
|
+
* If no key is provided, it will deduplicate based on the entire object.
|
|
65
|
+
* This is useful for normalizing data structures that may have nested arrays or objects.
|
|
66
|
+
* Flattening is done first, followed by deduplication.
|
|
67
|
+
*
|
|
68
|
+
* @param list
|
|
69
|
+
* @param key
|
|
70
|
+
* @typedef {T} AnyObject - A generic type representing list items.
|
|
71
|
+
* @returns {T[]} - A new array with duplicates removed.
|
|
72
|
+
*/
|
|
73
|
+
export const flattenAndDedupe = <T extends AnyObject>(
|
|
74
|
+
list: T[][] = [],
|
|
75
|
+
key?: keyof T,
|
|
76
|
+
) => {
|
|
77
|
+
return dedupe(flatten(list) as T[], key);
|
|
43
78
|
};
|
|
44
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Group a list of objects by a specific property.
|
|
82
|
+
* If the property is not present in an object, it will use the `missingKey` value as the key.
|
|
83
|
+
* This is useful for organizing data into categories based on a specific property.
|
|
84
|
+
*
|
|
85
|
+
* @param list
|
|
86
|
+
* @param prop
|
|
87
|
+
* @param missingKey
|
|
88
|
+
* @typedef {T} KeyValue - A generic type representing list items with key-value pairs.
|
|
89
|
+
* @returns {KeyValue<T[]>} - An object mapping keys to arrays of items.
|
|
90
|
+
*/
|
|
45
91
|
export const groupBy = <T extends KeyValue>(
|
|
46
92
|
list: T[],
|
|
47
93
|
prop: keyof T,
|
package/src/_fetch.ts
CHANGED
|
@@ -35,13 +35,18 @@ declare interface FetchOptions {
|
|
|
35
35
|
const _signals = new Map<string, AbortController>();
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Fetch
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @param
|
|
44
|
-
* @
|
|
38
|
+
* Fetch utility function to make HTTP requests.
|
|
39
|
+
* It supports GET, POST, PUT, and DELETE methods.
|
|
40
|
+
* It automatically handles JSON responses and error codes.
|
|
41
|
+
* It also supports aborting requests using an AbortController.
|
|
42
|
+
*
|
|
43
|
+
* @param method - The HTTP method to use (GET, POST, PUT, DELETE).
|
|
44
|
+
* @param url - The URL to send the request to.
|
|
45
|
+
* @param options.headers - Headers to include in the request.
|
|
46
|
+
* @param options.body - The body of the request (for POST and PUT requests).
|
|
47
|
+
* @param options.signal - An AbortSignal to cancel the request.
|
|
48
|
+
* @returns A promise that resolves to the JSON response from the server.
|
|
49
|
+
* @throws An error if the request fails or if the response contains an error code.
|
|
45
50
|
*/
|
|
46
51
|
export const _fetch = async (
|
|
47
52
|
method: METHODS,
|
package/src/_fileType.ts
CHANGED
|
@@ -23,7 +23,20 @@
|
|
|
23
23
|
|
|
24
24
|
/* istanbul ignore file */
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* File type utility to get the file type from a MIME type.
|
|
28
|
+
* This utility provides a mapping of common MIME types to their human-readable names.
|
|
29
|
+
* It can be used to display file types in a user-friendly manner.
|
|
30
|
+
*/
|
|
26
31
|
export default {
|
|
32
|
+
/**
|
|
33
|
+
* Get the human-readable file type from a MIME type.
|
|
34
|
+
* This method looks up the MIME type in a predefined list
|
|
35
|
+
* and returns a user-friendly name.
|
|
36
|
+
*
|
|
37
|
+
* @param mime MIME type string, e.g. "application/pdf"
|
|
38
|
+
* @returns {string} - The human-readable file type.
|
|
39
|
+
*/
|
|
27
40
|
mime(mime: string = "") {
|
|
28
41
|
const ret = (this.mimeList as KeyValue)[mime.split(";")[0]];
|
|
29
42
|
if (!ret && !!mime) {
|
|
@@ -32,6 +45,9 @@ export default {
|
|
|
32
45
|
}
|
|
33
46
|
return ret || "Unknown type";
|
|
34
47
|
},
|
|
48
|
+
/**
|
|
49
|
+
* List of common MIME types and their human-readable names.
|
|
50
|
+
*/
|
|
35
51
|
mimeList: {
|
|
36
52
|
"application/x-tika-msoffice": "Microsft Office File",
|
|
37
53
|
"application/x-tika-ooxml": "Open Office File",
|
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;
|
|
@@ -76,32 +90,78 @@ export namespace Format {
|
|
|
76
90
|
/* istanbul ignore next */
|
|
77
91
|
return `${
|
|
78
92
|
useCss
|
|
79
|
-
? `<span class="
|
|
93
|
+
? `<span class="icon-[circle-flags--${phone.country}"></span>`
|
|
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) =>
|