refactor(reactivity): refactor iteration key trigger logic + use more robust Map/Set check

This commit is contained in:
Evan You
2020-09-14 11:26:34 -04:00
parent cf1b6c666f
commit 0124eacc91
5 changed files with 48 additions and 28 deletions

View File

@@ -58,9 +58,11 @@ export const hasOwn = (
): key is keyof typeof val => hasOwnProperty.call(val, key)
export const isArray = Array.isArray
export const isSet = (val: any): boolean => {
return toRawType(val) === 'Set'
}
export const isMap = (val: unknown): val is Map<any, any> =>
toTypeString(val) === '[object Map]'
export const isSet = (val: unknown): val is Set<any> =>
toTypeString(val) === '[object Set]'
export const isDate = (val: unknown): val is Date => val instanceof Date
export const isFunction = (val: unknown): val is Function =>
typeof val === 'function'

View File

@@ -1,4 +1,4 @@
import { isArray, isObject, isPlainObject } from './index'
import { isArray, isMap, isObject, isPlainObject, isSet } from './index'
/**
* For converting {{ interpolation }} values to displayed strings.
@@ -13,14 +13,14 @@ export const toDisplayString = (val: unknown): string => {
}
const replacer = (_key: string, val: any) => {
if (val instanceof Map) {
if (isMap(val)) {
return {
[`Map(${val.size})`]: [...val.entries()].reduce((entries, [key, val]) => {
;(entries as any)[`${key} =>`] = val
return entries
}, {})
}
} else if (val instanceof Set) {
} else if (isSet(val)) {
return {
[`Set(${val.size})`]: [...val.values()]
}