@marxlnfcs/dotize
Advanced tools
Comparing version 1.1.2 to 1.2.0
export type IDotizeDotifyArrayMode = 'dotify' | 'dotify-bracket' | 'dotify-curly-bracket' | 'keep'; | ||
export type IDotizeDotifyObjArrMode = 'keep' | 'remove'; | ||
export interface IDotizeDotifyOptions { | ||
@@ -24,2 +25,14 @@ /** | ||
/** | ||
* The empty object mode defines how empty objects{} should be handled | ||
* > keep - Empty objects are kept and {} is set as the value. | ||
* > remove - Empty objects will be removed. | ||
*/ | ||
emptyObjectMode: IDotizeDotifyObjArrMode; | ||
/** | ||
* The empty array mode defines how empty arrays[] should be handled | ||
* > keep - Empty arrays are kept and [] is set as the value. | ||
* > remove - Empty arrays will be removed. | ||
*/ | ||
emptyArrayMode: IDotizeDotifyObjArrMode; | ||
/** | ||
* The method processes an object to the defined maxDepth. All beyond the maxDepth gets added to the dotified object as a whole | ||
@@ -26,0 +39,0 @@ */ |
@@ -14,2 +14,4 @@ "use strict"; | ||
arrayMode: (options === null || options === void 0 ? void 0 : options.arrayMode) || 'dotify-bracket', | ||
emptyObjectMode: (options === null || options === void 0 ? void 0 : options.emptyObjectMode) || 'keep', | ||
emptyArrayMode: (options === null || options === void 0 ? void 0 : options.emptyArrayMode) || 'keep', | ||
maxDepth: (options === null || options === void 0 ? void 0 : options.maxDepth) || 0, | ||
@@ -24,14 +26,33 @@ }, 0); | ||
depth = (0, utils_1.isNumber)(depth) ? depth : 0; | ||
// create helper function to build a returnable dotified object | ||
const createDotifiedObject = () => { | ||
if (!prefix) | ||
return object; | ||
dotified[prefix] = object; | ||
return dotified; | ||
}; | ||
// return primitive types, functions or array if options.arrayMode is 'keep' | ||
if ((0, utils_1.isNil)(object) || | ||
(0, utils_1.isPrimitive)(object) || | ||
((0, utils_1.isArray)(object) && options.arrayMode === 'keep') || | ||
(!(0, utils_1.isArray)(object) && (0, utils_1.isObject)(object) && Object.keys(object).length === 0) || | ||
//(isArray(object) && options.arrayMode === 'keep') || | ||
//(!isArray(object) && isObject(object) && Object.keys(object).length === 0) || | ||
(0, utils_1.isFunction)(object) || | ||
(options.maxDepth > 0 && depth >= options.maxDepth)) { | ||
if (!prefix) | ||
return object; | ||
dotified[prefix] = object; | ||
return dotified; | ||
return createDotifiedObject(); | ||
} | ||
// check array options | ||
if ((0, utils_1.isArray)(object) && (options.arrayMode === 'keep' || (object.length === 0 && options.emptyArrayMode === 'keep'))) { | ||
// return the full array if the arrayMode is set to keep | ||
if (options.arrayMode === 'keep') | ||
return createDotifiedObject(); | ||
// return the empty array if the array is empty and the emptyArrayMode is set to keep | ||
if (options.emptyArrayMode === 'keep' && object.length === 0) | ||
return createDotifiedObject(); | ||
} | ||
// check object options | ||
if ((0, utils_1.isObject)(object)) { | ||
// return the empty object if the object is empty and the emptyObjectMode is set to keep | ||
if (options.emptyObjectMode === 'keep' && Object.keys(object).length === 0) | ||
return createDotifiedObject(); | ||
} | ||
// iterate through entries | ||
@@ -38,0 +59,0 @@ for (let key in object) { |
{ | ||
"name": "@marxlnfcs/dotize", | ||
"private": false, | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "A Typescript library to convert complex objects/arrays to dotized key-value object", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -57,3 +57,3 @@ <p align="center" style="font-size: 40px;">Dotize</p> | ||
```typescript | ||
/** | ||
/** | ||
* The prefix will be added to every key on depth 0 | ||
@@ -75,9 +75,23 @@ * @example "$" | ||
* > dotify: The index will be set as plain number. Example: foo.0.bar | ||
* > dotify-bracket: The index will be surrounded with brackets (default). Example: "foo.[0].bar" | ||
* > dotify-bracket: The index will be wrapped with surrounded (default). Example: "foo.[0].bar" | ||
* > dotify-curly-bracket: The index will be surrounded with curly brackets. Example: "foo.{0}.bar" | ||
* > keep: Arrays will be ignored. Example: { "foo": [ { "bar": "Hello World!" } ] } | ||
*/ | ||
arrayMode: 'dotify'|'dotify-bracked'|'dotify-curly-bracket'|'keep'; | ||
arrayMode: 'dotify'|'dotify-bracket'|'dotify-curly-bracket'; | ||
/** | ||
* The empty object mode defines how empty objects{} should be handled | ||
* > keep - Empty objects are kept and {} is set as the value. | ||
* > remove - Empty objects will be removed. | ||
*/ | ||
emptyObjectMode: 'keep'|'remove'; | ||
/** | ||
* The empty array mode defines how empty arrays[] should be handled | ||
* > keep - Empty arrays are kept and [] is set as the value. | ||
* > remove - Empty arrays will be removed. | ||
*/ | ||
emptyArrayMode: 'keep'|'remove'; | ||
/** | ||
* The method processes an object to the defined maxDepth. All beyond the maxDepth gets added to the dotified object as a whole | ||
@@ -110,3 +124,3 @@ */ | ||
*/ | ||
arrayMode: 'dotify'|'dotify-bracked'|'dotify-curly-bracket'; | ||
arrayMode: 'dotify'|'dotify-bracket'|'dotify-curly-bracket'; | ||
@@ -113,0 +127,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52196
366
131