Skip to content

Commit 62c5d90

Browse files
author
Guillaume Chau
committed
Add support for Map and Set - Fix vuejs#349
Initial PR vuejs#490 by AdamNiederer
1 parent 57a65a0 commit 62c5d90

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

shells/dev/target/NativeTypes.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export default {
4848
def3: {
4949
render () {}
5050
},
51-
largeArray: []
51+
largeArray: [],
52+
i: new Set([1, 2, 3, 4, new Set([5, 6, 7, 8]), new Map([[1, 2], [3, 4], [5, new Map([[6, 7]])]])]),
53+
j: new Map([[1, 2], [3, 4], [5, new Map([[6, 7]])], [8, new Set([1, 2, 3, 4, new Set([5, 6, 7, 8]), new Map([[1, 2], [3, 4], [5, new Map([[6, 7]])]])])]])
5254
}
5355
},
5456
computed: {

src/util.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ function replacer (key) {
109109
return NEGATIVE_INFINITY
110110
} else if (Number.isNaN(val)) {
111111
return NAN
112+
} else if (isMap(val)) {
113+
return encodeCache.cache(val, () => getCustomMapDetails(val))
114+
} else if (isSet(val)) {
115+
return encodeCache.cache(val, () => getCustomSetDetails(val))
112116
} else if (val instanceof RegExp) {
113117
// special handling of native type
114118
return `[native RegExp ${val.toString()}]`
@@ -131,6 +135,67 @@ function replacer (key) {
131135
return sanitize(val)
132136
}
133137

138+
export function isMap (obj) {
139+
return obj instanceof Map
140+
}
141+
142+
export function getCustomMapDetails (val) {
143+
const list = []
144+
val.forEach(
145+
(key, value) => list.push({
146+
key,
147+
value
148+
})
149+
)
150+
return {
151+
_custom: {
152+
type: 'map',
153+
display: 'Map',
154+
value: list,
155+
readOnly: true,
156+
fields: {
157+
abstract: true
158+
}
159+
}
160+
}
161+
}
162+
163+
export function reviveMap (val) {
164+
const result = new Map()
165+
const list = val._custom.value
166+
for (let i = 0; i < list.length; i++) {
167+
const { key, value } = list[i]
168+
result.set(key, reviver(null, value))
169+
}
170+
return result
171+
}
172+
173+
export function isSet (obj) {
174+
return obj instanceof Set
175+
}
176+
177+
export function getCustomSetDetails (val) {
178+
const list = Array.from(val)
179+
return {
180+
_custom: {
181+
type: 'set',
182+
display: `Set[${list.length}]`,
183+
value: list,
184+
readOnly: true
185+
}
186+
}
187+
}
188+
189+
export function reviveSet (val) {
190+
const result = new Set()
191+
const list = val._custom.value
192+
for (let i = 0; i < list.length; i++) {
193+
const value = list[i]
194+
result.add(reviver(null, value))
195+
}
196+
return result
197+
}
198+
134199
export function isComponentDefinition (val) {
135200
return val && typeof val.render === 'function'
136201
}
@@ -206,6 +271,10 @@ function reviver (key, val) {
206271
} else if (val && val._custom) {
207272
if (val._custom.type === 'component') {
208273
return instanceMap.get(val._custom.id)
274+
} else if (val._custom.type === 'map') {
275+
return reviveMap(val)
276+
} else if (val._custom.type === 'set') {
277+
return reviveSet(val)
209278
}
210279
} else if (specialTypeRE.test(val)) {
211280
const [, type, string] = specialTypeRE.exec(val)

0 commit comments

Comments
 (0)