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

Search for a string among keys #52

Open
Paike opened this issue May 10, 2013 · 2 comments
Open

Search for a string among keys #52

Paike opened this issue May 10, 2013 · 2 comments

Comments

@Paike
Copy link

Paike commented May 10, 2013

If you need to search among the keys for a given string you could clone the index-function and modify it:

searchIndex: function(string){
            var index = [], i;
            for(i in _storage){
                if(_storage.hasOwnProperty(i) && i != "__jstorage_meta"){
                    if(i.indexOf(string) !== -1) index.push(i);
                }
            }
            return index;
        }

It will return an array with keys containing the string.

@doublejosh
Copy link

Wish I'd seen that before I wrote this, handy if you'd prefer an array of items with a _key property.

/**
 * Access records matching a string.
 *
 * @param {string} string
 *   Text of the string to return values for. OPTIONAL.
 *
 * return {array}
 *   Array list of localStorage objects, keys added as "_key" property.
 */
$.jStorage.search = function search(string) {
  var results = $.jStorage.index(),
      returnVals = [];

  for (var i in results) {
    // Remove unwanted types and return records.
    if (string) {
      if (results[i].match(new RegExp(string, "g")) !== null) {
        returnVals.push($.extend(
          $.jStorage.get(results[i]), {_key: results[i]}
        ));
      }
    }
    else {
      // Return everything.
      returnVals.push($.extend(
        $.jStorage.get(results[i]), {_key: results[i]}
      ));
    }
  }

  return returnVals;
};

@doublejosh
Copy link

Here's a possibility... #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants