Created
September 27, 2016 00:13
-
-
Save hughbris/2b5c9f6da7129e2ae7c4ee27e6e12104 to your computer and use it in GitHub Desktop.
Test if localStorage has a member
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
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