@creopse/utils 0.0.15 → 0.0.16
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/LICENSE +21 -0
- package/dist/enums/index.d.ts +3 -0
- package/dist/enums/index.js +3 -0
- package/dist/enums/notification-type.d.ts +1 -1
- package/dist/enums/notification-type.js +1 -1
- package/dist/enums/permission.d.ts +28 -0
- package/dist/enums/permission.js +38 -0
- package/dist/enums/profile-type.d.ts +1 -0
- package/dist/enums/profile-type.js +1 -0
- package/dist/{utils → helpers}/chronos.d.ts +1 -1
- package/dist/{utils → helpers}/chronos.js +1 -1
- package/dist/helpers/constants/constants.d.ts +2 -0
- package/dist/helpers/constants/constants.js +3 -0
- package/dist/helpers/constants/index.d.ts +1 -0
- package/dist/helpers/constants/index.js +1 -0
- package/dist/helpers/constants.d.ts +2 -0
- package/dist/helpers/constants.js +3 -0
- package/dist/helpers/functions/array.d.ts +22 -0
- package/dist/helpers/functions/array.js +42 -0
- package/dist/helpers/functions/browser.d.ts +40 -0
- package/dist/helpers/functions/browser.js +72 -0
- package/dist/helpers/functions/color.d.ts +14 -0
- package/dist/helpers/functions/color.js +26 -0
- package/dist/helpers/functions/file.d.ts +64 -0
- package/dist/helpers/functions/file.js +210 -0
- package/dist/helpers/functions/geo.d.ts +17 -0
- package/dist/helpers/functions/geo.js +31 -0
- package/dist/helpers/functions/image.d.ts +20 -0
- package/dist/helpers/functions/image.js +44 -0
- package/dist/helpers/functions/index.d.ts +13 -0
- package/dist/helpers/functions/index.js +13 -0
- package/dist/helpers/functions/misc.d.ts +55 -0
- package/dist/helpers/functions/misc.js +125 -0
- package/dist/helpers/functions/number.d.ts +15 -0
- package/dist/helpers/functions/number.js +26 -0
- package/dist/helpers/functions/object.d.ts +79 -0
- package/dist/helpers/functions/object.js +141 -0
- package/dist/helpers/functions/string.d.ts +110 -0
- package/dist/helpers/functions/string.js +211 -0
- package/dist/helpers/functions/svg.d.ts +7 -0
- package/dist/helpers/functions/svg.js +25 -0
- package/dist/helpers/functions/time.d.ts +97 -0
- package/dist/helpers/functions/time.js +97 -0
- package/dist/helpers/functions/url.d.ts +46 -0
- package/dist/helpers/functions/url.js +120 -0
- package/dist/{utils → helpers}/functions.d.ts +14 -0
- package/dist/{utils → helpers}/functions.js +26 -1
- package/dist/{utils → helpers}/index.d.ts +1 -1
- package/dist/{utils → helpers}/index.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/models/page-section.d.ts +2 -1
- package/dist/models/page-section.js +2 -1
- package/dist/types/core-bridge/api.d.ts +31 -0
- package/dist/types/core-bridge/api.js +1 -0
- package/dist/types/core-bridge/i18n.d.ts +6 -0
- package/dist/types/core-bridge/i18n.js +1 -0
- package/dist/types/core-bridge/index.d.ts +4 -0
- package/dist/types/core-bridge/index.js +4 -0
- package/dist/types/core-bridge/shared-data.d.ts +22 -0
- package/dist/types/core-bridge/shared-data.js +1 -0
- package/dist/types/core-bridge/stores.d.ts +31 -0
- package/dist/types/core-bridge/stores.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/plugin.d.ts +30 -0
- package/dist/types/plugin.js +1 -0
- package/package.json +23 -5
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is an object and not an array or null.
|
|
3
|
+
*
|
|
4
|
+
* @param {any} value - The value to check.
|
|
5
|
+
* @return {boolean} Returns true if the value is an object and not an array or null, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export function isRealObject(value) {
|
|
8
|
+
return typeof value === 'object' && !Array.isArray(value) && value !== null;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Determines if a given string is a valid JSON string representing an object.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} str - The string to be checked.
|
|
14
|
+
* @return {boolean} Returns true if the string is a valid JSON string of an object, false otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export const isStringifiedObject = (str) => {
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(str);
|
|
19
|
+
return (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed));
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Recursively flattens a nested object into a single-level object with
|
|
27
|
+
* dot-separated property names.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const obj = {
|
|
31
|
+
* a: {
|
|
32
|
+
* b: {
|
|
33
|
+
* c: 1
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* const flattened = flattenObject(obj) // { 'a.b.c': 1 }
|
|
38
|
+
*
|
|
39
|
+
* @param {object} obj The object to flatten.
|
|
40
|
+
* @param {string} [prefix=''] The prefix to use for the flattened property names.
|
|
41
|
+
* @returns {object} The flattened object.
|
|
42
|
+
*/
|
|
43
|
+
export const flattenObject = (obj, prefix = '') => {
|
|
44
|
+
return Object.keys(obj).reduce((acc, k) => {
|
|
45
|
+
const pre = prefix.length ? `${prefix}.` : '';
|
|
46
|
+
if (typeof obj[k] === 'object' &&
|
|
47
|
+
obj[k] !== null &&
|
|
48
|
+
!Array.isArray(obj[k])) {
|
|
49
|
+
Object.assign(acc, flattenObject(obj[k], pre + k));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
acc[pre + k] = obj[k];
|
|
53
|
+
}
|
|
54
|
+
return acc;
|
|
55
|
+
}, {});
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Recursively unflattens a single-level object with dot-separated property
|
|
59
|
+
* names into a nested object.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const obj = { 'a.b.c': 1 }
|
|
63
|
+
* const unflattened = unflattenObject(obj) // { a: { b: { c: 1 } } }
|
|
64
|
+
*
|
|
65
|
+
* @param {object} obj The object to unflatten.
|
|
66
|
+
* @returns {object} The unflattened object.
|
|
67
|
+
*/
|
|
68
|
+
export const unflattenObject = (obj) => {
|
|
69
|
+
const result = {};
|
|
70
|
+
for (const key in obj) {
|
|
71
|
+
const keys = key.split('.');
|
|
72
|
+
keys.reduce((acc, part, index) => {
|
|
73
|
+
if (index === keys.length - 1) {
|
|
74
|
+
acc[part] = obj[key];
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
if (!acc[part])
|
|
78
|
+
acc[part] = {};
|
|
79
|
+
}
|
|
80
|
+
return acc[part];
|
|
81
|
+
}, result);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Renames a key in an object.
|
|
87
|
+
*
|
|
88
|
+
* @param {Object} obj - The object that contains the key to rename.
|
|
89
|
+
* @param {string} oldKey - The old key name.
|
|
90
|
+
* @param {string} newKey - The new key name.
|
|
91
|
+
*/
|
|
92
|
+
export const renameKey = (obj, oldKey, newKey) => {
|
|
93
|
+
if (Object.prototype.hasOwnProperty.call(obj, oldKey)) {
|
|
94
|
+
obj[newKey] = obj[oldKey];
|
|
95
|
+
delete obj[oldKey];
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new object with a specified key renamed, leaving the original object unchanged.
|
|
100
|
+
*
|
|
101
|
+
* @param {Record<string, any>} obj - The original object containing the key to rename.
|
|
102
|
+
* @param {string} oldKey - The name of the key to rename.
|
|
103
|
+
* @param {string} newKey - The new name for the key.
|
|
104
|
+
* @returns {Record<string, any>} A new object with the key renamed.
|
|
105
|
+
*/
|
|
106
|
+
export const renameKeyImmutable = (obj, oldKey, newKey) => {
|
|
107
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
108
|
+
if (key === oldKey) {
|
|
109
|
+
acc[newKey] = obj[oldKey];
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
acc[key] = obj[key];
|
|
113
|
+
}
|
|
114
|
+
return acc;
|
|
115
|
+
}, {});
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Omits the specified keys from the given object.
|
|
119
|
+
*
|
|
120
|
+
* @param {T} obj The object to omit keys from.
|
|
121
|
+
* @param {K[]} keys The keys to omit.
|
|
122
|
+
* @returns {Omit<T, K>} A new object with the specified keys omitted.
|
|
123
|
+
*/
|
|
124
|
+
export const omitKeys = (obj, keys) => {
|
|
125
|
+
const result = { ...obj };
|
|
126
|
+
for (const key of keys) {
|
|
127
|
+
delete result[key];
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Checks if the given object is empty.
|
|
133
|
+
*
|
|
134
|
+
* An object is considered empty if it is an object instance (i.e. `obj.constructor === Object`), and if it has no own enumerable property (i.e. `Object.keys(obj).length === 0`).
|
|
135
|
+
*
|
|
136
|
+
* @param {any} obj - The object to check.
|
|
137
|
+
* @returns {boolean} Returns true if the object is empty, false otherwise.
|
|
138
|
+
*/
|
|
139
|
+
export const isObjectEmpty = (obj) => {
|
|
140
|
+
return obj && Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
141
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates the initials of a given name.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} name - The name to generate the initials from.
|
|
5
|
+
* @return {string} The initials of the given name.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getNameInitials(name: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Pads the left side of a string with a specified character until it reaches a specified length.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} string - The string to be padded.
|
|
12
|
+
* @param {string} pad - The character used for padding.
|
|
13
|
+
* @param {number} length - The desired length of the resulting string.
|
|
14
|
+
* @return {string} The padded string.
|
|
15
|
+
*/
|
|
16
|
+
export declare function strPadLeft(string: string, pad: string, length: number): string;
|
|
17
|
+
/**
|
|
18
|
+
* Capitalizes the first letter of a given string.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} string - The string to capitalize.
|
|
21
|
+
* @return {string} The modified string with the first letter capitalized.
|
|
22
|
+
*/
|
|
23
|
+
export declare function capitalizeFirstLetter(string: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Extracts uppercase words from the given input string.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} input - The input string to extract uppercase words from.
|
|
28
|
+
* @return {RegExpMatchArray | null} An array of uppercase words found in the input string.
|
|
29
|
+
*/
|
|
30
|
+
export declare function extractUppercaseWords(input: string): RegExpMatchArray | null;
|
|
31
|
+
/**
|
|
32
|
+
* Extracts lowercase words from the input string.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} input - The string to extract lowercase words from.
|
|
35
|
+
* @return {RegExpMatchArray | null} An array of lowercase words extracted from the input string.
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractLowercaseWords(input: string): RegExpMatchArray | null;
|
|
38
|
+
/**
|
|
39
|
+
* Extracts numbers from a given string.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} string - The string from which to extract numbers.
|
|
42
|
+
* @param {object} options - Optional parameters for customization.
|
|
43
|
+
* @param {boolean} options.array - Whether to return an array of numbers. Defaults to false.
|
|
44
|
+
* @param {boolean} options.float - Whether to include floating-point numbers. Defaults to true.
|
|
45
|
+
* @return {number | number[]} - The extracted number(s) from the string. If options.array is true, an array will be returned. Otherwise, a single number will be returned.
|
|
46
|
+
*/
|
|
47
|
+
export declare function extractNumber(string: string, { array, float }?: {
|
|
48
|
+
array?: boolean;
|
|
49
|
+
float?: boolean;
|
|
50
|
+
}): number | number[];
|
|
51
|
+
/**
|
|
52
|
+
* Extracts all alphabetic characters from a given string.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} string - The input string.
|
|
55
|
+
* @param {Object} options - Optional parameters.
|
|
56
|
+
* @param {boolean} options.array - Whether to return the result as an array. Default is false.
|
|
57
|
+
* @return {string | string[]} - The extracted alphabetic characters, either as a string or an array.
|
|
58
|
+
*/
|
|
59
|
+
export declare function extractAlphabet(string: string, { array }?: {
|
|
60
|
+
array?: boolean;
|
|
61
|
+
}): string | string[];
|
|
62
|
+
/**
|
|
63
|
+
* Removes the specified searchString from the given str.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} str - The original string.
|
|
66
|
+
* @param {string} searchString - The string to be removed from str.
|
|
67
|
+
* @return {string} The modified string with the searchString removed.
|
|
68
|
+
*/
|
|
69
|
+
export declare function removeFromString(str: string, searchString: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Replaces multiple parts of a string based on a given set of replacements.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} inputString - The original string to be modified.
|
|
74
|
+
* @param {Record<string, string>} replacements - An object containing key-value pairs where the key is the part to be replaced and the value is the replacement string.
|
|
75
|
+
* @return {string} - The modified string with all occurrences of the specified parts replaced.
|
|
76
|
+
*/
|
|
77
|
+
export declare function replaceStringParts(inputString: string, replacements: Record<string, string>): string;
|
|
78
|
+
/**
|
|
79
|
+
* Checks if a given value is a title-like string.
|
|
80
|
+
*
|
|
81
|
+
* A title-like string is defined as a string that is not empty, not too long, not too short, does not contain newlines, and starts with a capital letter.
|
|
82
|
+
*
|
|
83
|
+
* The function takes an optional options object with the following properties:
|
|
84
|
+
* - thresholdScore: The minimum score required for the string to be considered title-like. Defaults to 2.
|
|
85
|
+
* - tooLongValue: The maximum length of the string for it to be considered title-like. Defaults to 200.
|
|
86
|
+
* - tooShortValue: The minimum length of the string for it to be considered title-like. Defaults to 3.
|
|
87
|
+
*
|
|
88
|
+
* The function returns an object with two properties: match and score. Match is a boolean indicating whether the string is title-like, and score is a number indicating how well the string matches the title-like criteria.
|
|
89
|
+
*/
|
|
90
|
+
export declare const isTitleLike: (value: any, options?: {
|
|
91
|
+
thresholdScore?: number;
|
|
92
|
+
tooLongValue?: number;
|
|
93
|
+
tooShortValue?: number;
|
|
94
|
+
}) => {
|
|
95
|
+
match: boolean;
|
|
96
|
+
score: number;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Extracts a title-like value from the given data.
|
|
100
|
+
*
|
|
101
|
+
* This function first attempts to find a direct match by known field name.
|
|
102
|
+
* If no match is found, it then uses a heuristic scoring system to find a title-like value.
|
|
103
|
+
* Finally, it falls back to the first string value if no match is found.
|
|
104
|
+
*
|
|
105
|
+
* @param {unknown} data The data object to extract the title-like value from.
|
|
106
|
+
* @param {(value: string) => string} tr A function to translate the extracted value.
|
|
107
|
+
* @param {string} [defaultValue=''] The default value to return if no match is found.
|
|
108
|
+
* @returns {string | undefined} The extracted title-like value, or undefined if no match is found.
|
|
109
|
+
*/
|
|
110
|
+
export declare const extractTitleLike: (data: unknown, tr: (value: string) => string, defaultValue?: string) => string | undefined;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { isRealObject, isStringifiedObject } from './object';
|
|
3
|
+
/**
|
|
4
|
+
* Generates the initials of a given name.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} name - The name to generate the initials from.
|
|
7
|
+
* @return {string} The initials of the given name.
|
|
8
|
+
*/
|
|
9
|
+
export function getNameInitials(name) {
|
|
10
|
+
const nameSplit = name.split(' '), initials = nameSplit.length > 1
|
|
11
|
+
? nameSplit[0].charAt(0).toUpperCase() +
|
|
12
|
+
nameSplit[1].charAt(0).toUpperCase()
|
|
13
|
+
: nameSplit[0].charAt(0).toUpperCase();
|
|
14
|
+
return initials;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Pads the left side of a string with a specified character until it reaches a specified length.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} string - The string to be padded.
|
|
20
|
+
* @param {string} pad - The character used for padding.
|
|
21
|
+
* @param {number} length - The desired length of the resulting string.
|
|
22
|
+
* @return {string} The padded string.
|
|
23
|
+
*/
|
|
24
|
+
export function strPadLeft(string, pad, length) {
|
|
25
|
+
return (new Array(length + 1).join(pad) + string).slice(-length);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Capitalizes the first letter of a given string.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} string - The string to capitalize.
|
|
31
|
+
* @return {string} The modified string with the first letter capitalized.
|
|
32
|
+
*/
|
|
33
|
+
export function capitalizeFirstLetter(string) {
|
|
34
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Extracts uppercase words from the given input string.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} input - The input string to extract uppercase words from.
|
|
40
|
+
* @return {RegExpMatchArray | null} An array of uppercase words found in the input string.
|
|
41
|
+
*/
|
|
42
|
+
export function extractUppercaseWords(input) {
|
|
43
|
+
return input.match(/(\b[A-Z]['A-Z]+|\b[A-Z]\b)/g);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Extracts lowercase words from the input string.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} input - The string to extract lowercase words from.
|
|
49
|
+
* @return {RegExpMatchArray | null} An array of lowercase words extracted from the input string.
|
|
50
|
+
*/
|
|
51
|
+
export function extractLowercaseWords(input) {
|
|
52
|
+
return input.match(/(\b[a-z]['a-z]+|\b[a-z]\b)/g);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Extracts numbers from a given string.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} string - The string from which to extract numbers.
|
|
58
|
+
* @param {object} options - Optional parameters for customization.
|
|
59
|
+
* @param {boolean} options.array - Whether to return an array of numbers. Defaults to false.
|
|
60
|
+
* @param {boolean} options.float - Whether to include floating-point numbers. Defaults to true.
|
|
61
|
+
* @return {number | number[]} - The extracted number(s) from the string. If options.array is true, an array will be returned. Otherwise, a single number will be returned.
|
|
62
|
+
*/
|
|
63
|
+
export function extractNumber(string, { array = false, float = true } = {}) {
|
|
64
|
+
const result = string.toString().match(float ? /[+-]?\d+(\.\d+)?/g : /\d/g);
|
|
65
|
+
if (result)
|
|
66
|
+
return array
|
|
67
|
+
? result.map((r) => parseFloat(r))
|
|
68
|
+
: parseFloat(result.join(''));
|
|
69
|
+
else
|
|
70
|
+
return array ? [0] : 0;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Extracts all alphabetic characters from a given string.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} string - The input string.
|
|
76
|
+
* @param {Object} options - Optional parameters.
|
|
77
|
+
* @param {boolean} options.array - Whether to return the result as an array. Default is false.
|
|
78
|
+
* @return {string | string[]} - The extracted alphabetic characters, either as a string or an array.
|
|
79
|
+
*/
|
|
80
|
+
export function extractAlphabet(string, { array = false } = {}) {
|
|
81
|
+
const result = string.toString().match(/[a-zA-Z]/);
|
|
82
|
+
if (result)
|
|
83
|
+
return array ? result : result.join('');
|
|
84
|
+
else
|
|
85
|
+
return array ? [''] : '';
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Removes the specified searchString from the given str.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} str - The original string.
|
|
91
|
+
* @param {string} searchString - The string to be removed from str.
|
|
92
|
+
* @return {string} The modified string with the searchString removed.
|
|
93
|
+
*/
|
|
94
|
+
export function removeFromString(str, searchString) {
|
|
95
|
+
if (str.endsWith(searchString)) {
|
|
96
|
+
return str.slice(0, -searchString.length);
|
|
97
|
+
}
|
|
98
|
+
return str;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Replaces multiple parts of a string based on a given set of replacements.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} inputString - The original string to be modified.
|
|
104
|
+
* @param {Record<string, string>} replacements - An object containing key-value pairs where the key is the part to be replaced and the value is the replacement string.
|
|
105
|
+
* @return {string} - The modified string with all occurrences of the specified parts replaced.
|
|
106
|
+
*/
|
|
107
|
+
export function replaceStringParts(inputString, replacements) {
|
|
108
|
+
if (Object.keys(replacements).length === 0) {
|
|
109
|
+
return inputString;
|
|
110
|
+
}
|
|
111
|
+
// Create a regular expression pattern by joining all keys of replacements with the "|" (OR) operator
|
|
112
|
+
const pattern = new RegExp(Object.keys(replacements).join('|'), 'g');
|
|
113
|
+
// Use the replace method with a callback function
|
|
114
|
+
const resultString = inputString.replace(pattern, (match) => {
|
|
115
|
+
// Use the match as a key to get the replacement value from the replacements object
|
|
116
|
+
return replacements[match];
|
|
117
|
+
});
|
|
118
|
+
return resultString;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Checks if a given value is a title-like string.
|
|
122
|
+
*
|
|
123
|
+
* A title-like string is defined as a string that is not empty, not too long, not too short, does not contain newlines, and starts with a capital letter.
|
|
124
|
+
*
|
|
125
|
+
* The function takes an optional options object with the following properties:
|
|
126
|
+
* - thresholdScore: The minimum score required for the string to be considered title-like. Defaults to 2.
|
|
127
|
+
* - tooLongValue: The maximum length of the string for it to be considered title-like. Defaults to 200.
|
|
128
|
+
* - tooShortValue: The minimum length of the string for it to be considered title-like. Defaults to 3.
|
|
129
|
+
*
|
|
130
|
+
* The function returns an object with two properties: match and score. Match is a boolean indicating whether the string is title-like, and score is a number indicating how well the string matches the title-like criteria.
|
|
131
|
+
*/
|
|
132
|
+
export const isTitleLike = (value, options) => {
|
|
133
|
+
if (typeof value !== 'string')
|
|
134
|
+
return { match: false, score: 0 };
|
|
135
|
+
const { thresholdScore = 2, tooLongValue = 200, tooShortValue = 3, } = options || {};
|
|
136
|
+
const trimmed = value.trim();
|
|
137
|
+
const isNotEmpty = trimmed.length > 0;
|
|
138
|
+
const isNotTooLong = trimmed.length < tooLongValue;
|
|
139
|
+
const isNotTooShort = trimmed.length >= tooShortValue;
|
|
140
|
+
const hasNoNewlines = !trimmed.includes('\n');
|
|
141
|
+
const startsWithCapital = /^[A-ZÀ-ÖØ-Þ]/.test(trimmed);
|
|
142
|
+
let score = 0;
|
|
143
|
+
if (isNotEmpty && isNotTooLong && isNotTooShort)
|
|
144
|
+
score += 1;
|
|
145
|
+
if (hasNoNewlines)
|
|
146
|
+
score += 1;
|
|
147
|
+
if (startsWithCapital)
|
|
148
|
+
score += 1;
|
|
149
|
+
return { match: score >= thresholdScore, score };
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Extracts a title-like value from the given data.
|
|
153
|
+
*
|
|
154
|
+
* This function first attempts to find a direct match by known field name.
|
|
155
|
+
* If no match is found, it then uses a heuristic scoring system to find a title-like value.
|
|
156
|
+
* Finally, it falls back to the first string value if no match is found.
|
|
157
|
+
*
|
|
158
|
+
* @param {unknown} data The data object to extract the title-like value from.
|
|
159
|
+
* @param {(value: string) => string} tr A function to translate the extracted value.
|
|
160
|
+
* @param {string} [defaultValue=''] The default value to return if no match is found.
|
|
161
|
+
* @returns {string | undefined} The extracted title-like value, or undefined if no match is found.
|
|
162
|
+
*/
|
|
163
|
+
export const extractTitleLike = (data, tr, defaultValue) => {
|
|
164
|
+
if (!_.isObject(data) || !isRealObject(data))
|
|
165
|
+
return defaultValue;
|
|
166
|
+
const fields = [
|
|
167
|
+
'title',
|
|
168
|
+
'titre',
|
|
169
|
+
'name',
|
|
170
|
+
'nom',
|
|
171
|
+
'label',
|
|
172
|
+
'heading',
|
|
173
|
+
'text',
|
|
174
|
+
'texte',
|
|
175
|
+
'slug',
|
|
176
|
+
'subject',
|
|
177
|
+
'sujet',
|
|
178
|
+
'summary',
|
|
179
|
+
'description',
|
|
180
|
+
'content',
|
|
181
|
+
'body',
|
|
182
|
+
];
|
|
183
|
+
// Step 1: Direct match by known field name
|
|
184
|
+
for (const field of fields) {
|
|
185
|
+
const foundKey = Object.keys(data).find((k) => k.toLowerCase() === field);
|
|
186
|
+
if (foundKey) {
|
|
187
|
+
const value = data[foundKey];
|
|
188
|
+
if (typeof value === 'string') {
|
|
189
|
+
return isStringifiedObject(value) ? tr(value) : value;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Step 2: Heuristic scoring to find a title-like value
|
|
194
|
+
let bestScore = 0;
|
|
195
|
+
let bestValue;
|
|
196
|
+
let firstStrValue;
|
|
197
|
+
for (const [key, value] of Object.entries(data)) {
|
|
198
|
+
if (typeof value !== 'string')
|
|
199
|
+
continue;
|
|
200
|
+
const finalValue = isStringifiedObject(value) ? tr(value) : value;
|
|
201
|
+
if (!firstStrValue)
|
|
202
|
+
firstStrValue = finalValue;
|
|
203
|
+
const result = isTitleLike(finalValue, { tooLongValue: 500 });
|
|
204
|
+
if (result.match && result.score > bestScore) {
|
|
205
|
+
bestScore = result.score;
|
|
206
|
+
bestValue = finalValue;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Step 3: Fallback to the first string value if no match is found
|
|
210
|
+
return bestValue || firstStrValue || defaultValue;
|
|
211
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given string is a valid SVG document.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} input - The string to be checked.
|
|
5
|
+
* @return {boolean} Returns true if the string is a valid SVG document, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isSVG(input: string): boolean;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given string is a valid SVG document.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} input - The string to be checked.
|
|
5
|
+
* @return {boolean} Returns true if the string is a valid SVG document, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export function isSVG(input) {
|
|
8
|
+
const svgRegex = /^\s*<svg\b[^>]*>.*<\/svg>\s*$/is;
|
|
9
|
+
try {
|
|
10
|
+
if (svgRegex.test(input)) {
|
|
11
|
+
const parser = new DOMParser();
|
|
12
|
+
const doc = parser.parseFromString(input, 'image/svg+xml');
|
|
13
|
+
const parserErrors = doc.getElementsByTagName('parsererror');
|
|
14
|
+
if (parserErrors.length > 0) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const svgElements = doc.getElementsByTagName('svg');
|
|
18
|
+
return svgElements.length > 0 && svgElements[0].parentNode === doc;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import 'moment/locale/fr';
|
|
3
|
+
/**
|
|
4
|
+
* Waits for the given amount of milliseconds before resolving the promise.
|
|
5
|
+
* @param {number} ms - The time to wait in milliseconds.
|
|
6
|
+
* @returns {Promise<void>} - A promise that resolves after the specified time.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Returns the current timestamp in seconds.
|
|
11
|
+
*
|
|
12
|
+
* @returns {number} The current timestamp in seconds.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getCurrentTimestamp(): number;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a timestamp in seconds into a formatted date string.
|
|
17
|
+
*
|
|
18
|
+
* @param {number} date - The timestamp in seconds to convert.
|
|
19
|
+
* @param {{locale?: string, pattern?: string}} options - Optional parameters for the conversion.
|
|
20
|
+
* @param {string} options.locale - The locale to use for the conversion. Defaults to 'en'.
|
|
21
|
+
* @param {string} options.pattern - The pattern to use for the conversion. Defaults to 'MM/DD/YYYY HH:mm'.
|
|
22
|
+
* @returns {string} The formatted date string.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getDateFromTimestamp(date: number, { locale, pattern, }?: {
|
|
25
|
+
locale?: string;
|
|
26
|
+
pattern?: string;
|
|
27
|
+
}): string;
|
|
28
|
+
/**
|
|
29
|
+
* Converts a date string or Date object from one format to another.
|
|
30
|
+
*
|
|
31
|
+
* @param {string | Date} date - The date string or Date object to convert.
|
|
32
|
+
* @param {{inPattern?: string, outPattern?: string, locale?: string}} options - Optional parameters for the conversion.
|
|
33
|
+
* @param {string} options.inPattern - The pattern of the input date string. Defaults to 'YYYY-MM-DD'.
|
|
34
|
+
* @param {string} options.outPattern - The pattern of the output date string. Defaults to 'DD MMM YYYY'.
|
|
35
|
+
* @param {string} options.locale - The locale to use for the conversion. Defaults to 'fr'.
|
|
36
|
+
* @returns {string} The converted date string.
|
|
37
|
+
*/
|
|
38
|
+
export declare function reformatDate(date: string | Date, { inPattern, outPattern, locale, }?: {
|
|
39
|
+
inPattern?: string;
|
|
40
|
+
outPattern?: string;
|
|
41
|
+
locale?: string;
|
|
42
|
+
}): string;
|
|
43
|
+
/**
|
|
44
|
+
* Converts a date string or Date object into a formatted date string.
|
|
45
|
+
*
|
|
46
|
+
* @param {string | Date} date - The date string or Date object to convert.
|
|
47
|
+
* @param {{outPattern?: string, locale?: string}} options - Optional parameters for the conversion.
|
|
48
|
+
* @param {string} options.outPattern - The pattern of the output date string. Defaults to 'DD MMM YYYY'.
|
|
49
|
+
* @param {string} options.locale - The locale to use for the conversion. Defaults to 'fr'.
|
|
50
|
+
* @returns {string} The formatted date string.
|
|
51
|
+
*/
|
|
52
|
+
export declare function formatDate(date: string | Date, { outPattern, locale, }?: {
|
|
53
|
+
outPattern?: string;
|
|
54
|
+
locale?: string;
|
|
55
|
+
}): string;
|
|
56
|
+
/**
|
|
57
|
+
* Calculates the difference between the given date and today in the given unit.
|
|
58
|
+
*
|
|
59
|
+
* @param {string | Date} date - The date to calculate the difference for.
|
|
60
|
+
* @param {{pattern?: string, unit?: moment.unitOfTime.Diff}} options - Optional parameters for the calculation.
|
|
61
|
+
* @param {string} options.pattern - The pattern of the input date string. Defaults to 'YYYY-MM-DD'.
|
|
62
|
+
* @param {moment.unitOfTime.Diff} options.unit - The unit of time to calculate the difference in. Defaults to 'days'.
|
|
63
|
+
* @returns {number} The difference between the given date and today in the given unit.
|
|
64
|
+
*/
|
|
65
|
+
export declare function differenceWithToday(date: string | Date, { pattern, unit, }?: {
|
|
66
|
+
pattern?: string;
|
|
67
|
+
unit?: moment.unitOfTime.Diff;
|
|
68
|
+
}): number;
|
|
69
|
+
/**
|
|
70
|
+
* Calculates the difference between two dates.
|
|
71
|
+
*
|
|
72
|
+
* @param {string | Date} startDate - The start date.
|
|
73
|
+
* @param {string | Date} endDate - The end date.
|
|
74
|
+
* @param {{startDatePattern?: string, endDatePattern?: string, unit?: moment.unitOfTime.Diff}} options - Optional parameters for the calculation.
|
|
75
|
+
* @param {string} options.startDatePattern - The pattern of the start date string. Defaults to 'YYYY-MM-DD'.
|
|
76
|
+
* @param {string} options.endDatePattern - The pattern of the end date string. Defaults to 'YYYY-MM-DD'.
|
|
77
|
+
* @param {moment.unitOfTime.Diff} options.unit - The unit of time to calculate the difference in. Defaults to 'days'.
|
|
78
|
+
* @returns {number} The difference between the start and end dates in the given unit.
|
|
79
|
+
*/
|
|
80
|
+
export declare function differenceBetweenDates(startDate: string | Date, endDate: string | Date, { startDatePattern, endDatePattern, unit, }?: {
|
|
81
|
+
startDatePattern?: string;
|
|
82
|
+
endDatePattern?: string;
|
|
83
|
+
unit?: moment.unitOfTime.Diff;
|
|
84
|
+
}): number;
|
|
85
|
+
/**
|
|
86
|
+
* Calculates the difference between the given date and now.
|
|
87
|
+
*
|
|
88
|
+
* @param {string | Date} date - The date to calculate the difference from.
|
|
89
|
+
* @param {{locale?: string, pattern?: string}} options - Optional parameters for the calculation.
|
|
90
|
+
* @param {string} options.locale - The locale to use for the calculation. Defaults to 'en'.
|
|
91
|
+
* @param {string} options.pattern - The pattern of the date string. Defaults to 'YYYY-MM-DD HH:mm:ss'.
|
|
92
|
+
* @returns {string} The difference between the given date and now, relative to the given locale.
|
|
93
|
+
*/
|
|
94
|
+
export declare function differenceFromNow(date: string | Date, { locale, pattern, }?: {
|
|
95
|
+
locale?: string;
|
|
96
|
+
pattern?: string;
|
|
97
|
+
}): string;
|