@sohanemon/utils 5.0.3 → 5.0.5

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
+ }
@@ -64,4 +64,30 @@ export declare function getObjectValue<T, S extends string, D>(obj: T, path: S,
64
64
  * getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
65
65
  */
66
66
  export declare function getObjectValue<T, S extends string>(obj: T, path: S): GetValue<T, SplitPath<S>> | undefined;
67
+ /**
68
+ * Extend an object or function with additional properties while
69
+ * preserving the original type information.
70
+ *
71
+ * Works with both plain objects and callable functions since
72
+ * functions in JavaScript are objects too.
73
+ *
74
+ * @template T The base object or function type
75
+ * @template P The additional properties type
76
+ *
77
+ * @param base - The object or function to extend
78
+ * @param props - An object containing properties to attach
79
+ *
80
+ * @returns The same object/function, augmented with the given properties
81
+ *
82
+ * @example
83
+ * // Extend an object
84
+ * const obj = extendProps({ a: 1 }, { b: "hello" });
85
+ * // obj has { a: number; b: string }
86
+ *
87
+ * // Extend a function
88
+ * const fn = (x: number) => x * 2;
89
+ * const enhanced = extendProps(fn, { name: "doubler" });
90
+ * // enhanced is callable and also has { name: string }
91
+ */
92
+ export declare function extendProps<T extends object, P extends object>(base: T, props: P): T & P;
67
93
  export {};
@@ -37,3 +37,31 @@ export function getObjectValue(obj, path, defaultValue) {
37
37
  }
38
38
  return current !== undefined ? current : defaultValue;
39
39
  }
40
+ /**
41
+ * Extend an object or function with additional properties while
42
+ * preserving the original type information.
43
+ *
44
+ * Works with both plain objects and callable functions since
45
+ * functions in JavaScript are objects too.
46
+ *
47
+ * @template T The base object or function type
48
+ * @template P The additional properties type
49
+ *
50
+ * @param base - The object or function to extend
51
+ * @param props - An object containing properties to attach
52
+ *
53
+ * @returns The same object/function, augmented with the given properties
54
+ *
55
+ * @example
56
+ * // Extend an object
57
+ * const obj = extendProps({ a: 1 }, { b: "hello" });
58
+ * // obj has { a: number; b: string }
59
+ *
60
+ * // Extend a function
61
+ * const fn = (x: number) => x * 2;
62
+ * const enhanced = extendProps(fn, { name: "doubler" });
63
+ * // enhanced is callable and also has { name: string }
64
+ */
65
+ export function extendProps(base, props) {
66
+ return Object.assign(base, props);
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.0.3",
3
+ "version": "5.0.5",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",