nhb-toolbox 3.1.1 → 3.1.3

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.
@@ -1,16 +1,22 @@
1
1
  import type { GenericObject } from '../object/types';
2
- import type { SortOptions } from './types';
2
+ import type { OrderOption, SortByOption } from './types';
3
3
  /**
4
- * * Sorts an array of strings, numbers, booleans, or objects based on the provided options.
4
+ * * Sorts an array of objects.
5
5
  *
6
- * - If the array contains strings, it sorts them alphabetically.
7
- * - If the array contains numbers, it sorts them numerically.
8
- * - If the array contains booleans, it sorts them by their boolean value.
9
- * - If the array contains objects, it sorts them by the specified field in the options `sortByField`.
6
+ * - Sorts array by the specified field.
10
7
  *
11
- * @param array - The array to sort.
12
- * @param options - Sorting options for objects.
8
+ * @template T - The type of objects in the array.
9
+ * @param array - The array of objects to sort.
10
+ * @param options - Sorting options.
13
11
  * @returns The sorted array.
14
12
  */
15
- export declare function sortAnArray<T extends number | string | boolean | GenericObject>(array: T[], options?: SortOptions<T>): T[];
13
+ export declare function sortAnArray<T extends GenericObject>(array: T[], options: SortByOption<T>): T[];
14
+ /**
15
+ * * Sorts an array of `strings`, `numbers` or `boolean`.
16
+ *
17
+ * @param array - The array of `strings`, `numbers` or `boolean` to sort.
18
+ * @param options - Sorting options.
19
+ * @returns The sorted array.
20
+ */
21
+ export declare function sortAnArray<T extends string | number | boolean>(array: T[], options?: OrderOption): T[];
16
22
  //# sourceMappingURL=sort.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/array/sort.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAC1B,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,aAAa,EAClD,KAAK,EAAE,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAyE3C"}
1
+ {"version":3,"file":"sort.d.ts","sourceRoot":"","sources":["../../src/array/sort.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAe,MAAM,SAAS,CAAC;AAEtE;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,aAAa,EAClD,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GACtB,CAAC,EAAE,CAAC;AAEP;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,OAAO,EAC9D,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,CAAC,EAAE,WAAW,GACnB,CAAC,EAAE,CAAC"}
@@ -34,14 +34,13 @@ export function sortAnArray(array, options) {
34
34
  // Handle array of objects
35
35
  if (isArrayOfType(array, isObject) && options && 'sortByField' in options) {
36
36
  return [...array].sort((a, b) => {
37
- // const key = options.sortByField as keyof T;
38
- // const keyA = (a as T)[key];
39
- // const keyB = (b as T)[key];
40
- const getKeyValue = (obj, path) => path
41
- .split('.')
42
- .reduce((acc, key) => acc?.[key], obj);
43
- const keyA = getKeyValue(a, options?.sortByField);
44
- const keyB = getKeyValue(b, options?.sortByField);
37
+ const _getKeyValue = (obj, path) => {
38
+ return path
39
+ .split('.')
40
+ .reduce((acc, key) => acc?.[key], obj);
41
+ };
42
+ const keyA = _getKeyValue(a, options?.sortByField);
43
+ const keyB = _getKeyValue(b, options?.sortByField);
45
44
  if (keyA == null || keyB == null) {
46
45
  return keyA == null ? 1 : -1;
47
46
  }
@@ -7,7 +7,7 @@ import type { PrimitiveKey } from '../types';
7
7
  * */
8
8
  export type Flattened<T> = T extends (infer U)[] ? Flattened<U> : T;
9
9
  /**
10
- * - Configuration for `createOptionsArray`.
10
+ * * Configuration for `createOptionsArray`.
11
11
  * - Defines the mapping between keys in the input objects and the keys in the output options.
12
12
  *
13
13
  * @typeParam T - The type of the objects in the input array.
@@ -52,5 +52,6 @@ export interface SortByOption<T extends GenericObject> extends OrderOption {
52
52
  /** The field by which to sort the objects in the array. */
53
53
  sortByField: NestedPrimitiveKey<T>;
54
54
  }
55
+ /** * Options for sorting array. */
55
56
  export type SortOptions<T> = T extends GenericObject ? SortByOption<T> : OrderOption;
56
57
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;;;MAIM;AACN,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEpE;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE;IACvC;;;OAGG;IACH,aAAa,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAE/B;;;OAGG;IACH,cAAc,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAEhC;;;;OAIG;IACH,cAAc,CAAC,EAAE,EAAE,CAAC;IAEpB;;;;OAIG;IACH,eAAe,CAAC,EAAE,EAAE,CAAC;CACrB;AAED,kCAAkC;AAClC,MAAM,WAAW,WAAW;IAC3B;;;;OAIG;IACH,SAAS,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAC3B;AAED,yEAAyE;AACzE,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,aAAa,CAAE,SAAQ,WAAW;IAC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@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",