@sohanemon/utils 5.0.3 → 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.
@@ -220,3 +220,22 @@ export declare function goToClientSideHash(id: string, opts?: ScrollIntoViewOpti
220
220
  * // escapedString === 'Hello\\, world!'
221
221
  */
222
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;
@@ -387,3 +387,33 @@ export function goToClientSideHash(id, opts) {
387
387
  export function escapeRegExp(str) {
388
388
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
389
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.0.3",
3
+ "version": "5.0.4",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",