hierarchy-js
Version:
Elegant and lightweight library for working with data structures
19 lines (15 loc) • 468 B
JavaScript
const createCopy = (item) => {
if (!item || typeof item !== 'object') return item
const accumulator = Array.isArray(item) ? [] : {}
return iterator(item, accumulator, createCopy)
}
const iterator = (iterable, accumulator, callback) => {
for (let key in iterable) {
// istanbul ignore next
if (iterable.hasOwnProperty(key)) {
accumulator[key] = callback(iterable[key])
}
}
return accumulator
}
module.exports = { createCopy, iterator }