Created
September 15, 2017 00:28
-
-
Save wookiehangover/44a90a21b9d625afa2bae2fd5b884be3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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