Skip to content

Add IDBKeyRange.prototype.includes(key) #41

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

Closed
inexorabletash opened this issue Oct 7, 2015 · 0 comments
Closed

Add IDBKeyRange.prototype.includes(key) #41

inexorabletash opened this issue Oct 7, 2015 · 0 comments

Comments

@inexorabletash
Copy link
Member

Pretty straightforward. A polyfill would be like:

IDBKeyRange.prototype.includes = function(key) {
  var r = this, c;
  if (r.lower !== undefined) {
    c = indexedDB.cmp(key, r.lower)
    if (r.lowerOpen && c <= 0) return false;
    if (!r.lowerOpen && c < 0) return false;
  }
  if (r.upper !== undefined) {
    c = indexedDB.cmp(key, r.upper)
    if (r.upperOpen && c >= 0) return false;
    if (!r.upperOpen && c > 0) return false;
  }
  return true;
};

And some sample cases:

// basic cases
IDBKeyRange.bound(12, 34).includes(11); // false
IDBKeyRange.bound(12, 34).includes(12); // true
IDBKeyRange.bound(12, 34).includes(22); // true
IDBKeyRange.bound(12, 34).includes(44); // false

// closed bound (inclusive)
IDBKeyRange.lowerBound(12).includes(11); // false
IDBKeyRange.lowerBound(12).includes(12); // true
IDBKeyRange.lowerBound(12).includes(13); // true

// open bound (exclusive)
IDBKeyRange.lowerBound(12, true).includes(11); // false
IDBKeyRange.lowerBound(12, true).includes(12); // false
IDBKeyRange.lowerBound(12, true).includes(13); // true

// only
IDBKeyRange.only(12).includes(12); // true
IDBKeyRange.only(12).includes(11); // false
@inexorabletash inexorabletash changed the title Add IDBKeyRange.includes(key) Add IDBKeyRange.prototype.includes(key) Oct 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant