Created
May 30, 2018 21:16
-
-
Save rkurbatov/17468b2ade459a7498c8209800287a03 to your computer and use it in GitHub Desktop.
localStorage/sessionStorage mock
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
class Storage { | |
constructor() { | |
this.__valuesMap = new Map() | |
} | |
getItem(key) { | |
return this.__valuesMap.has(key) | |
? String(this.__valuesMap.get(String(key))) | |
: null | |
} | |
setItem(key, val) { | |
this.__valuesMap.set(String(key), String(val)) | |
} | |
removeItem(key) { | |
this.__valuesMap.delete(key) | |
} | |
clear() { | |
this.__valuesMap.clear() | |
} | |
key(i) { | |
if (!arguments.length) { | |
// this is a TypeError implemented on Chrome, Firefox throws Not enough arguments to Storage.key. | |
throw new TypeError( | |
"Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present.", | |
) | |
} | |
return Array.from(this.__valuesMap.keys())[i] | |
} | |
get length() { | |
return this.__valuesMap.size | |
} | |
set length(val) {} | |
} | |
const getterSetter = instance => ({ | |
set: function(obj, prop, value) { | |
if (Storage.prototype.hasOwnProperty(prop)) { | |
instance[prop] = value | |
} else { | |
instance.setItem(prop, value) | |
} | |
return true | |
}, | |
get: function(target, name) { | |
if (Storage.prototype.hasOwnProperty(name) || name === '__valuesMap') { | |
return instance[name] | |
} | |
if (instance.__valuesMap.has(name)) { | |
return instance.getItem(name) | |
} | |
}, | |
}) | |
const localInstance = new Storage() | |
const sessionInstance = new Storage() | |
global.localStorage = new Proxy(localInstance, getterSetter(localInstance)) | |
global.sessionStorage = new Proxy( | |
sessionInstance, | |
getterSetter(sessionInstance), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment