@react-fabric/utilities 1.0.0-beta.6 → 1.0.0-beta.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/cjs/_ascii.js +34 -9
  2. package/dist/cjs/_boundingBox.js +16 -4
  3. package/dist/cjs/_countries.js +39 -21
  4. package/dist/cjs/_debounce.js +8 -2
  5. package/dist/cjs/_dedupe.js +48 -2
  6. package/dist/cjs/_fetch.js +12 -7
  7. package/dist/cjs/_fileType.js +16 -0
  8. package/dist/cjs/_format.js +19 -7
  9. package/dist/cjs/_getByPath.js +16 -5
  10. package/dist/cjs/_hash.js +13 -7
  11. package/dist/cjs/_interpolate.js +18 -2
  12. package/dist/cjs/_isEqual.js +40 -6
  13. package/dist/cjs/_isType.js +126 -1
  14. package/dist/cjs/_lonlat.js +102 -0
  15. package/dist/cjs/_mergeRefs.js +11 -0
  16. package/dist/cjs/_tokenize.js +17 -4
  17. package/dist/cjs/getImageColorset.js +17 -2
  18. package/dist/cjs/index.js +5 -0
  19. package/dist/cjs/useLogger.js +9 -1
  20. package/dist/esm/_ascii.js +34 -9
  21. package/dist/esm/_boundingBox.js +16 -4
  22. package/dist/esm/_countries.js +39 -21
  23. package/dist/esm/_debounce.js +8 -2
  24. package/dist/esm/_dedupe.js +48 -2
  25. package/dist/esm/_fetch.js +12 -7
  26. package/dist/esm/_fileType.js +16 -0
  27. package/dist/esm/_format.js +19 -7
  28. package/dist/esm/_getByPath.js +16 -5
  29. package/dist/esm/_hash.js +13 -7
  30. package/dist/esm/_interpolate.js +18 -2
  31. package/dist/esm/_isEqual.js +41 -7
  32. package/dist/esm/_isType.js +126 -2
  33. package/dist/esm/_lonlat.js +100 -0
  34. package/dist/esm/_mergeRefs.js +11 -0
  35. package/dist/esm/_tokenize.js +17 -4
  36. package/dist/esm/getImageColorset.js +17 -2
  37. package/dist/esm/index.js +2 -1
  38. package/dist/esm/useLogger.js +9 -1
  39. package/dist/types/_ascii.d.ts +7 -2
  40. package/dist/types/_boundingBox.d.ts +16 -4
  41. package/dist/types/_countries.d.ts +39 -21
  42. package/dist/types/_debounce.d.ts +8 -2
  43. package/dist/types/_dedupe.d.ts +47 -4
  44. package/dist/types/_fetch.d.ts +12 -9
  45. package/dist/types/_fileType.d.ts +16 -0
  46. package/dist/types/_format.d.ts +84 -13
  47. package/dist/types/_getByPath.d.ts +15 -4
  48. package/dist/types/_hash.d.ts +12 -6
  49. package/dist/types/_interpolate.d.ts +17 -2
  50. package/dist/types/_isEqual.d.ts +37 -6
  51. package/dist/types/_isType.d.ts +112 -3
  52. package/dist/types/_lonlat.d.ts +48 -0
  53. package/dist/types/_mergeRefs.d.ts +11 -0
  54. package/dist/types/_tokenize.d.ts +16 -4
  55. package/dist/types/getImageColorset.d.ts +15 -0
  56. package/dist/types/index.d.ts +1 -0
  57. package/dist/types/useLogger.d.ts +9 -1
  58. package/package.json +7 -7
  59. package/src/_ascii.ts +10 -5
  60. package/src/_boundingBox.ts +16 -4
  61. package/src/_countries.ts +39 -21
  62. package/src/_debounce.ts +8 -2
  63. package/src/_dedupe.ts +51 -5
  64. package/src/_fetch.ts +12 -7
  65. package/src/_fileType.ts +16 -0
  66. package/src/_format.ts +88 -19
  67. package/src/_getByPath.ts +18 -7
  68. package/src/_hash.ts +13 -7
  69. package/src/_interpolate.ts +17 -2
  70. package/src/_isEqual.ts +41 -7
  71. package/src/_isType.ts +116 -2
  72. package/src/_lonlat.ts +135 -0
  73. package/src/_mergeRefs.ts +11 -0
  74. package/src/_tokenize.ts +16 -4
  75. package/src/getImageColorset.ts +17 -2
  76. package/src/index.ts +1 -0
  77. package/src/useLogger.ts +9 -1
@@ -1,14 +1,123 @@
1
+ /**
2
+ * Check if a value is undefined.
3
+ * @param value - The value to check.
4
+ * @returns true if the value is undefined, false otherwise.
5
+ */
1
6
  export declare const isUndefined: (value: AnyObject) => value is undefined;
7
+ /**
8
+ * Check if a value is null.
9
+ * @param value - The value to check.
10
+ * @returns true if the value is null, false otherwise.
11
+ */
2
12
  export declare const isNull: (value: AnyObject) => value is null;
3
- export declare const isNil: (val: AnyObject) => val is undefined;
13
+ /**
14
+ * Check if a value is either null or undefined.
15
+ * @param val - The value to check.
16
+ * @returns true if the value is null or undefined, false otherwise.
17
+ */
18
+ export declare const isNil: (val: AnyObject) => val is null | undefined;
19
+ export declare const isDefined: (val: AnyObject) => boolean;
20
+ /**
21
+ * Check if a value is empty.
22
+ * A value is considered empty if it is:
23
+ * - undefined
24
+ * - null
25
+ * - an empty string
26
+ * - an empty array
27
+ * - an empty object
28
+ * - an empty Map or Set
29
+ * - a boolean, number, or bigint (not empty)
30
+ * @param val - The value to check.
31
+ * @returns true if the value is empty, false otherwise.
32
+ */
4
33
  export declare const isEmpty: (val: AnyObject) => val is undefined;
5
- export declare const isObject: (val: AnyObject) => val is KeyValue<any>;
6
- export declare const isArray: <T = any>(val: AnyObject) => val is T[];
34
+ /**
35
+ * Check if a value is an object.
36
+ * An object is considered valid if it is:
37
+ * - not null or undefined
38
+ * - not a Date instance
39
+ * - of type 'object'
40
+ * - not an array
41
+ * @param val - The value to check.
42
+ * @returns true if the value is an object, false otherwise.
43
+ */
44
+ export declare const isObject: (val: AnyObject) => val is KeyValue;
45
+ /**
46
+ * Check if a value is an array.
47
+ * An array is considered valid if it is:
48
+ * - not null or undefined
49
+ * - an instance of Array
50
+ * @param val - The value to check.
51
+ * @returns true if the value is an array, false otherwise.
52
+ */
53
+ export declare const isArray: <T = AnyObject>(val: AnyObject) => val is T[];
54
+ /**
55
+ * Check if a value is a string.
56
+ * A string is considered valid if it is:
57
+ * - not null or undefined
58
+ * - of type 'string'
59
+ * @param value - The value to check.
60
+ * @returns true if the value is a string, false otherwise.
61
+ */
7
62
  export declare const isString: (value: AnyObject) => value is string;
63
+ /**
64
+ * Check if a value is a number.
65
+ * A number is considered valid if it is:
66
+ * - not null or undefined
67
+ * - of type 'number'
68
+ * @param value - The value to check.
69
+ * @returns true if the value is a number, false otherwise.
70
+ */
8
71
  export declare const isNumber: (value: AnyObject) => value is number;
72
+ /**
73
+ * Check if a value is a boolean.
74
+ * A boolean is considered valid if it is:
75
+ * - not null or undefined
76
+ * - of type 'boolean'
77
+ */
9
78
  export declare const isBoolean: (value: AnyObject) => value is boolean;
79
+ /**
80
+ * Check if a value is a true.
81
+ * A value is considered true if it is:
82
+ * - strictly equal to true
83
+ * - equal to 1
84
+ * - a string that is "yes" or "true" (case insensitive)
85
+ */
10
86
  export declare const isTrue: (value?: AnyObject) => value is true;
87
+ /**
88
+ * Check if a value is false.
89
+ * A value is considered false if it is:
90
+ * - strictly equal to false
91
+ * - equal to 0
92
+ * - a string that is "no" or "false" (case insensitive)
93
+ */
11
94
  export declare const isFalse: (value?: AnyObject) => value is false;
95
+ /**
96
+ * Check if a value is a valid color.
97
+ * A color is considered valid if it:
98
+ * - starts with a '#' (hex color)
99
+ * - starts with 'rgb' (RGB color)
100
+ * - starts with 'hsl' (HSL color)
101
+ * - starts with 'lab' (Lab color)
102
+ * - is equal to 'transparent'
103
+ * @param color - The value to check.
104
+ * @returns true if the value is a valid color, false otherwise.
105
+ */
12
106
  export declare const isColor: (color: string) => boolean;
107
+ /**
108
+ * Check if a value is a valid SVG path.
109
+ * A valid SVG path is a string that:
110
+ * - starts with 'M' or 'm' (move to command)
111
+ * - contains at least one digit or decimal point
112
+ * - ends with 'Z' or 'z' (close path command)
113
+ * @param value - The value to check.
114
+ * @returns true if the value is a valid SVG path, false otherwise.
115
+ */
13
116
  export declare const isSvgPath: (value: AnyObject) => value is string;
117
+ /**
118
+ * Check if the document is in RTL (Right-to-Left) mode.
119
+ * This function checks the computed style of the document's root element
120
+ * to determine if the direction is set to 'rtl'.
121
+ * @returns true if the document is in RTL mode, false otherwise.
122
+ */
14
123
  export declare const isRtl: () => boolean;
@@ -0,0 +1,48 @@
1
+ export 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] | [lon: number | string, lat: number | string, z: number | string] | string;
11
+ export declare const getLatitudeLongitude: (lonLat: LngLatLike) => {
12
+ latitude: number;
13
+ longitude: number;
14
+ };
15
+ /**
16
+ * Convert various formats of longitude and latitude to a tuple of [lon, lat].
17
+ * This function supports:
18
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
19
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
20
+ * - Strings in the format "lat,lon"
21
+ * The function will return a tuple of numbers [lon, lat].
22
+ *
23
+ * @param {LngLatLike} lonLat - The input longitude and latitude in various formats.
24
+ * @returns { [lon: number, lat: number] } A tuple of [lon, lat] where lon and lat are numbers.
25
+ * @throws Error if the input is not a valid format.
26
+ */
27
+ export declare const convertLatLng: (lonLat: LngLatLike) => [lon: number, lat: number];
28
+ /**
29
+ * Convert a latitude and longitude to a human-readable text format.
30
+ * This function takes a latitude and longitude in various formats and
31
+ * returns a string in the format "degrees˚minutes'seconds" with direction (N/S, E/W).
32
+ *
33
+ * This function supports:
34
+ *
35
+ * - Objects with properties `lng`, `lat`, `lon`, or `longitude` and `latitude`
36
+ * - Arrays with two or three elements (e.g., [lon, lat] or [lon, lat, alt])
37
+ * - Strings in the format "lat,lon"
38
+ *
39
+ * @param {LngLatLike} latlon - The latitude and longitude in various formats.
40
+ * @returns {string} A string representation of the location in DMS format.
41
+ *
42
+ * @example
43
+ * ```jsx
44
+ * const location = getLocationAsText({ lat: 40.7128, lng: -74.0060 });
45
+ * console.log(location); // "40˚42'51.36"N, 74˚0'21.60"W"
46
+ * ```
47
+ */
48
+ export declare const getLocationAsText: (latlon: LngLatLike) => string;
@@ -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
- * split text into array of tokens by word boundaries
3
- * @param str
4
- * @param keywords
5
- * @internal
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;
@@ -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
- * @internal
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.6",
3
+ "version": "1.0.0-beta.60",
4
4
  "description": "RUF: Utility package",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -32,17 +32,17 @@
32
32
  "watch": "npm-run-all -p watch:rollup watch:dts"
33
33
  },
34
34
  "dependencies": {
35
- "date-fns": "^4.0.0",
36
- "libphonenumber-js": "^1.11.8",
37
- "numeral": "^2.0.6",
38
- "yup": "^1.4.0"
35
+ "date-fns": "~4.1.0",
36
+ "libphonenumber-js": "~1.12.25",
37
+ "numeral": "~2.0.6",
38
+ "yup": "~1.7.1"
39
39
  },
40
40
  "devDependencies": {
41
- "@types/numeral": "^2.0.2"
41
+ "@types/numeral": "~2.0.5"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=18.12.0",
45
45
  "npm": ">=8.19.0"
46
46
  },
47
- "gitHead": "21167df13cedeae8b365e87b9bab6d7401298cb6"
47
+ "gitHead": "a79f053ee7e13422c06d7d9c35d4f0c2610cdf5d"
48
48
  }
package/src/_ascii.ts CHANGED
@@ -80,14 +80,19 @@ const conversions = {
80
80
  };
81
81
 
82
82
  /**
83
- * latin to ascii converter for convenient searching
84
- * @internal
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(([a, l]) => {
89
- const re = new RegExp(l, "g");
90
- str = `${str}`.replace(re, a);
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
  };
@@ -38,8 +38,13 @@ const _getBox = (
38
38
  };
39
39
 
40
40
  /**
41
- * box "x,y,w,h" | [x,y,w,h]
42
- * returns canvas box [x,y,w,h]
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 "x1,y1,x2,y2" | [x1,y1,x2,y2]
59
- * returns canvas box [x,y,w,h]
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
- * @internal
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
- * country list with helper functions
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 iso2 code
50
- * @param code
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 iso3 code
58
- * @param code
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
- * Country flag emoji
66
- * @param code
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
- * @param code
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
- * Find country by code
82
- * @param code
83
- * @return continent: string;
84
- * @return iso2: string;
85
- * @return iso3: string;
86
- * @return name: string;
87
- * @return fullname: string;
88
- * @return capital: string;
89
- * @return tld: string;
90
- * @return currency: string;
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
- * debounce callback
28
- * @internal
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
- export const dedupe = (list: any[], key?: string) => {
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] ?? obj, obj])).values(),
44
+ new Map(filtered.map((obj) => [(obj as KeyValue)[key], obj])).values(),
34
45
  );
35
46
  };
36
47
 
37
- export const flatten = (list: any[]) => {
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
- export const flattenAndDedupe = (list: any[] = [], key?: string) => {
42
- return dedupe(flatten(list), key);
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 helper
39
- * @param method
40
- * @param url
41
- * @param options.headers
42
- * @param options.body
43
- * @param options.signal
44
- * @internal
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",