@sohanemon/utils 5.0.2 → 5.0.4
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/functions/index.d.ts +30 -0
- package/dist/functions/index.js +43 -0
- package/package.json +1 -1
|
@@ -209,3 +209,33 @@ export declare const mergeRefs: MergeRefs;
|
|
|
209
209
|
* @example goToClientSideHash('my-element');
|
|
210
210
|
*/
|
|
211
211
|
export declare function goToClientSideHash(id: string, opts?: ScrollIntoViewOptions): void;
|
|
212
|
+
/**
|
|
213
|
+
* Escapes a string for use in a regular expression.
|
|
214
|
+
*
|
|
215
|
+
* @param str - The string to escape
|
|
216
|
+
* @returns - The escaped string
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
220
|
+
* // escapedString === 'Hello\\, world!'
|
|
221
|
+
*/
|
|
222
|
+
export declare function escapeRegExp(str: string): string;
|
|
223
|
+
/**
|
|
224
|
+
* Normalizes a string by:
|
|
225
|
+
* - Applying Unicode normalization (NFC)
|
|
226
|
+
* - Optionally removing diacritic marks (accents)
|
|
227
|
+
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
228
|
+
* - Optionally converting to lowercase
|
|
229
|
+
*
|
|
230
|
+
* @param str - The string to normalize
|
|
231
|
+
* @param options - Normalization options
|
|
232
|
+
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
233
|
+
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
234
|
+
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
235
|
+
* @returns The normalized string
|
|
236
|
+
*/
|
|
237
|
+
export declare function normalizeText(str?: string | null, options?: {
|
|
238
|
+
lowercase?: boolean;
|
|
239
|
+
removeAccents?: boolean;
|
|
240
|
+
removeNonAlphanumeric?: boolean;
|
|
241
|
+
}): string;
|
package/dist/functions/index.js
CHANGED
|
@@ -374,3 +374,46 @@ export function goToClientSideHash(id, opts) {
|
|
|
374
374
|
el.scrollIntoView({ behavior: 'smooth', block: 'start', ...opts });
|
|
375
375
|
window.history.pushState(null, '', `#${id}`);
|
|
376
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Escapes a string for use in a regular expression.
|
|
379
|
+
*
|
|
380
|
+
* @param str - The string to escape
|
|
381
|
+
* @returns - The escaped string
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
385
|
+
* // escapedString === 'Hello\\, world!'
|
|
386
|
+
*/
|
|
387
|
+
export function escapeRegExp(str) {
|
|
388
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Normalizes a string by:
|
|
392
|
+
* - Applying Unicode normalization (NFC)
|
|
393
|
+
* - Optionally removing diacritic marks (accents)
|
|
394
|
+
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
395
|
+
* - Optionally converting to lowercase
|
|
396
|
+
*
|
|
397
|
+
* @param str - The string to normalize
|
|
398
|
+
* @param options - Normalization options
|
|
399
|
+
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
400
|
+
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
401
|
+
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
402
|
+
* @returns The normalized string
|
|
403
|
+
*/
|
|
404
|
+
export function normalizeText(str, options = {}) {
|
|
405
|
+
if (!str)
|
|
406
|
+
return '';
|
|
407
|
+
const { lowercase = true, removeAccents = true, removeNonAlphanumeric = true, } = options;
|
|
408
|
+
let result = str.normalize('NFC');
|
|
409
|
+
if (removeAccents) {
|
|
410
|
+
result = result.replace(/\p{M}/gu, ''); // remove accents
|
|
411
|
+
}
|
|
412
|
+
if (removeNonAlphanumeric) {
|
|
413
|
+
result = result.replace(/^[^\p{L}\p{N}]*|[^\p{L}\p{N}]*$/gu, ''); // trim edges
|
|
414
|
+
}
|
|
415
|
+
if (lowercase) {
|
|
416
|
+
result = result.toLocaleLowerCase();
|
|
417
|
+
}
|
|
418
|
+
return result;
|
|
419
|
+
}
|