nhb-toolbox 0.9.3 → 0.9.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.
@@ -0,0 +1,20 @@
1
+ import type { Flatten, SelectOptions } from './types';
2
+ /**
3
+ * * Flattens a nested array recursively or wraps any non-array data type in an array.
4
+ *
5
+ * @typeParam T : The type of the input, which can be a nested array or a non-array value.
6
+ * @param input - The input value, which can be a nested array or a non-array value.
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
+ */
9
+ export declare const flattenArray: <T>(input: T | T[]) => Flatten<T>[];
10
+ /**
11
+ * * Converts an array of objects into a formatted array of select options.
12
+ *
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.
18
+ */
19
+ export declare const createSelectOptions: <T extends Record<string, string | number | null | undefined>>(data: T[], valueKey: keyof T, labelKey: keyof T) => SelectOptions[];
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSelectOptions = exports.flattenArray = void 0;
4
+ /**
5
+ * * Flattens a nested array recursively or wraps any non-array data type in an array.
6
+ *
7
+ * @typeParam T : The type of the input, which can be a nested array or a non-array value.
8
+ * @param input - The input value, which can be a nested array or a non-array value.
9
+ * @returns A fully flattened array of type `Flatten<T>`. If the input is not an array, it wraps it in a single-element array.
10
+ */
11
+ const flattenArray = (input) => {
12
+ if (!Array.isArray(input))
13
+ return [input];
14
+ return input.reduce((acc, item) => {
15
+ // If item is an array, recursively flatten it; otherwise, add it directly.
16
+ return acc.concat(Array.isArray(item) ? (0, exports.flattenArray)(item) : [item]);
17
+ }, []);
18
+ };
19
+ exports.flattenArray = flattenArray;
20
+ /**
21
+ * * Converts an array of objects into a formatted array of select options.
22
+ *
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.
28
+ */
29
+ const createSelectOptions = (data, valueKey, labelKey) => {
30
+ if (data && data.length) {
31
+ return data.map((datum) => ({
32
+ value: String(datum[valueKey] ?? ''),
33
+ label: String(datum[labelKey] ?? ''),
34
+ }));
35
+ }
36
+ else {
37
+ return [];
38
+ }
39
+ };
40
+ exports.createSelectOptions = createSelectOptions;
@@ -0,0 +1,9 @@
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
+ }
8
+ /** */
9
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /** */
4
+ // export interface
@@ -1,7 +1,8 @@
1
1
  /** - A string, number for generating color. */
2
2
  export type ColorInput = string | number;
3
3
  /** - An array of strings/numbers or nested arrays of strings/numbers for generating colors. */
4
- export type ColorInputArray = (ColorInput | ColorInputArray)[];
4
+ export interface ColorInputArray extends Array<ColorInput | ColorInputArray> {
5
+ }
5
6
  /** - Opacity options */
6
7
  export type OpacityValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100;
7
8
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,MAAM,eAAe,GAAG,CAAC,UAAU,GAAG,eAAe,CAAC,EAAE,CAAC;AAE/D,wBAAwB;AACxB,MAAM,MAAM,YAAY,GACrB,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,GAAG,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/colors/types.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEzC,+FAA+F;AAC/F,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,UAAU,GAAG,eAAe,CAAC;CAAG;AAE/E,wBAAwB;AACxB,MAAM,MAAM,YAAY,GACrB,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,CAAC,GACD,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,EAAE,GACF,GAAG,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { capitalizeString, truncateString, generateRandomID } from './string';
2
2
  export { getColorForFirstCharacter } from './colors';
3
+ export { flattenArray, createSelectOptions } from './array';
4
+ export { generateQueryParams } from './object';
3
5
  //# 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,yBAAyB,EAAE,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,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAErD,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -1,9 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getColorForFirstCharacter = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
3
+ exports.generateQueryParams = exports.createSelectOptions = exports.flattenArray = exports.getColorForFirstCharacter = 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; } });
7
7
  Object.defineProperty(exports, "generateRandomID", { enumerable: true, get: function () { return string_1.generateRandomID; } });
8
8
  var colors_1 = require("./colors");
9
9
  Object.defineProperty(exports, "getColorForFirstCharacter", { enumerable: true, get: function () { return colors_1.getColorForFirstCharacter; } });
10
+ var array_1 = require("./array");
11
+ Object.defineProperty(exports, "flattenArray", { enumerable: true, get: function () { return array_1.flattenArray; } });
12
+ Object.defineProperty(exports, "createSelectOptions", { enumerable: true, get: function () { return array_1.createSelectOptions; } });
13
+ var object_1 = require("./object");
14
+ Object.defineProperty(exports, "generateQueryParams", { enumerable: true, get: function () { return object_1.generateQueryParams; } });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * * Utility function to generate query parameters from an object.
3
+ *
4
+ * @template T - A generic type extending `Record<string, string | number | string[] | number[]>`.
5
+ * @param params - Object containing query parameters.
6
+ * @returns A query string as a URL-encoded string, e.g., `?key1=value1&key2=value2`.
7
+ *
8
+ * @example
9
+ * generateQueryParams({ key1: 'value1', key2: 42 }); // "?key1=value1&key2=42"
10
+ * generateQueryParams({ key1: ['value1', 'value2'], key2: 42 }); // "?key1=value1&key1=value2&key2=42"
11
+ */
12
+ export declare const generateQueryParams: <T extends Record<string, string | number | string[] | number[]>>(params?: T) => string;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,GAC/B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,WAEvD,CAAC,KACP,MAkBF,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateQueryParams = void 0;
4
+ /**
5
+ * * Utility function to generate query parameters from an object.
6
+ *
7
+ * @template T - A generic type extending `Record<string, string | number | string[] | number[]>`.
8
+ * @param params - Object containing query parameters.
9
+ * @returns A query string as a URL-encoded string, e.g., `?key1=value1&key2=value2`.
10
+ *
11
+ * @example
12
+ * generateQueryParams({ key1: 'value1', key2: 42 }); // "?key1=value1&key2=42"
13
+ * generateQueryParams({ key1: ['value1', 'value2'], key2: 42 }); // "?key1=value1&key1=value2&key2=42"
14
+ */
15
+ const generateQueryParams = (params = {}) => {
16
+ const queryParams = Object.entries(params)
17
+ .filter(([_, value]) => value !== undefined && value !== null)
18
+ .flatMap(([key, value]) => Array.isArray(value)
19
+ ? value.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`)
20
+ : `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
21
+ .join('&');
22
+ return queryParams ? `?${queryParams}` : '';
23
+ };
24
+ exports.generateQueryParams = generateQueryParams;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
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",