From fde3a976aace2456a1f40c2a12c975f0232c2c19 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sun, 8 Jan 2017 10:17:10 +0000 Subject: [PATCH] Revert "This expands the consistency of .find and .exists to include the id property" (#1074) --- src/util/Collection.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/util/Collection.js b/src/util/Collection.js index 7cf63ec06..bafe710b6 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -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). + * 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. * @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`). + * 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. * @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)); }