nhb-toolbox 4.10.47 → 4.10.50

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/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  <img src="https://img.shields.io/npm/v/nhb-toolbox.svg?style=for-the-badge&color=teal" alt="Version" />
11
11
  </a>
12
12
  <a href="https://www.npmjs.com/package/nhb-toolbox" aria-label="License">
13
- <img src="https://img.shields.io/npm/l/nhb-toolbox.svg?label=license&style=for-the-badge&color=gold" alt="License" />
13
+ <img src="https://img.shields.io/npm/l/nhb-toolbox.svg?label=license&style=for-the-badge&color=orange" alt="License" />
14
14
  </a>
15
15
  </p>
16
16
 
@@ -21,7 +21,7 @@
21
21
  - **Helper Functions & Classes**: Reusable solutions for everyday tasks
22
22
  - **Type Guards & Predicates**: Runtime safety with perfect type inference
23
23
  - **Validation Utilities**: Robust data validation patterns
24
- - **Zero Dependencies**: Lightweight and framework-agnostic
24
+ - **Zero Dependencies**: Framework-agnostic implementation using only native TS/JS with 0 external package
25
25
 
26
26
  > [Explore Full Documentation →](https://nhb-toolbox.vercel.app/)
27
27
 
@@ -49,7 +49,7 @@ yarn add nhb-toolbox
49
49
 
50
50
  - **Type-Safe Utilities**:Fully typed for perfect TypeScript integration with strict type checking
51
51
  - **Modular Design**: Tree-shaking friendly – import only what you need with zero bloat
52
- - **Zero Dependencies**: Lightweight and framework-agnostic
52
+ - **Zero Dependencies**: No external dependencies - works with any JS/TS framework
53
53
  - **IDE Support**: Full type hints with JSDoc-powered API references in your editor
54
54
  - **Comprehensive Documentation**: Learn with real-world use cases on [documentation site](https://nhb-toolbox.vercel.app/)
55
55
  - **Battle-Tested**: Reliable utilities refined through real-world production use
@@ -7,6 +7,7 @@ exports.findMissingElements = findMissingElements;
7
7
  exports.splitArray = splitArray;
8
8
  exports.rotateArray = rotateArray;
9
9
  exports.moveArrayElement = moveArrayElement;
10
+ const primitives_1 = require("../guards/primitives");
10
11
  const utils_1 = require("../utils");
11
12
  /**
12
13
  * * Converts an array of objects into a formatted array of options.
@@ -16,10 +17,12 @@ const utils_1 = require("../utils");
16
17
  * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
17
18
  */
18
19
  const createOptionsArray = (data, config) => {
19
- const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
20
- if (data && data.length) {
21
- return data.map((datum) => ({
22
- [firstFieldName]: String(datum[firstFieldKey] ?? ''),
20
+ const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', numberAsNumber = false, } = config || {};
21
+ if (data && data?.length) {
22
+ return data?.map((datum) => ({
23
+ [firstFieldName]: numberAsNumber && (0, primitives_1.isNumber)(datum[firstFieldKey]) ?
24
+ datum[firstFieldKey]
25
+ : String(datum[firstFieldKey] ?? ''),
23
26
  [secondFieldName]: String(datum[secondFieldKey] ?? ''),
24
27
  }));
25
28
  }
@@ -7,7 +7,7 @@ import type { OptionsConfig } from './types';
7
7
  * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
8
8
  * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
9
9
  */
10
- export declare const createOptionsArray: <T extends GenericObject, K1 extends string = "value", K2 extends string = "label">(data: T[], config: OptionsConfig<T, K1, K2>) => { [P in K1 | K2]: string; }[];
10
+ export declare const createOptionsArray: <T extends GenericObject, K1 extends string = "value", K2 extends string = "label", V extends boolean = false>(data: T[], config: OptionsConfig<T, K1, K2, V>) => Array<{ [P in K1 | K2]: P extends K1 ? V extends true ? T[OptionsConfig<T, K1, K2, V>["firstFieldKey"]] extends string ? string : number : string : string; }>;
11
11
  /**
12
12
  * * Removes duplicate values from an array, supporting deep comparison for objects and arrays.
13
13
  *
@@ -1 +1 @@
1
- {"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../src/array/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE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H,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAE,CAQ3D;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EACjC,GAAG,EAAE,CAAC,EAAE,EACR,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GACb,CAAC,EAAE,CAQL"}
@@ -9,30 +9,48 @@ export type Flattened<T> = T extends (infer U)[] ? Flattened<U> : T;
9
9
  * @typeParam T - The type of the objects in the input array.
10
10
  * @typeParam K1 - The name of the key for the first field in the output (default: `'value'`).
11
11
  * @typeParam K2 - The name of the key for the second field in the output (default: `'label'`).
12
+ * @typeParam V - Whether to keep the `value` field as number if it is a number. Defaults to `false`.
12
13
  */
13
- export interface OptionsConfig<T, K1, K2> {
14
+ export interface OptionsConfig<T, K1, K2, V extends boolean = false> {
14
15
  /**
15
16
  * - The key in the input objects to use for the first field of the option.
16
- * @example If the input objects have an `id` field and you want to use it as the `value` field in the output, set createOptionsArray(data, {firstFieldKey: 'id'}).
17
+ * @example
18
+ * // If the input objects have an `id` field and you want to use it as the `value` field in the output:
19
+ * createOptionsArray(data, {firstFieldKey: 'id'}).
17
20
  */
18
21
  firstFieldKey: PrimitiveKey<T>;
19
22
  /**
20
23
  * - The key in the input objects to use for the second field of the option.
21
- * @example If the input objects have a `name` field and you want to use it as the `label` field in the output, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name'}).
24
+ * @example
25
+ * // If the input objects have a `name` field and you want to use it as the `label` field in the output:
26
+ * createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name'}).
22
27
  */
23
28
  secondFieldKey: PrimitiveKey<T>;
24
29
  /**
25
30
  * - The name of the first field in the output object.
26
31
  * - Defaults to `'value'`.
27
- * @example If you want the output field to be named `'key'` instead of `'value'`, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key'}).
32
+ * @example
33
+ * // If you want the output field to be named `'key'` instead of `'value'`:
34
+ * createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key'}).
28
35
  */
29
36
  firstFieldName?: K1;
30
37
  /**
31
38
  * - The name of the second field in the output object.
32
39
  * - Defaults to `'label'`.
33
- * @example If you want the output field to be named `'title'` instead of `'label'`, set createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key', secondFieldName: 'title'}).
40
+ * @example
41
+ * // If you want the output field to be named `'title'` instead of `'label'`:
42
+ * createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', firstFieldName: 'key', secondFieldName: 'title'}).
34
43
  */
35
44
  secondFieldName?: K2;
45
+ /**
46
+ * - If `true`, and the value from `firstFieldKey` is a number if it is originally number, it will be retained as a number.
47
+ * - Otherwise, all values are converted to strings.
48
+ * Defaults to `false`.
49
+ * @example
50
+ * // Set `numberAsNumber: true` to keep numeric values (like `id`) as numbers instead of strings.
51
+ * createOptionsArray(data, {firstFieldKey: 'id', secondFieldKey: 'name', numberAsNumber: true}).
52
+ */
53
+ numberAsNumber?: V;
36
54
  }
37
55
  /** * Option for sorting order. */
38
56
  export interface OrderOption {
@@ -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,uCAAuC;AACvC,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,CAA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AW,WAAW,CAAC,CAAC,GAAG,OAAO;IACvC,4EAA4E;IAC5E,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,2EAA2E;IAC3E,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,uFAAuF;IACvF,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,2GAA2G;IAC3G,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,kEAAkE;IAClE,IAAI,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;CACzB"}
@@ -1,3 +1,4 @@
1
+ import { isNumber } from '../guards/primitives';
1
2
  import { isDeepEqual } from '../utils';
2
3
  /**
3
4
  * * Converts an array of objects into a formatted array of options.
@@ -7,10 +8,12 @@ import { isDeepEqual } from '../utils';
7
8
  * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
8
9
  */
9
10
  export const createOptionsArray = (data, config) => {
10
- const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
11
- if (data && data.length) {
12
- return data.map((datum) => ({
13
- [firstFieldName]: String(datum[firstFieldKey] ?? ''),
11
+ const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', numberAsNumber = false, } = config || {};
12
+ if (data && data?.length) {
13
+ return data?.map((datum) => ({
14
+ [firstFieldName]: numberAsNumber && isNumber(datum[firstFieldKey]) ?
15
+ datum[firstFieldKey]
16
+ : String(datum[firstFieldKey] ?? ''),
14
17
  [secondFieldName]: String(datum[secondFieldKey] ?? ''),
15
18
  }));
16
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.10.47",
3
+ "version": "4.10.50",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -157,7 +157,7 @@
157
157
  "lint": "node scripts/lint.mjs",
158
158
  "fix": "node scripts/fix.mjs",
159
159
  "commit": "node scripts/commit.mjs",
160
- "count": "node scripts/exports.mjs",
160
+ "count": "node scripts/count.mjs",
161
161
  "types": "node scripts/types.mjs"
162
162
  }
163
163
  }