Add key/value validation to Collection.find/findAll (#569)

* Add key/value validation to Collection.find/findAll

* Fix ESLint errors
This commit is contained in:
Schuyler Cebulskie
2016-08-30 13:20:20 -04:00
committed by Amish Shah
parent 4df7968630
commit bce3cd2b8b

View File

@@ -77,6 +77,8 @@ class Collection extends Map {
* collection.getAll('username', 'Bob');
*/
findAll(key, value) {
if (typeof key !== 'string') throw new TypeError('key must be a string');
if (typeof value === 'undefined') throw new Error('value must be specified');
const results = [];
for (const item of this.values()) {
if (item[key] === value) {
@@ -95,6 +97,8 @@ class Collection extends Map {
* collection.get('id', '123123...');
*/
find(key, value) {
if (typeof key !== 'string') throw new TypeError('key must be a string');
if (typeof value === 'undefined') throw new Error('value must be specified');
for (const item of this.values()) {
if (item[key] === value) {
return item;