From 4fde9ca00ca4073764f918956d7c1061c1dd4495 Mon Sep 17 00:00:00 2001 From: janrywang Date: Wed, 7 Apr 2021 15:39:47 +0800 Subject: [PATCH] fix(path): fix accessor --- packages/path/src/index.ts | 28 +++++++++++++++++----------- packages/path/src/shared.ts | 4 +++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/path/src/index.ts b/packages/path/src/index.ts index 1b2f8982fd0..fc92892efa8 100644 --- a/packages/path/src/index.ts +++ b/packages/path/src/index.ts @@ -8,6 +8,7 @@ import { isNum, isRegExp, isPlainObj, + isAssignable, } from './shared' import { getDestructor, @@ -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) }, }, } diff --git a/packages/path/src/shared.ts b/packages/path/src/shared.ts index 3428b3d483c..17792c00481 100644 --- a/packages/path/src/shared.ts +++ b/packages/path/src/shared.ts @@ -17,7 +17,9 @@ const hasProp = Object.prototype.hasOwnProperty export const toArr = (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