Skip to content

Instantly share code, notes, and snippets.

@hughbris
Created September 27, 2016 00:13
Show Gist options
  • Save hughbris/2b5c9f6da7129e2ae7c4ee27e6e12104 to your computer and use it in GitHub Desktop.
Save hughbris/2b5c9f6da7129e2ae7c4ee27e6e12104 to your computer and use it in GitHub Desktop.
Test if localStorage has a member
Storage.prototype.hasItem = function(itemName, rejectEmpty) {
var setting = this.getItem(itemName);
return ( (setting !== null) && (rejectEmpty ? setting.length > 0 : true) );
}
/*
testStorageHasItem = function() {
window.localStorage.setItem('test.foo', 'foo');
console.log(window.localStorage.hasItem('test.foo')); // true
console.log(window.localStorage.hasItem('test.foo', false)); // true
console.log(window.localStorage.hasItem('test.foo', true)); // true
window.localStorage.setItem('test.empty','');
console.log(window.localStorage.hasItem('test.empty')); // true
console.log(window.localStorage.hasItem('test.empty', false)); // true
console.log(window.localStorage.hasItem('test.empty', true)); // false
window.localStorage.removeItem('test.bar');
console.log(window.localStorage.hasItem('test.bar' )); // false
console.log(window.localStorage.hasItem('test.bar', false)); // false
console.log(window.localStorage.hasItem('test.bar', true)); // false
}();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment