diff --git a/src/utils.ts b/src/utils.ts index e01615a..b907e48 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,26 +1,22 @@ -import { isObservable, toJS } from 'mobx' - export const is = { fun: (a: unknown): a is Function => typeof a === 'function', - obj: (a: unknown): a is AnyObject => - Object.prototype.toString.call(a) === '[object Object]', + arr: (a: unknown): a is [] => Array.isArray(a), + obj: (a: unknown): a is AnyObject => Object.prototype.toString.call(a) === '[object Object]' } -const mapProps = (source: AnyObject) => (operation: Function) => { - const target: AnyObject = {} +export const toData = (source: any): any => { + if (is.arr(source)) { + return source.map((item) => toData(item)) + } - Object.getOwnPropertyNames(source) - .filter((key) => !is.fun(source[key])) - .forEach((key) => { - target[key] = operation(source[key]) - }) + if (is.obj(source)) { + const target: AnyObject = {} - return target -} + Object.getOwnPropertyNames(source).forEach((key) => { + target[key] = toData(source[key]) + }) -export const toData = (source: any) => { - if (is.obj(source)) { - return mapProps(source)(isObservable(source) ? toJS : toData) + return target } return source