nhb-toolbox 1.0.1 → 1.0.2

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,4 +1,4 @@
1
- import type { Flatten, SelectOptions } from './types';
1
+ import type { Flattened, ConfigOptions, OptionInput } from './types';
2
2
  /**
3
3
  * * Flattens a nested array recursively or wraps any non-array data type in an array.
4
4
  *
@@ -6,15 +6,14 @@ import type { Flatten, SelectOptions } from './types';
6
6
  * @param input - The input value, which can be a nested array or a non-array value.
7
7
  * @returns A fully flattened array of type `Flatten<T>`. If the input is not an array, it wraps it in a single-element array.
8
8
  */
9
- export declare const flattenArray: <T>(input: T | T[]) => Flatten<T>[];
9
+ export declare const flattenArray: <T>(input: T | T[]) => Flattened<T>[];
10
10
  /**
11
- * * Converts an array of objects into a formatted array of select options.
11
+ * * Converts an array of objects into a formatted array of options.
12
12
  *
13
13
  * @typeParam T - The type of each object in the `data` array.
14
- * @param data - An array of objects to convert into select options.
15
- * @param valueKey - The key in each object to use as the `value` field in the select options.
16
- * @param labelKey - The key in each object to use as the `label` field in the select options.
17
- * @returns An array of select options, where each option has a `value` and `label` field.
14
+ * @param data - An array of objects to convert into options.
15
+ * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
16
+ * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
18
17
  */
19
- export declare const createSelectOptions: <T extends Record<string, string | number | null | undefined>>(data: T[], valueKey: keyof T, labelKey: keyof T) => SelectOptions[];
18
+ export declare const createOptionsArray: <T extends OptionInput, K1 extends string = "value", K2 extends string = "label">(data: T[], config: ConfigOptions<T, K1, K2>) => { [P in K1 | K2]: string; }[];
20
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,KAAG,OAAO,CAAC,CAAC,CAAC,EAO1D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,GAC/B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,QAEtD,CAAC,EAAE,YACC,MAAM,CAAC,YACP,MAAM,CAAC,KACf,aAAa,EASf,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAErE;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,KAAG,SAAS,CAAC,CAAC,CAAC,EAO5D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAC9B,CAAC,SAAS,WAAW,EACrB,EAAE,SAAS,MAAM,YACjB,EAAE,SAAS,MAAM,kBAEX,CAAC,EAAE,UACD,aAAa,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,KAC9B,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,GAAE,EAe5B,CAAC"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelectOptions = exports.flattenArray = void 0;
3
+ exports.createOptionsArray = exports.flattenArray = void 0;
4
4
  /**
5
5
  * * Flattens a nested array recursively or wraps any non-array data type in an array.
6
6
  *
@@ -18,23 +18,23 @@ const flattenArray = (input) => {
18
18
  };
19
19
  exports.flattenArray = flattenArray;
20
20
  /**
21
- * * Converts an array of objects into a formatted array of select options.
21
+ * * Converts an array of objects into a formatted array of options.
22
22
  *
23
23
  * @typeParam T - The type of each object in the `data` array.
24
- * @param data - An array of objects to convert into select options.
25
- * @param valueKey - The key in each object to use as the `value` field in the select options.
26
- * @param labelKey - The key in each object to use as the `label` field in the select options.
27
- * @returns An array of select options, where each option has a `value` and `label` field.
24
+ * @param data - An array of objects to convert into options.
25
+ * @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
26
+ * @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
28
27
  */
29
- const createSelectOptions = (data, valueKey, labelKey) => {
28
+ const createOptionsArray = (data, config) => {
29
+ const { firstFieldKey, secondFieldKey, firstFieldName = 'value', secondFieldName = 'label', } = config || {};
30
30
  if (data && data.length) {
31
31
  return data.map((datum) => ({
32
- value: String(datum[valueKey] ?? ''),
33
- label: String(datum[labelKey] ?? ''),
32
+ [firstFieldName]: String(datum[firstFieldKey] ?? ''),
33
+ [secondFieldName]: String(datum[secondFieldKey] ?? ''),
34
34
  }));
35
35
  }
36
36
  else {
37
37
  return [];
38
38
  }
39
39
  };
40
- exports.createSelectOptions = createSelectOptions;
40
+ exports.createOptionsArray = createOptionsArray;
@@ -1,9 +1,37 @@
1
1
  /** - Flatten Array */
2
- export type Flatten<T> = T extends (infer U)[] ? Flatten<U> : T;
3
- /** - Select Options */
4
- export interface SelectOptions {
5
- value: string;
6
- label: string;
7
- }
2
+ export type Flattened<T> = T extends (infer U)[] ? Flattened<U> : T;
8
3
  /** */
4
+ export type OptionInput = Record<string, string | number | null | undefined>;
5
+ /**
6
+ * - Configuration for `createOptionsArray`.
7
+ * - Defines the mapping between keys in the input objects and the keys in the output options.
8
+ *
9
+ * @typeParam T - The type of the objects in the input array.
10
+ * @typeParam K1 - The name of the key for the first field in the output (default: `'value'`).
11
+ * @typeParam K2 - The name of the key for the second field in the output (default: `'label'`).
12
+ */
13
+ export interface ConfigOptions<T extends Record<string, string | number | null | undefined>, K1 extends string = 'value', K2 extends string = 'label'> {
14
+ /**
15
+ * - 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
+ */
18
+ firstFieldKey: keyof T;
19
+ /**
20
+ * - 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'}).
22
+ */
23
+ secondFieldKey: keyof T;
24
+ /**
25
+ * - The name of the first field in the output object.
26
+ * - 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'}).
28
+ */
29
+ firstFieldName?: K1;
30
+ /**
31
+ * - The name of the second field in the output object.
32
+ * - 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'}).
34
+ */
35
+ secondFieldName?: K2;
36
+ }
9
37
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEhE,uBAAuB;AACvB,MAAM,WAAW,aAAa;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;CACd;AAED,OAAO"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/array/types.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEpE,OAAO;AACP,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa,CAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAC5D,EAAE,SAAS,MAAM,GAAG,OAAO,EAC3B,EAAE,SAAS,MAAM,GAAG,OAAO;IAE3B;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC,CAAC;IAEvB;;;OAGG;IACH,cAAc,EAAE,MAAM,CAAC,CAAC;IAExB;;;;OAIG;IACH,cAAc,CAAC,EAAE,EAAE,CAAC;IAEpB;;;;OAIG;IACH,eAAe,CAAC,EAAE,EAAE,CAAC;CACrB"}
@@ -1,4 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /** */
4
- // export interface
package/dist/index.d.ts CHANGED
@@ -3,6 +3,6 @@ export { getRandomNumber, convertToDecimal } from './number';
3
3
  export { getColorForFirstCharacter } from './colors';
4
4
  export { generateRandomColor } from './colors/random';
5
5
  export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, convertRgbToHex, convertRgbToHsl, } from './colors/convert';
6
- export { flattenArray, createSelectOptions } from './array';
6
+ export { flattenArray, createOptionsArray, } from './array';
7
7
  export { generateQueryParams, cloneObject, isDeepEqual, mergeObjects, flattenObject, mergeAndFlattenObjects, isEmptyObject, countObjectFields, } from './object';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAErD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EACN,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE5D,OAAO,EACN,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,aAAa,EACb,iBAAiB,GACjB,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAErD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EACN,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,EACf,eAAe,GACf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACN,YAAY,EACZ,kBAAkB,GAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACN,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,aAAa,EACb,iBAAiB,GACjB,MAAM,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.createSelectOptions = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForFirstCharacter = exports.convertToDecimal = exports.getRandomNumber = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
3
+ exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.createOptionsArray = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForFirstCharacter = exports.convertToDecimal = exports.getRandomNumber = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
4
4
  var string_1 = require("./string");
5
5
  Object.defineProperty(exports, "capitalizeString", { enumerable: true, get: function () { return string_1.capitalizeString; } });
6
6
  Object.defineProperty(exports, "truncateString", { enumerable: true, get: function () { return string_1.truncateString; } });
@@ -21,7 +21,7 @@ Object.defineProperty(exports, "convertRgbToHex", { enumerable: true, get: funct
21
21
  Object.defineProperty(exports, "convertRgbToHsl", { enumerable: true, get: function () { return convert_1.convertRgbToHsl; } });
22
22
  var array_1 = require("./array");
23
23
  Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function () { return array_1.flattenArray; } });
24
- Object.defineProperty(exports, "createSelectOptions", { enumerable: true, get: function () { return array_1.createSelectOptions; } });
24
+ Object.defineProperty(exports, "createOptionsArray", { enumerable: true, get: function () { return array_1.createOptionsArray; } });
25
25
  var object_1 = require("./object");
26
26
  Object.defineProperty(exports, "generateQueryParams", { enumerable: true, get: function () { return object_1.generateQueryParams; } });
27
27
  Object.defineProperty(exports, "cloneObject", { enumerable: true, get: function () { return object_1.cloneObject; } });
@@ -9,7 +9,7 @@ export interface RandomNumberOptions {
9
9
  /** Whether to include the maximum number. */
10
10
  includeMax?: boolean;
11
11
  }
12
- /** Decimal options for converting to decimal */
12
+ /** - Decimal options for converting to decimal */
13
13
  export interface DecimalOptions {
14
14
  /** Number of decimal places to round to. Defaults to `2`. */
15
15
  decimalPlaces?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/number/types.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,gDAAgD;AAChD,MAAM,WAAW,cAAc;IAC9B,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/number/types.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC9B,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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",