Skip to content

Commit

Permalink
fix: data update exception
Browse files Browse the repository at this point in the history
  • Loading branch information
cicec committed May 10, 2020
1 parent 2530472 commit b402577
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b402577

Please sign in to comment.