UNPKG

ember-cerebraljs

Version:
40 lines (31 loc) 987 B
import { isArray } from '@ember/array'; function flatten (target, opts) { opts = opts || {} var delimiter = opts.delimiter || '.' var maxDepth = opts.maxDepth var output = {} function step (object, prev, currentDepth) { currentDepth = currentDepth || 1 Object.keys(object).forEach(function (key) { var value = object[key] var isarray = opts.safe && isArray(value) var type = Object.prototype.toString.call(value) var isbuffer = false; var isobject = ( type === '[object Object]' || type === '[object Array]' ) var newKey = prev ? prev + delimiter + key : key if (!isarray && !isbuffer && isobject && Object.keys(value).length && (!opts.maxDepth || currentDepth < maxDepth)) { return step(value, newKey, currentDepth + 1) } output[newKey] = value }) } step(target) return output } export default flatten;