Key -> Property

This commit is contained in:
Schuyler Cebulskie
2016-09-12 02:20:53 -04:00
parent 516e6556a0
commit 2aae20085b
2 changed files with 28 additions and 28 deletions

File diff suppressed because one or more lines are too long

View File

@@ -56,59 +56,59 @@ class Collection extends Map {
} }
/** /**
* Returns an array of items where `item[key] === value` of the collection * Returns an array of items where `item[prop] === value` of the collection
* @param {string} key The key to filter by * @param {string} prop The property to test against
* @param {*} value The expected value * @param {*} value The expected value
* @returns {array} * @returns {array}
* @example * @example
* collection.findAll('username', 'Bob'); * collection.findAll('username', 'Bob');
*/ */
findAll(key, value) { findAll(prop, value) {
if (typeof key !== 'string') throw new TypeError('Key must be a string.'); if (typeof prop !== 'string') throw new TypeError('Key must be a string.');
if (typeof value === 'undefined') throw new Error('Value must be specified.'); if (typeof value === 'undefined') throw new Error('Value must be specified.');
const results = []; const results = [];
for (const item of this.values()) { for (const item of this.values()) {
if (item[key] === value) results.push(item); if (item[prop] === value) results.push(item);
} }
return results; return results;
} }
/** /**
* Returns a single item where `item[key] === value`, or the given function returns `true`. * Returns a single item where `item[prop] === value`, or the given function returns `true`.
* In the latter case, this is identical to * In the latter case, this is identical to
* [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). * [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
* @param {string|function} keyOrFn The key to filter by, or the function to test with * @param {string|function} propOrFn The property to test against, or the function to test with
* @param {*} [value] The expected value - required if using a key for the first param * @param {*} [value] The expected value - required if using a property for the first argument
* @returns {*} * @returns {*}
* @example * @example
* collection.find('id', '123123...'); * collection.find('id', '123123...');
* @example * @example
* collection.find(val => val.id === '123123...'); * collection.find(val => val.id === '123123...');
*/ */
find(keyOrFn, value) { find(propOrFn, value) {
if (typeof keyOrFn === 'string') { if (typeof propOrFn === 'string') {
if (typeof value === 'undefined') throw new Error('Value must be specified.'); if (typeof value === 'undefined') throw new Error('Value must be specified.');
for (const item of this.values()) { for (const item of this.values()) {
if (item[keyOrFn] === value) return item; if (item[propOrFn] === value) return item;
} }
return null; return null;
} else if (typeof keyOrFn === 'function') { } else if (typeof propOrFn === 'function') {
for (const [key, val] of this) { for (const [key, val] of this) {
if (keyOrFn(val, key, this)) return val; if (propOrFn(val, key, this)) return val;
} }
return null; return null;
} else { } else {
throw new Error('First parameter must be a string key or a function.'); throw new Error('First argument must be a property string or a function.');
} }
} }
/* eslint-disable max-len */ /* eslint-disable max-len */
/** /**
* Returns the key of the item where `item[key] === value`, or the given function returns `true`. * Returns the key of the item where `item[prop] === value`, or the given function returns `true`.
* In the latter case, this is identical to * In the latter case, this is identical to
* [Array.findIndex()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex). * [Array.findIndex()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex).
* @param {string|function} keyOrFn The key to filter by, or the function to test with * @param {string|function} propOrFn The property to test against, or the function to test with
* @param {*} [value] The expected value - required if using a key for the first param * @param {*} [value] The expected value - required if using a property for the first argument
* @returns {*} * @returns {*}
* @example * @example
* collection.find('id', '123123...'); * collection.find('id', '123123...');
@@ -116,26 +116,26 @@ class Collection extends Map {
* collection.find(val => val.id === '123123...'); * collection.find(val => val.id === '123123...');
*/ */
/* eslint-enable max-len */ /* eslint-enable max-len */
findKey(keyOrFn, value) { findKey(propOrFn, value) {
if (typeof keyOrFn === 'string') { if (typeof propOrFn === 'string') {
if (typeof value === 'undefined') throw new Error('Value must be specified.'); if (typeof value === 'undefined') throw new Error('Value must be specified.');
for (const [key, val] of this) { for (const [key, val] of this) {
if (val[keyOrFn] === value) return key; if (val[propOrFn] === value) return key;
} }
return null; return null;
} else if (typeof keyOrFn === 'function') { } else if (typeof propOrFn === 'function') {
for (const [key, val] of this) { for (const [key, val] of this) {
if (keyOrFn(val, key, this)) return key; if (propOrFn(val, key, this)) return key;
} }
return null; return null;
} else { } else {
throw new Error('First parameter must be a string key or a function.'); throw new Error('First argument must be a property string or a function.');
} }
} }
/** /**
* Returns true if the collection has an item where `item[key] === value` * Returns true if the collection has an item where `item[prop] === value`
* @param {string} key The key to filter by * @param {string} prop The property to test against
* @param {*} value The expected value * @param {*} value The expected value
* @returns {boolean} * @returns {boolean}
* @example * @example
@@ -143,8 +143,8 @@ class Collection extends Map {
* console.log('user here!'); * console.log('user here!');
* } * }
*/ */
exists(key, value) { exists(prop, value) {
return Boolean(this.find(key, value)); return Boolean(this.find(prop, value));
} }
/** /**