nhb-toolbox 4.1.3 → 4.1.4
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/cjs/object/objectify.js +10 -2
- package/dist/cjs/utils/index.js +17 -6
- package/dist/dts/object/objectify.d.ts +11 -3
- package/dist/dts/object/objectify.d.ts.map +1 -1
- package/dist/dts/object/sanitize.d.ts +2 -2
- package/dist/dts/object/sanitize.d.ts.map +1 -1
- package/dist/dts/utils/index.d.ts +16 -6
- package/dist/dts/utils/index.d.ts.map +1 -1
- package/dist/esm/object/objectify.js +10 -2
- package/dist/esm/utils/index.js +17 -6
- package/package.json +1 -1
|
@@ -216,12 +216,20 @@ const extractUpdatedAndNewFields = (baseObject, updatedObject) => {
|
|
|
216
216
|
exports.extractUpdatedAndNewFields = extractUpdatedAndNewFields;
|
|
217
217
|
/**
|
|
218
218
|
* * Safely parses a JSON string into an object.
|
|
219
|
-
*
|
|
220
|
-
* *Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).*
|
|
219
|
+
* * Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).
|
|
221
220
|
*
|
|
222
221
|
* @param value - The JSON string to parse.
|
|
223
222
|
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: `true`).
|
|
224
223
|
* @returns A parsed object with primitive conversions, or an empty object on failure or if the root is not a valid object.
|
|
224
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
225
|
+
*
|
|
226
|
+
* - **N.B.** This function will return an empty object if the JSON string is invalid or if the root element is not an object.
|
|
227
|
+
*
|
|
228
|
+
* - *Unlike `parseJSON`, which returns any valid JSON structure (including arrays, strings, numbers, etc.),
|
|
229
|
+
* this function strictly ensures that the result is an object and optionally transforms stringified primitives.*
|
|
230
|
+
*
|
|
231
|
+
* @see parseJSON - For parsing generic JSON values (arrays, numbers, etc.) with optional primitive transformation.
|
|
232
|
+
*
|
|
225
233
|
*/
|
|
226
234
|
const parseJsonToObject = (value, parsePrimitives = true) => {
|
|
227
235
|
try {
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -176,16 +176,25 @@ function getClassDetails(cls) {
|
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
178
|
/**
|
|
179
|
-
* * Parses
|
|
179
|
+
* * Parses any valid JSON string, optionally converting stringified primitives inside (nested) arrays or objects.
|
|
180
180
|
*
|
|
181
|
+
* @template T - Expected return type (default is unknown).
|
|
181
182
|
* @param value - The JSON string to parse.
|
|
182
183
|
* @param parsePrimitives - Whether to convert stringified primitives (default: `true`).
|
|
183
|
-
* @returns The parsed JSON value with optional primitive
|
|
184
|
+
* @returns The parsed JSON value typed as `T`, or the original parsed value with optional primitive conversion.
|
|
185
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
186
|
+
*
|
|
187
|
+
* - *Unlike `parseJsonToObject`, which ensures the root value is an object,
|
|
188
|
+
* this function returns any valid JSON structure such as arrays, strings, numbers, or objects.*
|
|
189
|
+
*
|
|
190
|
+
* This is useful when you're not sure of the root structure of the JSON, or when you expect something other than an object.
|
|
191
|
+
*
|
|
192
|
+
* @see `parseJsonToObject` for strict object-only parsing.
|
|
184
193
|
*/
|
|
185
194
|
const parseJSON = (value, parsePrimitives = true) => {
|
|
186
195
|
try {
|
|
187
196
|
const parsed = JSON.parse(value);
|
|
188
|
-
return parsePrimitives ? deepParsePrimitives(parsed) : parsed;
|
|
197
|
+
return (parsePrimitives ? deepParsePrimitives(parsed) : parsed);
|
|
189
198
|
}
|
|
190
199
|
catch {
|
|
191
200
|
return {};
|
|
@@ -193,10 +202,11 @@ const parseJSON = (value, parsePrimitives = true) => {
|
|
|
193
202
|
};
|
|
194
203
|
exports.parseJSON = parseJSON;
|
|
195
204
|
/**
|
|
196
|
-
* Recursively parses primitive values inside objects and arrays.
|
|
205
|
+
* * Recursively parses primitive values inside objects and arrays.
|
|
197
206
|
*
|
|
207
|
+
* @template T - Expected return type after parsing (default is unknown).
|
|
198
208
|
* @param input - Any input value to parse recursively.
|
|
199
|
-
* @returns Input with primitives (strings like "true", "123") converted
|
|
209
|
+
* @returns Input with primitives (strings like "true", "123") converted, typed as `T`.
|
|
200
210
|
*/
|
|
201
211
|
function deepParsePrimitives(input) {
|
|
202
212
|
if (Array.isArray(input)) {
|
|
@@ -211,7 +221,7 @@ function deepParsePrimitives(input) {
|
|
|
211
221
|
}
|
|
212
222
|
if ((0, primitives_1.isString)(input)) {
|
|
213
223
|
if (/^(true|false)$/i.test(input)) {
|
|
214
|
-
return input.toLowerCase() === 'true';
|
|
224
|
+
return (input.toLowerCase() === 'true');
|
|
215
225
|
}
|
|
216
226
|
if ((0, specials_1.isNumericString)(input)) {
|
|
217
227
|
return Number(input);
|
|
@@ -222,6 +232,7 @@ function deepParsePrimitives(input) {
|
|
|
222
232
|
if (input === 'undefined') {
|
|
223
233
|
return undefined;
|
|
224
234
|
}
|
|
235
|
+
return input;
|
|
225
236
|
}
|
|
226
237
|
return input;
|
|
227
238
|
}
|
|
@@ -56,12 +56,20 @@ export declare const extractNewFields: <T extends GenericObject, U extends Gener
|
|
|
56
56
|
export declare const extractUpdatedAndNewFields: <T extends GenericObject, U extends GenericObject>(baseObject: T, updatedObject: FlattenPartial<T> & FlattenPartial<U>) => FlattenPartial<T> & FlattenPartial<U>;
|
|
57
57
|
/**
|
|
58
58
|
* * Safely parses a JSON string into an object.
|
|
59
|
-
*
|
|
60
|
-
* *Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).*
|
|
59
|
+
* * Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).
|
|
61
60
|
*
|
|
62
61
|
* @param value - The JSON string to parse.
|
|
63
62
|
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: `true`).
|
|
64
63
|
* @returns A parsed object with primitive conversions, or an empty object on failure or if the root is not a valid object.
|
|
64
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
65
|
+
*
|
|
66
|
+
* - **N.B.** This function will return an empty object if the JSON string is invalid or if the root element is not an object.
|
|
67
|
+
*
|
|
68
|
+
* - *Unlike `parseJSON`, which returns any valid JSON structure (including arrays, strings, numbers, etc.),
|
|
69
|
+
* this function strictly ensures that the result is an object and optionally transforms stringified primitives.*
|
|
70
|
+
*
|
|
71
|
+
* @see parseJSON - For parsing generic JSON values (arrays, numbers, etc.) with optional primitive transformation.
|
|
72
|
+
*
|
|
65
73
|
*/
|
|
66
|
-
export declare const parseJsonToObject: (value: string, parsePrimitives?: boolean) =>
|
|
74
|
+
export declare const parseJsonToObject: <T = GenericObject>(value: string, parsePrimitives?: boolean) => T;
|
|
67
75
|
//# sourceMappingURL=objectify.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objectify.d.ts","sourceRoot":"","sources":["../../../src/object/objectify.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAO,cAAc,EAAE,MAAM,UAAU,CAAC;AAGpD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,aAAa,cAAc,CAAC,EAAE,KAAG,CAuCvE,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,aAAa,cACjD,CAAC,EAAE,KACb,aAyBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS,aAAa,UACpD,CAAC,KACP,CAmBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,GAAI,CAAC,SAAS,aAAa,UACvD,CAAC,KACP,aAkCF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS,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,KAC9B,cAAc,CAAC,CAAC,CAwBlB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAC5B,CAAC,SAAS,aAAa,EACvB,CAAC,SAAS,aAAa,cAEX,CAAC,iBACE,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,KAClD,cAAc,CAAC,CAAC,CAyBlB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GACtC,CAAC,SAAS,aAAa,EACvB,CAAC,SAAS,aAAa,cAEX,CAAC,iBACE,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,KAClD,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAwBtC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,yBAC3B,MAAM,gCAEX,CAYF,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FlattenPartial } from '../types';
|
|
2
|
-
import type { GenericObject, SanitizeOptions
|
|
2
|
+
import type { GenericObject, SanitizeOptions } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* * Trims all the words in a string.
|
|
5
5
|
*
|
|
@@ -41,5 +41,5 @@ export declare function sanitizeData<T>(array: T[], options?: SanitizeOptions<T>
|
|
|
41
41
|
* @param object - The object with potentially stringified primitive values.
|
|
42
42
|
* @returns A new object with parsed values converted to their original types.
|
|
43
43
|
*/
|
|
44
|
-
export declare function parseObjectValues(object: GenericObject):
|
|
44
|
+
export declare function parseObjectValues<T = GenericObject>(object: GenericObject): T;
|
|
45
45
|
//# sourceMappingURL=sanitize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/object/sanitize.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/object/sanitize.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,KAAK,EAAkB,aAAa,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE9E;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAEpD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;AAExD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,aAAa,EACnD,MAAM,EAAE,CAAC,EACT,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,cAAc,CAAC,CAAC,CAAC,CAAC;AAErB;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC1B,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC;AA8KvB;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,aAAa,EAAE,MAAM,EAAE,aAAa,GAAG,CAAC,CAgC7E"}
|
|
@@ -81,18 +81,28 @@ export declare function countStaticMethods(cls: Constructor): number;
|
|
|
81
81
|
*/
|
|
82
82
|
export declare function getClassDetails(cls: Constructor): ClassDetails;
|
|
83
83
|
/**
|
|
84
|
-
* * Parses
|
|
84
|
+
* * Parses any valid JSON string, optionally converting stringified primitives inside (nested) arrays or objects.
|
|
85
85
|
*
|
|
86
|
+
* @template T - Expected return type (default is unknown).
|
|
86
87
|
* @param value - The JSON string to parse.
|
|
87
88
|
* @param parsePrimitives - Whether to convert stringified primitives (default: `true`).
|
|
88
|
-
* @returns The parsed JSON value with optional primitive
|
|
89
|
+
* @returns The parsed JSON value typed as `T`, or the original parsed value with optional primitive conversion.
|
|
90
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
91
|
+
*
|
|
92
|
+
* - *Unlike `parseJsonToObject`, which ensures the root value is an object,
|
|
93
|
+
* this function returns any valid JSON structure such as arrays, strings, numbers, or objects.*
|
|
94
|
+
*
|
|
95
|
+
* This is useful when you're not sure of the root structure of the JSON, or when you expect something other than an object.
|
|
96
|
+
*
|
|
97
|
+
* @see `parseJsonToObject` for strict object-only parsing.
|
|
89
98
|
*/
|
|
90
|
-
export declare const parseJSON: (value: string, parsePrimitives?: boolean) =>
|
|
99
|
+
export declare const parseJSON: <T = unknown>(value: string, parsePrimitives?: boolean) => T;
|
|
91
100
|
/**
|
|
92
|
-
* Recursively parses primitive values inside objects and arrays.
|
|
101
|
+
* * Recursively parses primitive values inside objects and arrays.
|
|
93
102
|
*
|
|
103
|
+
* @template T - Expected return type after parsing (default is unknown).
|
|
94
104
|
* @param input - Any input value to parse recursively.
|
|
95
|
-
* @returns Input with primitives (strings like "true", "123") converted
|
|
105
|
+
* @returns Input with primitives (strings like "true", "123") converted, typed as `T`.
|
|
96
106
|
*/
|
|
97
|
-
export declare function deepParsePrimitives(input: unknown):
|
|
107
|
+
export declare function deepParsePrimitives<T = unknown>(input: unknown): T;
|
|
98
108
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACX,YAAY,EACZ,WAAW,EACX,SAAS,EACT,WAAW,EACX,YAAY,EACZ,MAAM,UAAU,CAAC;AAElB;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAG,OA6B3C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAC9B,CAAC,EAAE,cACC,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Ac,GAAI,CAAC,SAAS,YAAY,YAC1C,CAAC,qBAET,WAAW,CAAC,CAAC,CAWf,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,EAAE,CAcjE;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,EAAE,CAQ/D;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,CAE7D;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,WAAW,GAAG,MAAM,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,WAAW,GAAG,YAAY,CAW9D;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,mBACnB,MAAM,gCAEX,CAOF,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAoClE"}
|
|
@@ -206,12 +206,20 @@ export const extractUpdatedAndNewFields = (baseObject, updatedObject) => {
|
|
|
206
206
|
};
|
|
207
207
|
/**
|
|
208
208
|
* * Safely parses a JSON string into an object.
|
|
209
|
-
*
|
|
210
|
-
* *Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).*
|
|
209
|
+
* * Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).
|
|
211
210
|
*
|
|
212
211
|
* @param value - The JSON string to parse.
|
|
213
212
|
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: `true`).
|
|
214
213
|
* @returns A parsed object with primitive conversions, or an empty object on failure or if the root is not a valid object.
|
|
214
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
215
|
+
*
|
|
216
|
+
* - **N.B.** This function will return an empty object if the JSON string is invalid or if the root element is not an object.
|
|
217
|
+
*
|
|
218
|
+
* - *Unlike `parseJSON`, which returns any valid JSON structure (including arrays, strings, numbers, etc.),
|
|
219
|
+
* this function strictly ensures that the result is an object and optionally transforms stringified primitives.*
|
|
220
|
+
*
|
|
221
|
+
* @see parseJSON - For parsing generic JSON values (arrays, numbers, etc.) with optional primitive transformation.
|
|
222
|
+
*
|
|
215
223
|
*/
|
|
216
224
|
export const parseJsonToObject = (value, parsePrimitives = true) => {
|
|
217
225
|
try {
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -163,26 +163,36 @@ export function getClassDetails(cls) {
|
|
|
163
163
|
};
|
|
164
164
|
}
|
|
165
165
|
/**
|
|
166
|
-
* * Parses
|
|
166
|
+
* * Parses any valid JSON string, optionally converting stringified primitives inside (nested) arrays or objects.
|
|
167
167
|
*
|
|
168
|
+
* @template T - Expected return type (default is unknown).
|
|
168
169
|
* @param value - The JSON string to parse.
|
|
169
170
|
* @param parsePrimitives - Whether to convert stringified primitives (default: `true`).
|
|
170
|
-
* @returns The parsed JSON value with optional primitive
|
|
171
|
+
* @returns The parsed JSON value typed as `T`, or the original parsed value with optional primitive conversion.
|
|
172
|
+
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
|
|
173
|
+
*
|
|
174
|
+
* - *Unlike `parseJsonToObject`, which ensures the root value is an object,
|
|
175
|
+
* this function returns any valid JSON structure such as arrays, strings, numbers, or objects.*
|
|
176
|
+
*
|
|
177
|
+
* This is useful when you're not sure of the root structure of the JSON, or when you expect something other than an object.
|
|
178
|
+
*
|
|
179
|
+
* @see `parseJsonToObject` for strict object-only parsing.
|
|
171
180
|
*/
|
|
172
181
|
export const parseJSON = (value, parsePrimitives = true) => {
|
|
173
182
|
try {
|
|
174
183
|
const parsed = JSON.parse(value);
|
|
175
|
-
return parsePrimitives ? deepParsePrimitives(parsed) : parsed;
|
|
184
|
+
return (parsePrimitives ? deepParsePrimitives(parsed) : parsed);
|
|
176
185
|
}
|
|
177
186
|
catch {
|
|
178
187
|
return {};
|
|
179
188
|
}
|
|
180
189
|
};
|
|
181
190
|
/**
|
|
182
|
-
* Recursively parses primitive values inside objects and arrays.
|
|
191
|
+
* * Recursively parses primitive values inside objects and arrays.
|
|
183
192
|
*
|
|
193
|
+
* @template T - Expected return type after parsing (default is unknown).
|
|
184
194
|
* @param input - Any input value to parse recursively.
|
|
185
|
-
* @returns Input with primitives (strings like "true", "123") converted
|
|
195
|
+
* @returns Input with primitives (strings like "true", "123") converted, typed as `T`.
|
|
186
196
|
*/
|
|
187
197
|
export function deepParsePrimitives(input) {
|
|
188
198
|
if (Array.isArray(input)) {
|
|
@@ -197,7 +207,7 @@ export function deepParsePrimitives(input) {
|
|
|
197
207
|
}
|
|
198
208
|
if (isString(input)) {
|
|
199
209
|
if (/^(true|false)$/i.test(input)) {
|
|
200
|
-
return input.toLowerCase() === 'true';
|
|
210
|
+
return (input.toLowerCase() === 'true');
|
|
201
211
|
}
|
|
202
212
|
if (isNumericString(input)) {
|
|
203
213
|
return Number(input);
|
|
@@ -208,6 +218,7 @@ export function deepParsePrimitives(input) {
|
|
|
208
218
|
if (input === 'undefined') {
|
|
209
219
|
return undefined;
|
|
210
220
|
}
|
|
221
|
+
return input;
|
|
211
222
|
}
|
|
212
223
|
return input;
|
|
213
224
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.4",
|
|
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",
|