Skip to content

Commit

Permalink
use isPlainObject
Browse files Browse the repository at this point in the history
  • Loading branch information
eliranhe authored Feb 28, 2021
2 parents 6948bee + 31989e1 commit 5b1844c
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ export * from './types'
const BIG_FACTOR = 2048
const SMALL_FACTOR = 128

const isPlainObject = (value: unknown) => {
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false
}
const prototype = Object.getPrototypeOf(value)
return prototype === null || prototype === Object.prototype
}

const traverse = (obj: any, visit: Visitor, queueFactor: number) => {
const queue = new Queue<Node>(queueFactor)
queue.enqueue({path: [], val: obj})

while (!queue.isEmpty()) {
const next = queue.dequeue()
if (!visit(next.val, next.path)) {
if (typeof next.val === 'object') {
if (next.val !== null && !(next.val instanceof HTMLElement)) {
const keys = Object.keys(next.val)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
queue.enqueue({
path: [...next.path, key],
val: next.val[key]
})
}
}
} else if (Array.isArray(next.val)) {
if (Array.isArray(next.val)) {
for (let i = 0; i < next.val.length; i++) {
queue.enqueue({
path: [...next.path, i],
val: next.val[i]
})
}
}
} else if (isPlainObject(next.val)) {
const keys = Object.keys(next.val)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
queue.enqueue({
path: [...next.path, key],
val: next.val[key]
})
}
}
}
}
}
Expand Down

0 comments on commit 5b1844c

Please sign in to comment.