@hyurl/utils
Advanced tools
Comparing version 0.1.26 to 0.1.27
@@ -12,3 +12,3 @@ declare type OmitChildrenNodes<T> = Pick<T, { | ||
*/ | ||
export default function flatObject<T extends object>(obj: T, depth?: number): OmitChildrenNodes<T> & Record<string | symbol, any>; | ||
export default function flatObject<T extends object>(obj: T, depth?: number, flatArray?: boolean): OmitChildrenNodes<T> & Record<string | symbol, any>; | ||
export {}; |
@@ -14,7 +14,7 @@ "use strict"; | ||
*/ | ||
function flatObject(obj, depth = 1) { | ||
return flatDeep({}, obj, "", 0, depth); | ||
function flatObject(obj, depth = 1, flatArray = false) { | ||
return flatDeep({}, obj, "", 0, depth, flatArray); | ||
} | ||
exports.default = flatObject; | ||
function flatDeep(carrier, source, field, depth, maxDepth) { | ||
function flatDeep(carrier, source, field, depth, maxDepth, flatArray) { | ||
let isArr; | ||
@@ -35,3 +35,3 @@ let isObj; | ||
else { | ||
flatDeep(carrier, value, field ? `${field}.${key}` : key, depth, maxDepth); | ||
flatDeep(carrier, value, field ? `${field}.${key}` : key, field ? depth + 1 : depth, maxDepth, flatArray); | ||
} | ||
@@ -41,5 +41,10 @@ }); | ||
else if (isArr) { | ||
for (let i = 0, len = source.length; i < len; ++i) { | ||
flatDeep(carrier, source[i], field ? `${field}.${i}` : String(i), depth + 1, maxDepth); | ||
if (flatArray) { | ||
for (let i = 0, len = source.length; i < len; ++i) { | ||
flatDeep(carrier, source[i], field ? `${field}.${i}` : String(i), field ? depth - 1 : depth, maxDepth, flatArray); | ||
} | ||
} | ||
else if (depth > 0) { | ||
carrier[field] = source; | ||
} | ||
} | ||
@@ -46,0 +51,0 @@ return carrier; |
{ | ||
"name": "@hyurl/utils", | ||
"version": "0.1.26", | ||
"version": "0.1.27", | ||
"description": "Utility functions of HyURL collection.", | ||
@@ -5,0 +5,0 @@ "main": "libs/index.js", |
Sorry, the diff of this file is not supported yet
63984
990