@react-fabric/utilities 1.0.0-beta.50 → 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 +7 -2
- 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 +47 -0
- 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 +15 -4
- package/dist/cjs/_hash.js +12 -6
- package/dist/cjs/_interpolate.js +18 -2
- package/dist/cjs/_isEqual.js +37 -6
- package/dist/cjs/_isType.js +121 -0
- package/dist/cjs/_lonlat.js +46 -0
- package/dist/cjs/_mergeRefs.js +11 -0
- package/dist/cjs/_tokenize.js +17 -4
- package/dist/cjs/getImageColorset.js +15 -0
- package/dist/cjs/useLogger.js +9 -1
- package/dist/esm/_ascii.js +7 -2
- 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 +47 -0
- 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 +15 -4
- package/dist/esm/_hash.js +12 -6
- package/dist/esm/_interpolate.js +18 -2
- package/dist/esm/_isEqual.js +37 -6
- package/dist/esm/_isType.js +121 -0
- package/dist/esm/_lonlat.js +46 -0
- package/dist/esm/_mergeRefs.js +11 -0
- package/dist/esm/_tokenize.js +17 -4
- package/dist/esm/getImageColorset.js +15 -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 +110 -2
- package/dist/types/_lonlat.d.ts +32 -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/useLogger.d.ts +9 -1
- package/package.json +2 -2
- package/src/_ascii.ts +7 -2
- package/src/_boundingBox.ts +16 -4
- package/src/_countries.ts +39 -21
- package/src/_debounce.ts +8 -2
- package/src/_dedupe.ts +43 -0
- package/src/_fetch.ts +12 -7
- package/src/_fileType.ts +16 -0
- package/src/_format.ts +87 -18
- package/src/_getByPath.ts +15 -4
- package/src/_hash.ts +12 -6
- package/src/_interpolate.ts +17 -2
- package/src/_isEqual.ts +37 -6
- package/src/_isType.ts +112 -0
- package/src/_lonlat.ts +44 -0
- package/src/_mergeRefs.ts +11 -0
- package/src/_tokenize.ts +16 -4
- package/src/getImageColorset.ts +15 -0
- package/src/useLogger.ts +9 -1
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Tokenize a string based on provided keywords.
|
|
3
|
+
* This function splits the input string into tokens, where each token is an array containing
|
|
4
|
+
* the substring before the keyword and the keyword itself.
|
|
5
|
+
* If no keywords are provided, it returns the entire string as a single token.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} str - The string to tokenize.
|
|
8
|
+
* @param {string | string[]} keywords - A string or an array of strings to use as keywords for tokenization.
|
|
9
|
+
* @returns {Array<[string, string]>} An array of tokens, where each token is an array of [substring, keyword].
|
|
6
10
|
*/
|
|
7
11
|
export declare const tokenize: (str: string, keywords?: string | string[]) => string[][];
|
|
12
|
+
/**
|
|
13
|
+
* Generate a token from a string.
|
|
14
|
+
* This function creates a token by taking the first letter of the first word and the first letter of the last word,
|
|
15
|
+
* or the first two letters of the first word if there is no last word.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} str - The string to tokenize.
|
|
18
|
+
* @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.
|
|
19
|
+
*/
|
|
8
20
|
export declare const iconToken: (str: string) => string;
|
|
@@ -1 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the color set of an image element.
|
|
3
|
+
* This function analyzes the pixel data of an image to determine if it is predominantly light or dark.
|
|
4
|
+
* It returns "light", "dark", or "light_transparent"/"dark_transparent" based on the pixel analysis.
|
|
5
|
+
* It uses a canvas to draw the image and extract pixel data for analysis.
|
|
6
|
+
* * The function calculates the brightness of each pixel using the formula:
|
|
7
|
+
* brightness = 0.2126 * R + 0.7152 * G + 0.0722 * B
|
|
8
|
+
* * It counts the number of light and dark pixels, and determines the overall color set based on the ratio of light to dark pixels.
|
|
9
|
+
* * If the image has transparent pixels, it appends "_transparent" to the result.
|
|
10
|
+
* * This function is useful for determining the color scheme of an image, which can be helpful for UI design,
|
|
11
|
+
* theming, and accessibility considerations.
|
|
12
|
+
*
|
|
13
|
+
* @param {HTMLImageElement} el - The HTMLImageElement to analyze.
|
|
14
|
+
* @returns {string} A string indicating the color set of the image.
|
|
15
|
+
*/
|
|
1
16
|
export declare const getImageColorset: (el: HTMLImageElement) => string;
|
|
@@ -5,7 +5,15 @@ interface LogDetail {
|
|
|
5
5
|
totalTime?: number;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* useLogger is a custom hook that provides logging functionality.
|
|
9
|
+
* It allows you to log messages with different levels (log, debug, info, error, warning)
|
|
10
|
+
* and also provides a timer function to measure the duration of processes.
|
|
11
|
+
* The logs can include additional details such as the module name, process name, duration, and total time.
|
|
12
|
+
* It is designed to work in both Node.js and browser environments, adapting the log format accordingly.
|
|
13
|
+
* It uses ANSI escape codes for terminal colors in Node.js and CSS styles for console logs in the browser.
|
|
14
|
+
*
|
|
15
|
+
* @param mod - Optional module name to include in the log details.
|
|
16
|
+
* @returns An object with methods for logging messages and a timer function.
|
|
9
17
|
*/
|
|
10
18
|
export declare const useLogger: (mod?: string) => {
|
|
11
19
|
timer(process: string): {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-fabric/utilities",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.52",
|
|
4
4
|
"description": "RUF: Utility package",
|
|
5
5
|
"author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"node": ">=18.12.0",
|
|
45
45
|
"npm": ">=8.19.0"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "0eabb9d88d91af93d657bcc209b226fd576e8260"
|
|
48
48
|
}
|
package/src/_ascii.ts
CHANGED
|
@@ -80,8 +80,13 @@ const conversions = {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
83
|
+
* Converts a string to its ASCII representation by replacing
|
|
84
|
+
* special characters with their ASCII equivalents.
|
|
85
|
+
* This function is useful for normalizing strings for search or display purposes.
|
|
86
|
+
* It replaces characters like "Ä" with "Ae", "ß" with "ss", etc.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} str - The input string to be converted.
|
|
89
|
+
* @returns {string} - The ASCII representation of the input string.
|
|
85
90
|
*/
|
|
86
91
|
export const ascii = (str: string) => {
|
|
87
92
|
if (isEmpty(str)) return "";
|
package/src/_boundingBox.ts
CHANGED
|
@@ -38,8 +38,13 @@ const _getBox = (
|
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* box
|
|
42
|
-
*
|
|
41
|
+
* Get bounding box from a string or array.
|
|
42
|
+
* The box can be in the format "x,y,w,h" or [x,y,w,h].
|
|
43
|
+
* The box is adjusted to ensure that x and y are non-negative.
|
|
44
|
+
* The width and height are adjusted accordingly.
|
|
45
|
+
*
|
|
46
|
+
* @param box "x,y,w,h" | [x,y,w,h]
|
|
47
|
+
* @returns canvas box [x,y,w,h]
|
|
43
48
|
*/
|
|
44
49
|
export const getBox = (
|
|
45
50
|
box: string | [number, number, number, number] = "0,0,0,0",
|
|
@@ -55,8 +60,15 @@ export const getBox = (
|
|
|
55
60
|
};
|
|
56
61
|
|
|
57
62
|
/**
|
|
58
|
-
* box
|
|
59
|
-
*
|
|
63
|
+
* Get bounding box from a string or array.
|
|
64
|
+
* The box can be in the format "x1,y1,x2,y2" or [x1,y1,x2,y2].
|
|
65
|
+
* The box is adjusted to ensure that x1 and y1 are non-negative.
|
|
66
|
+
* The width and height are calculated as x2 - x1 and y2 - y1 respectively.
|
|
67
|
+
* If x1 or y1 are negative, they are set to 0 and the width and height are adjusted accordingly.
|
|
68
|
+
* This is useful for ensuring that the bounding box is always within the canvas area.
|
|
69
|
+
*
|
|
70
|
+
* @param box "x1,y1,x2,y2" | [x1,y1,x2,y2]
|
|
71
|
+
* @returns canvas box [x,y,w,h]
|
|
60
72
|
*/
|
|
61
73
|
export const getBoundingBox = (
|
|
62
74
|
box: string | [number, number, number, number] = "0,0,0,0",
|
package/src/_countries.ts
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
/* istanbul ignore file */
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* Country interface
|
|
28
28
|
*/
|
|
29
29
|
export interface Country {
|
|
30
30
|
continent: string;
|
|
@@ -42,28 +42,38 @@ export interface Country {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Country list with helper functions
|
|
46
|
+
* This is a list of all countries with their details.
|
|
47
|
+
* It includes information such as continent, ISO codes, name, full name, capital, top-level domain (TLD), currency, phone code, and emoji.
|
|
48
|
+
* This list can be used to retrieve country information based on various codes or names.
|
|
49
|
+
* @type {Country[]}
|
|
46
50
|
*/
|
|
47
51
|
export default {
|
|
48
52
|
/**
|
|
49
|
-
* Get
|
|
50
|
-
*
|
|
53
|
+
* Get ISO2 country code
|
|
54
|
+
*
|
|
55
|
+
* @param code - The country code to search for. It can be an ISO2 code, ISO3 code, or an alternative code.
|
|
56
|
+
* @return {string | undefined} - Returns the ISO2 country code if found, otherwise undefined.
|
|
51
57
|
*/
|
|
52
58
|
toIso2(code: string) {
|
|
53
59
|
const ctry = this.find(code);
|
|
54
60
|
return ctry != null ? ctry.iso2 : undefined;
|
|
55
61
|
},
|
|
56
62
|
/**
|
|
57
|
-
* Get
|
|
58
|
-
*
|
|
63
|
+
* Get ISO3 country code
|
|
64
|
+
*
|
|
65
|
+
* @param code - The country code to search for. It can be an ISO2 code, ISO3 code, or an alternative code.
|
|
66
|
+
* @return {string | undefined} - Returns the ISO3 country code if found, otherwise undefined.
|
|
59
67
|
*/
|
|
60
68
|
toIso3(code: string) {
|
|
61
69
|
const ctry = this.find(code);
|
|
62
70
|
return ctry != null ? ctry.iso3 : undefined;
|
|
63
71
|
},
|
|
64
72
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
73
|
+
* Get country emoji
|
|
74
|
+
*
|
|
75
|
+
* @param code - The country code to search for. It can be an ISO2 code, ISO3 code, or an alternative code.
|
|
76
|
+
* @return {string} - Returns the emoji representation of the country if found, otherwise returns a default flag emoji.
|
|
67
77
|
*/
|
|
68
78
|
emoji(code: string) {
|
|
69
79
|
const ctry = this.find(code);
|
|
@@ -71,25 +81,25 @@ export default {
|
|
|
71
81
|
},
|
|
72
82
|
/**
|
|
73
83
|
* Get country name
|
|
74
|
-
*
|
|
84
|
+
*
|
|
85
|
+
* @param code - The country code to search for. It can be an ISO2 code, ISO3 code, or an alternative code.
|
|
86
|
+
* @return {string} - Returns the name of the country if found, otherwise returns the code itself.
|
|
75
87
|
*/
|
|
76
88
|
name: function (code: string) {
|
|
77
89
|
const ctry = this.find(code);
|
|
78
90
|
return ctry != null ? ctry.name : code;
|
|
79
91
|
},
|
|
80
92
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
90
|
-
* @return
|
|
91
|
-
* @return phone: string;
|
|
92
|
-
* @return emoji: string;
|
|
93
|
+
* This function searches for a country in the list by its code.
|
|
94
|
+
* It supports various formats:
|
|
95
|
+
* - ISO2 code (e.g., "US")
|
|
96
|
+
* - ISO3 code (e.g., "USA")
|
|
97
|
+
* - Alternative code (e.g., "US", "USA", "United States")
|
|
98
|
+
* It returns the first matching country object or undefined if not found.
|
|
99
|
+
* This function is useful for retrieving country details based on different code formats.
|
|
100
|
+
*
|
|
101
|
+
* @param code - The country code to search for. It can be an ISO2 code, ISO3 code, or an alternative code.
|
|
102
|
+
* @return {Country | undefined} - Returns the country object if found, otherwise undefined.
|
|
93
103
|
*/
|
|
94
104
|
find(code: string): Country | undefined {
|
|
95
105
|
return this.list.find(
|
|
@@ -99,6 +109,14 @@ export default {
|
|
|
99
109
|
ct.iso2.toLowerCase() === code.toLowerCase(),
|
|
100
110
|
);
|
|
101
111
|
},
|
|
112
|
+
/**
|
|
113
|
+
* Country list
|
|
114
|
+
* This is a list of all countries with their details.
|
|
115
|
+
* It includes information such as continent, ISO codes, name, full name, capital, top-level domain (TLD), currency, phone code, and emoji.
|
|
116
|
+
* This list can be used to retrieve country information based on various codes or names.
|
|
117
|
+
*
|
|
118
|
+
* @type {Country[]}
|
|
119
|
+
*/
|
|
102
120
|
list: [
|
|
103
121
|
{
|
|
104
122
|
continent: "Asia",
|
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,6 +23,17 @@
|
|
|
23
23
|
|
|
24
24
|
import { isEmpty } from "./_isType";
|
|
25
25
|
|
|
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
|
+
*/
|
|
26
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) {
|
|
@@ -34,10 +45,31 @@ export const dedupe = <T extends AnyObject>(list: T[], key?: keyof T) => {
|
|
|
34
45
|
);
|
|
35
46
|
};
|
|
36
47
|
|
|
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
|
+
*/
|
|
37
57
|
export const flatten = <T extends AnyObject>(list: T[]) => {
|
|
38
58
|
return list.flat(5).filter((i) => !isEmpty(i));
|
|
39
59
|
};
|
|
40
60
|
|
|
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
|
+
*/
|
|
41
73
|
export const flattenAndDedupe = <T extends AnyObject>(
|
|
42
74
|
list: T[][] = [],
|
|
43
75
|
key?: keyof T,
|
|
@@ -45,6 +77,17 @@ export const flattenAndDedupe = <T extends AnyObject>(
|
|
|
45
77
|
return dedupe(flatten(list) as T[], key);
|
|
46
78
|
};
|
|
47
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
|
+
*/
|
|
48
91
|
export const groupBy = <T extends KeyValue>(
|
|
49
92
|
list: T[],
|
|
50
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
|
+
/**
|
|
34
|
+
* Format utility functions for various data types.
|
|
35
|
+
* This namespace provides methods to format phone numbers, numbers, bytes, percentages, dates, and durations.
|
|
36
|
+
*/
|
|
33
37
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
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
|
@@ -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
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);
|