Skip to content

Instantly share code, notes, and snippets.

@fredyang
Created April 4, 2012 02:22
Show Gist options
  • Save fredyang/2297169 to your computer and use it in GitHub Desktop.
Save fredyang/2297169 to your computer and use it in GitHub Desktop.
buildAccess
function buildAccess ( storage, convertKey, convertValue ) {
return function access ( key, value ) {
if (typeof key === "object") {
for (var k in key) {
access( k, key[k] );
}
return storage;
}
if (convertKey) {
key = convertKey( key );
}
if (value === undefined) {
if (arguments.length === 1) {
return storage[key];
} else {
//allow access(key, undefined)
//to delete the key from storage
delete storage[key];
}
} else {
if (convertValue) {
value = convertValue( value );
}
return (storage[key] = value);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment