Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict'
class JSONMap extends Map {
constructor (value) {
let mapArgs = []
if (value) {
for (let k of Object.keys(value)) {
mapArgs.push([ k, value[k] ])
}
}
super(mapArgs)
}
get (key, defaultValue) {
const value = super.get(key)
if (value === undefined && defaultValue !== undefined) {
return defaultValue
}
return value
}
toJSON () {
const obj = Object.create(null)
for (let [k, v] of this.entries()) {
if (v instanceof JSONMap) {
obj[k] = v.toJSON()
} else {
obj[k] = v
}
}
return obj
}
}
exports.JSONMap = JSONMap
exports.createJSONMap = value => new JSONMap(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.