Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search-by-string on key function #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion jstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,33 @@
return typeof(def) == 'undefined' ? null : def;
},

/**
* Looks up items by key match.
*
* @param {String} string - Value to match on.
* @return {array} List of storage items.
*/
search: function(str) {
var returnVals = [];

for (i in _storage) {
if (_storage.hasOwnProperty(i) && i != '__jstorage_meta') {
if (str) {
// remove unwanted types and return records
if (_storage[i].match(new RegExp(str, "g")) !== null) {
returnVals.push($.extend(_storage[_storage[i]], {_key: _storage[i]}));
}
}
else {
// return everything
returnVals.push($.extend(_storage[_storage[i]], {_key: _storage[i]}));
}
}
}

return returnVals;
}

/**
* Deletes a key from cache.
*
Expand Down Expand Up @@ -993,4 +1020,4 @@
// Initialize jStorage
_init();

})();
})();