From 4751389b3ca613ac8d10e369dc4de949628716d0 Mon Sep 17 00:00:00 2001 From: Josh Lind Date: Tue, 11 Aug 2015 23:19:07 -0700 Subject: [PATCH] add search by key string function --- jstorage.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/jstorage.js b/jstorage.js index 1ac8fcc..3e15547 100644 --- a/jstorage.js +++ b/jstorage.js @@ -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. * @@ -993,4 +1020,4 @@ // Initialize jStorage _init(); -})(); \ No newline at end of file +})();