nhb-toolbox 1.1.6 → 1.2.1

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/dist/index.d.ts CHANGED
@@ -6,4 +6,5 @@ export { convertHexToHsl, convertHexToRgb, convertHslToHex, convertHslToRgb, con
6
6
  export { flattenArray, createOptionsArray, sortAnArray, filterArrayOfObjects, } from './array';
7
7
  export { generateQueryParams, cloneObject, isDeepEqual, mergeObjects, flattenObject, mergeAndFlattenObjects, isEmptyObject, countObjectFields, } from './object';
8
8
  export { sanitizeData } from './object/sanitize';
9
+ export { convertObjectValues } from './object/convert';
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,UAAU,GACV,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C,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,EAClB,WAAW,EACX,oBAAoB,GACpB,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;AAElB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,UAAU,GACV,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C,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,EAClB,WAAW,EACX,oBAAoB,GACpB,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;AAElB,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sanitizeData = exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.filterArrayOfObjects = exports.sortAnArray = exports.createOptionsArray = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForInitial = exports.convertToDecimal = exports.getRandomNumber = exports.trimString = exports.generateRandomID = exports.truncateString = exports.capitalizeString = void 0;
3
+ exports.convertObjectValues = exports.sanitizeData = exports.countObjectFields = exports.isEmptyObject = exports.mergeAndFlattenObjects = exports.flattenObject = exports.mergeObjects = exports.isDeepEqual = exports.cloneObject = exports.generateQueryParams = exports.filterArrayOfObjects = exports.sortAnArray = exports.createOptionsArray = exports.flattenArray = exports.convertRgbToHsl = exports.convertRgbToHex = exports.convertHslToRgb = exports.convertHslToHex = exports.convertHexToRgb = exports.convertHexToHsl = exports.generateRandomColor = exports.getColorForInitial = exports.convertToDecimal = exports.getRandomNumber = exports.trimString = 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; } });
@@ -36,3 +36,5 @@ Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: functio
36
36
  Object.defineProperty(exports, "countObjectFields", { enumerable: true, get: function () { return object_1.countObjectFields; } });
37
37
  var sanitize_1 = require("./object/sanitize");
38
38
  Object.defineProperty(exports, "sanitizeData", { enumerable: true, get: function () { return sanitize_1.sanitizeData; } });
39
+ var convert_2 = require("./object/convert");
40
+ Object.defineProperty(exports, "convertObjectValues", { enumerable: true, get: function () { return convert_2.convertObjectValues; } });
@@ -0,0 +1,57 @@
1
+ import type { DotNotationKey, GenericObject, Numberified, Stringified } from './types';
2
+ /**
3
+ * * Converts the values of specified keys in an object to strings.
4
+ * * Supports nested objects using dot-notation keys.
5
+ *
6
+ * @param data The object to convert.
7
+ * @param options Options object specifying the conversion mapping.
8
+ * - `keys`: The keys in the object to be converted (dot-notation supported).
9
+ * - `convertTo`: Must be "string".
10
+ * @returns The modified object with the converted values as strings.
11
+ */
12
+ export declare function convertObjectValues<T extends GenericObject>(data: T, options: {
13
+ keys: DotNotationKey<T>[];
14
+ convertTo: 'string';
15
+ }): Stringified<T>;
16
+ /**
17
+ * * Converts the values of specified keys in an object to numbers.
18
+ * * Supports nested objects using dot-notation keys.
19
+ *
20
+ * @param data The object to convert.
21
+ * @param options Options object specifying the conversion mapping.
22
+ * - `keys`: The keys in the object to be converted (dot-notation supported).
23
+ * - `convertTo`: Must be "number".
24
+ * @returns The modified object with the converted values as numbers.
25
+ */
26
+ export declare function convertObjectValues<T extends GenericObject>(data: T, options: {
27
+ keys: DotNotationKey<T>[];
28
+ convertTo: 'number';
29
+ }): Numberified<T>;
30
+ /**
31
+ * * Converts the values of specified keys in an array of objects to strings.
32
+ * * Supports nested objects using dot-notation keys.
33
+ *
34
+ * @param data The array of objects to convert.
35
+ * @param options Options object specifying the conversion mapping.
36
+ * - `keys`: The keys in the objects to be converted (dot-notation supported).
37
+ * - `convertTo`: Must be "string".
38
+ * @returns The modified array of objects with the converted values as strings.
39
+ */
40
+ export declare function convertObjectValues<T extends GenericObject>(data: T[], options: {
41
+ keys: DotNotationKey<T>[];
42
+ convertTo: 'string';
43
+ }): Stringified<T>[];
44
+ /**
45
+ * * Converts the values of specified keys in an array of objects to numbers.
46
+ * * Supports nested objects using dot-notation keys.
47
+ * @param data The array of objects to convert.
48
+ * @param options Options object specifying the conversion mapping.
49
+ * - `keys`: The keys in the objects to be converted (dot-notation supported).
50
+ * - `convertTo`: Must be "number".
51
+ * @returns The modified array of objects with the converted values as numbers.
52
+ */
53
+ export declare function convertObjectValues<T extends GenericObject>(data: T[], options: {
54
+ keys: DotNotationKey<T>[];
55
+ convertTo: 'number';
56
+ }): Numberified<T>[];
57
+ //# sourceMappingURL=convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/object/convert.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEX,cAAc,EACd,aAAa,EACb,WAAW,EACX,WAAW,EACX,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,aAAa,EAC1D,IAAI,EAAE,CAAC,EACP,OAAO,EAAE;IAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,SAAS,EAAE,QAAQ,CAAA;CAAE,GACzD,WAAW,CAAC,CAAC,CAAC,CAAC;AAElB;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,aAAa,EAC1D,IAAI,EAAE,CAAC,EACP,OAAO,EAAE;IAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,SAAS,EAAE,QAAQ,CAAA;CAAE,GACzD,WAAW,CAAC,CAAC,CAAC,CAAC;AAElB;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,aAAa,EAC1D,IAAI,EAAE,CAAC,EAAE,EACT,OAAO,EAAE;IAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,SAAS,EAAE,QAAQ,CAAA;CAAE,GACzD,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC;AAEpB;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,aAAa,EAC1D,IAAI,EAAE,CAAC,EAAE,EACT,OAAO,EAAE;IAAE,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,SAAS,EAAE,QAAQ,CAAA;CAAE,GACzD,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertObjectValues = convertObjectValues;
4
+ /**
5
+ * * Converts the values of specified keys in an object or array of objects to either string or number.
6
+ * * Supports nested objects using dot-notation keys.
7
+ *
8
+ * @param data The object or array of objects to convert.
9
+ * @param options Options object specifying the conversion mapping.
10
+ * - `keys`: The keys in the object to be converted (dot-notation supported).
11
+ * - `convertTo`: The target type, either "string" or "number".
12
+ * @returns The modified object or array of objects with the converted values, with updated types.
13
+ */
14
+ function convertObjectValues(data, options) {
15
+ const { keys, convertTo } = options;
16
+ /** * Helper function to determine if value should be preserved. */
17
+ const _shouldPreserveValue = (value) => convertTo === 'number' &&
18
+ typeof value === 'string' &&
19
+ isNaN(Number(value));
20
+ /** * Helper function to resolve a dot-notation key path and modify the corresponding value in the object. */
21
+ const _setValueAtPath = (obj, path, convertTo) => {
22
+ const segments = path.split('.');
23
+ let current = obj;
24
+ segments.forEach((key, index) => {
25
+ if (index === segments.length - 1) {
26
+ const value = current[key];
27
+ if (_shouldPreserveValue(value)) {
28
+ return;
29
+ }
30
+ if (convertTo === 'string' && typeof value !== 'string') {
31
+ current[key] = String(value);
32
+ }
33
+ else if (convertTo === 'number' &&
34
+ typeof value !== 'number') {
35
+ current[key] = Number(value);
36
+ }
37
+ }
38
+ else {
39
+ if (typeof current[key] === 'object' && current[key] !== null) {
40
+ current = current[key];
41
+ }
42
+ else {
43
+ current[key] = {};
44
+ current = current[key];
45
+ }
46
+ }
47
+ });
48
+ return obj;
49
+ };
50
+ /** * Recursively process a single object. */
51
+ const convertValue = (obj) => {
52
+ let newObj = { ...obj };
53
+ keys.forEach((key) => {
54
+ newObj = _setValueAtPath(newObj, key, convertTo);
55
+ });
56
+ return newObj;
57
+ };
58
+ if (Array.isArray(data)) {
59
+ return data.map((d) => convertValue(d));
60
+ }
61
+ return convertValue(data);
62
+ }
@@ -1,8 +1,8 @@
1
1
  /** - Generic object type */
2
2
  export type GenericObject = Record<string, unknown>;
3
- /** - Dot-notation keys for nested objects. */
4
- export type DotNotationKey<T> = T extends object ? {
5
- [K in keyof T & string]: T[K] extends object ? K | `${K}.${DotNotationKey<T[K]>}` : K;
3
+ /** - Dot-notation keys for nested objects */
4
+ export type DotNotationKey<T> = T extends GenericObject ? {
5
+ [K in keyof T & string]: T[K] extends GenericObject ? `${K}` | `${K}.${DotNotationKey<T[K]>}` : `${K}`;
6
6
  }[keyof T & string] : never;
7
7
  /** - Options for `sanitizeData` */
8
8
  export interface SanitizeOptions<T extends GenericObject> {
@@ -15,6 +15,24 @@ export interface SanitizeOptions<T extends GenericObject> {
15
15
  }
16
16
  /** - Data after sanitization. */
17
17
  export type SanitizedData<T> = {
18
- [P in keyof T]?: T[P] extends object ? SanitizedData<T[P]> : T[P];
18
+ [P in keyof T]?: T[P] extends GenericObject ? SanitizedData<T[P]> : T[P];
19
+ };
20
+ /** - Dot-notation keys for nested objects. */
21
+ export type KeyConversion<T> = T extends GenericObject ? {
22
+ [K in keyof T & string]: K extends string ? T[K] extends GenericObject ? `${K}` | `${K}.${KeyConversion<T[K]>}` : `${K}` : never;
23
+ }[keyof T & string] : never;
24
+ /**
25
+ * * Determines the return type of `convertObjectValues` based on the `ConvertTo` type.
26
+ * @template T The object type.
27
+ * @template C The conversion type, either "string" or "number".
28
+ */
29
+ export type ConvertedData<T, C extends 'string' | 'number'> = C extends 'string' ? Stringified<T> | Stringified<T>[] : T | T[];
30
+ /** - Type of data value converted to `string` */
31
+ export type Stringified<T> = {
32
+ [K in keyof T]: T[K] extends (infer U)[] ? Stringified<U>[] : T[K] extends object | null | undefined ? Stringified<T[K]> : T[K] extends string | number ? string : T[K];
33
+ };
34
+ /** - Type of data value converted to `number` */
35
+ export type Numberified<T> = {
36
+ [K in keyof T]: T[K] extends (infer U)[] ? Numberified<U>[] : T[K] extends object | null | undefined ? Numberified<T[K]> : T[K] extends string ? number : T[K] extends number ? T[K] : number;
19
37
  };
20
38
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/object/types.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEpD,8CAA8C;AAC9C,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAC7C;KACC,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GACzC,CAAC,GAAG,GAAG,CAAC,IAAI,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GAClC,CAAC;CACJ,CAAC,MAAM,CAAC,GAAG,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AAC,CAAC,CAAC;CACN,CAAC;AAEF,iDAAiD;AACjD,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KAC3B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,GACzD,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAC5B,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAC1B,MAAM;CACR,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Uncontrolled any to use for edge cases */
2
+ export type UncontrolledAny = any;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,eAAe,GAAG,GAAG,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "1.1.6",
3
+ "version": "1.2.1",
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",