@react-fabric/utilities 1.0.0-beta.5 → 1.0.0-beta.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/_ascii.js +34 -9
- package/dist/cjs/_boundingBox.js +16 -4
- package/dist/cjs/_countries.js +39 -21
- package/dist/cjs/_debounce.js +8 -2
- package/dist/cjs/_dedupe.js +48 -2
- package/dist/cjs/_fetch.js +12 -7
- package/dist/cjs/_fileType.js +16 -0
- package/dist/cjs/_format.js +19 -6
- package/dist/cjs/_getByPath.js +16 -5
- package/dist/cjs/_hash.js +13 -7
- package/dist/cjs/_interpolate.js +18 -2
- package/dist/cjs/_isEqual.js +40 -6
- package/dist/cjs/_isType.js +121 -0
- package/dist/cjs/_lonlat.js +87 -0
- package/dist/cjs/_mergeRefs.js +11 -0
- package/dist/cjs/_tokenize.js +17 -4
- package/dist/cjs/getImageColorset.js +17 -2
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/useLogger.js +9 -1
- package/dist/esm/_ascii.js +34 -9
- package/dist/esm/_boundingBox.js +16 -4
- package/dist/esm/_countries.js +39 -21
- package/dist/esm/_debounce.js +8 -2
- package/dist/esm/_dedupe.js +48 -2
- package/dist/esm/_fetch.js +12 -7
- package/dist/esm/_fileType.js +16 -0
- package/dist/esm/_format.js +19 -6
- package/dist/esm/_getByPath.js +16 -5
- package/dist/esm/_hash.js +13 -7
- package/dist/esm/_interpolate.js +18 -2
- package/dist/esm/_isEqual.js +41 -7
- package/dist/esm/_isType.js +121 -0
- package/dist/esm/_lonlat.js +86 -0
- package/dist/esm/_mergeRefs.js +11 -0
- package/dist/esm/_tokenize.js +17 -4
- package/dist/esm/getImageColorset.js +17 -2
- package/dist/esm/index.js +1 -0
- package/dist/esm/useLogger.js +9 -1
- package/dist/types/_ascii.d.ts +7 -2
- package/dist/types/_boundingBox.d.ts +16 -4
- package/dist/types/_countries.d.ts +39 -21
- package/dist/types/_debounce.d.ts +8 -2
- package/dist/types/_dedupe.d.ts +47 -4
- package/dist/types/_fetch.d.ts +12 -9
- package/dist/types/_fileType.d.ts +16 -0
- package/dist/types/_format.d.ts +84 -13
- package/dist/types/_getByPath.d.ts +15 -4
- package/dist/types/_hash.d.ts +12 -6
- package/dist/types/_interpolate.d.ts +17 -2
- package/dist/types/_isEqual.d.ts +37 -6
- package/dist/types/_isType.d.ts +111 -3
- package/dist/types/_lonlat.d.ts +45 -0
- package/dist/types/_mergeRefs.d.ts +11 -0
- package/dist/types/_tokenize.d.ts +16 -4
- package/dist/types/getImageColorset.d.ts +15 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/useLogger.d.ts +9 -1
- package/package.json +4 -4
- package/src/_ascii.ts +10 -5
- package/src/_boundingBox.ts +16 -4
- package/src/_countries.ts +39 -21
- package/src/_debounce.ts +8 -2
- package/src/_dedupe.ts +51 -5
- package/src/_fetch.ts +12 -7
- package/src/_fileType.ts +16 -0
- package/src/_format.ts +87 -18
- package/src/_getByPath.ts +18 -7
- package/src/_hash.ts +13 -7
- package/src/_interpolate.ts +17 -2
- package/src/_isEqual.ts +41 -7
- package/src/_isType.ts +113 -1
- package/src/_lonlat.ts +124 -0
- package/src/_mergeRefs.ts +11 -0
- package/src/_tokenize.ts +16 -4
- package/src/getImageColorset.ts +17 -2
- package/src/index.ts +1 -0
- package/src/useLogger.ts +9 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
type LngLatLike = {
|
|
2
|
+
lng: number;
|
|
3
|
+
lat: number;
|
|
4
|
+
} | {
|
|
5
|
+
lon: number;
|
|
6
|
+
lat: number;
|
|
7
|
+
} | {
|
|
8
|
+
longitude: number;
|
|
9
|
+
latitude: number;
|
|
10
|
+
} | [lon: number | string, lat: number | string] | string;
|
|
11
|
+
/**
|
|
12
|
+
* Convert various formats of longitude and latitude to a tuple of [lon, lat].
|
|
13
|
+
* This function supports:
|
|
14
|
+
* - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
|
|
15
|
+
* - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
|
|
16
|
+
* - Strings in the format "lat,lon"
|
|
17
|
+
* The function will return a tuple of numbers [lon, lat].
|
|
18
|
+
*
|
|
19
|
+
* @param {LngLatLike} lonLat - The input longitude and latitude in various formats.
|
|
20
|
+
* @returns { [lon: number, lat: number] } A tuple of [lon, lat] where lon and lat are numbers.
|
|
21
|
+
* @throws Error if the input is not a valid format.
|
|
22
|
+
*/
|
|
23
|
+
export declare const convertLatLng: (lonLat: LngLatLike) => [lon: number, lat: number];
|
|
24
|
+
/**
|
|
25
|
+
* Convert a latitude and longitude to a human-readable text format.
|
|
26
|
+
* This function takes a latitude and longitude in various formats and
|
|
27
|
+
* returns a string in the format "degrees˚minutes'seconds" with direction (N/S, E/W).
|
|
28
|
+
*
|
|
29
|
+
* This function supports:
|
|
30
|
+
*
|
|
31
|
+
* - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
|
|
32
|
+
* - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
|
|
33
|
+
* - Strings in the format "lat,lon"
|
|
34
|
+
*
|
|
35
|
+
* @param {LngLatLike} latlon - The latitude and longitude in various formats.
|
|
36
|
+
* @returns {string} A string representation of the location in DMS format.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```jsx
|
|
40
|
+
* const location = getLocationAsText({ lat: 40.7128, lng: -74.0060 });
|
|
41
|
+
* console.log(location); // "40˚42'51.36"N, 74˚0'21.60"W"
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const getLocationAsText: (latlon: LngLatLike) => string;
|
|
45
|
+
export {};
|
|
@@ -1 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge multiple refs into a single ref callback.
|
|
3
|
+
* This function allows you to combine multiple refs (either callback refs or React refs)
|
|
4
|
+
* into a single ref callback that can be used in React components.
|
|
5
|
+
* This is particularly useful when you want to assign a single element to multiple refs,
|
|
6
|
+
* such as when using third-party libraries that require refs or when you want to share refs between components.
|
|
7
|
+
* The function iterates over the provided refs and assigns the element to each ref.
|
|
8
|
+
*
|
|
9
|
+
* @param {AnyObject[]} refs - An array of refs to merge.
|
|
10
|
+
* @returns {function} A function that takes an element and assigns it to all provided refs.
|
|
11
|
+
*/
|
|
1
12
|
export declare const mergeRefs: (...refs: AnyObject[]) => (el: AnyObject) => void;
|
|
@@ -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;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./_hash";
|
|
|
13
13
|
export * from "./_interpolate";
|
|
14
14
|
export * from "./_isEqual";
|
|
15
15
|
export * from "./_isType";
|
|
16
|
+
export * from "./_lonlat";
|
|
16
17
|
export * from "./_mergeRefs";
|
|
17
18
|
export * from "./_tokenize";
|
|
18
19
|
export { getImageColorset } from "./getImageColorset";
|
|
@@ -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",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"watch": "npm-run-all -p watch:rollup watch:dts"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"date-fns": "^4.
|
|
36
|
-
"libphonenumber-js": "^1.
|
|
35
|
+
"date-fns": "^4.1.0",
|
|
36
|
+
"libphonenumber-js": "^1.12.8",
|
|
37
37
|
"numeral": "^2.0.6",
|
|
38
38
|
"yup": "^1.4.0"
|
|
39
39
|
},
|
|
@@ -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,14 +80,19 @@ 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 "";
|
|
88
|
-
Object.entries(conversions).forEach((
|
|
89
|
-
const re = new RegExp(
|
|
90
|
-
str = `${str}`.replace(re,
|
|
93
|
+
Object.entries(conversions).forEach((entry) => {
|
|
94
|
+
const re = new RegExp(entry[1], "g");
|
|
95
|
+
str = `${str}`.replace(re, entry[0]);
|
|
91
96
|
});
|
|
92
97
|
return str;
|
|
93
98
|
};
|
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,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
|
+
/**
|
|
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,
|