Revert "This expands the consistency of .find and .exists to include the id property" (#1074)

This commit is contained in:
Amish Shah
2017-01-08 10:17:10 +00:00
committed by GitHub
parent 2a668ac997
commit fde3a976aa

View File

@@ -135,6 +135,8 @@ class Collection extends Map {
* Searches for a single item where its specified property's value is identical to the given value
* (`item[prop] === value`), or the given function returns a truthy value. In the latter case, this is identical to
* [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
* <warn>Do not use this to obtain an item by its ID. Instead, use `collection.get(id)`. See
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.</warn>
* @param {string|Function} propOrFn The property to test against, or the function to test with
* @param {*} [value] The expected value - only applicable and required if using a property for the first argument
* @returns {*}
@@ -146,7 +148,7 @@ class Collection extends Map {
find(propOrFn, value) {
if (typeof propOrFn === 'string') {
if (typeof value === 'undefined') throw new Error('Value must be specified.');
if (propOrFn === 'id') return this.get(value);
if (propOrFn === 'id') throw new RangeError('Don\'t use .find() with IDs. Instead, use .get(id).');
for (const item of this.values()) {
if (item[propOrFn] === value) return item;
}
@@ -195,6 +197,8 @@ class Collection extends Map {
/**
* Searches for the existence of a single item where its specified property's value is identical to the given value
* (`item[prop] === value`).
* <warn>Do not use this to check for an item by its ID. Instead, use `collection.has(id)`. See
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has) for details.</warn>
* @param {string} prop The property to test against
* @param {*} value The expected value
* @returns {boolean}
@@ -204,7 +208,7 @@ class Collection extends Map {
* }
*/
exists(prop, value) {
if (prop === 'id') return this.has(value);
if (prop === 'id') throw new RangeError('Don\'t use .exists() with IDs. Instead, use .has(id).');
return Boolean(this.find(prop, value));
}