Last active
January 20, 2017 18:11
-
-
Save jhyland87/6b3fca2d21168fad38eba95f9f78f5d1 to your computer and use it in GitHub Desktop.
Lodash mixin to check if a collection (array or object) has any of the specified value(s) or key(s) (Basically its _.has(), except instead of requiring ALL paths specified exist, this checks of ANY exist)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @examples | |
| * // Collection Array & multiple seeked values: | |
| * _.hasAny(['one','two','three','four'], ['four','five']) // true | |
| * _.hasAny(['one','two','three','four'], ['five','six']) // false | |
| * | |
| * // Collection Array & a single seeked value: | |
| * _.hasAny(['one','two','three','four'], 'four') // true | |
| * _.hasAny(['one','two','three','four'], 'five') // false | |
| * | |
| * // Collection Array & multiple seeked values: | |
| * | |
| * // Collection Object & multiple seeked values: | |
| * _.hasAny({ one: 'two', three: 'four'}, ['four', 'six']) // true | |
| * _.hasAny({ one: 'two', three: 'four'}, ['six', 'eight']) // false | |
| * | |
| * // Collection Object & single seeked value: | |
| * _.hasAny({ one: 'two', three: 'four'}, 'four') // true | |
| * _.hasAny({ one: 'two', three: 'four'}, 'six') // false | |
| * | |
| * // Collection Object & multiple seeked keys: | |
| * _.hasAny({ one: 'two', three: 'four'}, ['three','five'], 'keys') // true | |
| * _.hasAny({ one: 'two', three: 'four'}, ['five','seven'], 'keys') // false | |
| * | |
| * // Collection Object & single seeked key: | |
| * _.hasAny({ one: 'two', three: 'four'}, 'three', 'keys') // true | |
| * _.hasAny({ one: 'two', three: 'four'}, 'five', 'keys') // false | |
| */ | |
| _.mixin({ 'hasAny': function ( collection, seekedVals, keyOrVal ) { | |
| if ( _.isEmpty( collection ) || _.isEmpty( seekedVals ) ){ | |
| return false | |
| } | |
| var _collection, _seekedVals, _keyOrVal | |
| // Only validate the keyOrVal if the collection is an Object | |
| if ( _.isPlainObject( collection ) ) { | |
| if ( ! keyOrVal ){ | |
| _keyOrVal = 'value' | |
| } | |
| else if ( ! _.isString( keyOrVal ) ){ | |
| throw new Error( '_.hasAny() expects to find a string for the 3rd parameter, or nothing at all - recieved typeof: ' + typeof keyOrVal ) | |
| } | |
| else { | |
| _keyOrVal = keyOrVal.trim().toLowerCase() | |
| if ( _.indexOf( [ 'key','value','keys','values' ], _keyOrVal ) === -1 ) { | |
| throw new Error( '_.hasAny() expects to find the 3rd parameter defined as "key", "keys", "value" or "values" - recieved value: ' + keyOrVal ) | |
| } | |
| if ( _keyOrVal === 'key' ){ | |
| _keyOrVal = 'keys' | |
| } | |
| else if ( _keyOrVal === 'value' ){ | |
| _keyOrVal = 'values' | |
| } | |
| } | |
| } | |
| // Convert the seekedVals to an Array | |
| if ( _.isArray( seekedVals ) ) { | |
| _seekedVals = seekedVals | |
| } | |
| else if ( _.isString( seekedVals ) || _.isNumeric( seekedVals ) ) { | |
| _seekedVals = [ _.toString( seekedVals ) ] | |
| } | |
| else { | |
| throw new Error( '_.hasAny() expects to find either a string, a number, or an array for the 2nd parameter - recieved typeof: ' + typeof seekedVals ) | |
| } | |
| // Convert the collection from the source specified (either the collection | |
| // Array, or the keys or values from an Object defined as collection) | |
| if ( _.isPlainObject( collection ) ) { | |
| // This should use _.keys() or _.values() to retrieve the data from the object | |
| _collection = _[ _keyOrVal ]( collection ) | |
| } | |
| else if ( _.isArray( collection ) ) { | |
| _collection = collection | |
| } | |
| else { | |
| throw new Error( 'Expected to find the collection defined as either an Array or an Object - recieved typeof: ' + typeof collection ) | |
| } | |
| var result = false | |
| _.forEach( _seekedVals, function( val ){ | |
| if ( _collection.indexOf( val ) !== -1 ){ | |
| result = true | |
| return false | |
| } | |
| }) | |
| return result | |
| }}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment