-
-
Save lukaszhanusik/6d53959f38432071861fb68fd231c318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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