Skip to content

Commit

Permalink
fix(path): fix accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 7, 2021
1 parent 76429fb commit 4fde9ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 17 additions & 11 deletions packages/path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isNum,
isRegExp,
isPlainObj,
isAssignable,
} from './shared'
import {
getDestructor,
Expand All @@ -24,24 +25,29 @@ import { Matcher } from './matcher'
const REGISTRY: IRegistry = {
accessors: {
get(source: any, key: number | string | symbol) {
if (typeof source !== 'object') return source
return Reflect.get(source, key)
if (isAssignable(source)) {
return Reflect.get(source, key)
}
},
set(source: any, key: number | string | symbol, value: any) {
if (typeof source !== 'object') return
return Reflect.set(source, key, value)
if (isAssignable(source)) {
return Reflect.set(source, key, value)
}
},
has(source: any, key: number | string | symbol) {
if (typeof source !== 'object') return
return Reflect.has(source, key)
if (isAssignable(source)) {
return Reflect.has(source, key)
}
return false
},
delete(source: any, key: number | string | symbol) {
if (typeof source !== 'object') return
if (Array.isArray(source) && isNumberIndex(key)) {
source.splice(Number(key), 1)
return true
if (isAssignable(source)) {
if (Array.isArray(source) && isNumberIndex(key)) {
source.splice(Number(key), 1)
return true
}
return Reflect.deleteProperty(source, key)
}
return Reflect.deleteProperty(source, key)
},
},
}
Expand Down
4 changes: 3 additions & 1 deletion packages/path/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const hasProp = Object.prototype.hasOwnProperty

export const toArr = <T>(val: T | T[]): T[] =>
Array.isArray(val) ? val : val !== undefined ? [val] : []

export const isAssignable = (val: any) => {
return typeof val === 'object' || typeof val === 'function'
}
export const isEqual = (a: any, b: any) => {
if (a === b) {
return true
Expand Down

0 comments on commit 4fde9ca

Please sign in to comment.