Skip to content

Commit

Permalink
feat: add support for map & set data type
Browse files Browse the repository at this point in the history
  • Loading branch information
cicec committed May 10, 2020
1 parent b402577 commit ffe65df
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
export const is = {
fun: (a: unknown): a is Function => typeof a === 'function',
arr: (a: unknown): a is [] => Array.isArray(a),
obj: (a: unknown): a is AnyObject => Object.prototype.toString.call(a) === '[object Object]'
obj: (a: unknown): a is AnyObject => Object.prototype.toString.call(a) === '[object Object]',
set: (a: unknown): a is Set<any> => Object.prototype.toString.call(a) === '[object Set]',
map: (a: unknown): a is Map<any, any> => Object.prototype.toString.call(a) === '[object Map]',
}

export const toData = (source: any): any => {
if (is.arr(source)) {
return source.map((item) => toData(item))
}

if (is.set(source)) {
return Array.from(source).map((item) => toData(item))
}

if (is.obj(source)) {
const target: AnyObject = {}

Expand All @@ -19,5 +25,16 @@ export const toData = (source: any): any => {
return target
}

if (is.map(source)) {
const obj = Object.fromEntries(source)
const target: AnyObject = {}

Object.getOwnPropertyNames(obj).forEach((key) => {
target[key] = toData(obj[key])
})

return target
}

return source
}

0 comments on commit ffe65df

Please sign in to comment.