nhb-toolbox 4.10.48 → 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.
|
@@ -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
|
|
21
|
-
return data
|
|
22
|
-
[firstFieldName]:
|
|
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":"
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../src/array/transform.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAC9B,CAAC,SAAS,aAAa,EACvB,EAAE,SAAS,MAAM,YACjB,EAAE,SAAS,MAAM,YACjB,CAAC,SAAS,OAAO,gBAEX,CAAC,EAAE,UACD,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,KACjC,KAAK,CAAC,GACP,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,SAAS,EAAE,GAC3B,CAAC,SAAS,IAAI,GACb,CAAC,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,SAAS,MAAM,GAC7D,MAAM,GACL,MAAM,GACP,MAAM,GACP,MAAM,GACR,CAwCA,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAK5D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAgBhD;AAED;;;;;GAKG;AAEH;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,EACvC,MAAM,EAAE,CAAC,EAAE,EACX,MAAM,EAAE,CAAC,EAAE,EACX,WAAW,EAAE,YAAY,GAAG,aAAa,GACvC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAKX;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,CAQhE;AAED;;;;;;GAMG;AACH,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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,SAAS,OAAO,GAAG,KAAK;IAClE;;;;;OAKG;IACH,aAAa,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAE/B;;;;;OAKG;IACH,cAAc,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAEhC;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,EAAE,CAAC;IAEpB;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,EAAE,CAAC;IAErB;;;;;;;OAOG;IACH,cAAc,CAAC,EAAE,CAAC,CAAC;CACnB;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;IACzE,2DAA2D;IAC3D,WAAW,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC;CACnC;AAED,mCAAmC;AACnC,MAAM,MAAM,WAAW,CAAC,CAAC,IACxB,CAAC,SAAS,aAAa,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;AAEzD,0DAA0D;AAC1D,MAAM,WAAW,UAAU;IAC1B,oFAAoF;IACpF,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,qFAAqF;IACrF,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,qDAAqD;AACrD,MAAM,WAAW,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
|
|
12
|
-
return data
|
|
13
|
-
[firstFieldName]:
|
|
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.
|
|
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/
|
|
160
|
+
"count": "node scripts/count.mjs",
|
|
161
161
|
"types": "node scripts/types.mjs"
|
|
162
162
|
}
|
|
163
163
|
}
|