Skip to content

Commit

Permalink
Add _.gpIndexOfNonStrict and _.gpIncludesNonStrict
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-pribilinskiy committed Apr 18, 2016
1 parent 106e560 commit 5b65cd9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
43 changes: 43 additions & 0 deletions array/gpIndexOfNonStrict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(function() {

_.mixin({gpIndexOfNonStrict: gpIndexOfNonStrict});

/**
* Same as `_.indexOf` except that non-strict equality operator is used for comparison
*
* @see {@link _.indexOf}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality_()}
*
* @example
*
* _.indexOf([1, 2, 1, 2], '2');
* // => 1
*/
function gpIndexOfNonStrict(array, value, fromIndex) {

var length = array ? array.length : 0;

if (!length) {
return -1;
}

fromIndex = _.toInteger(fromIndex);

if (fromIndex < 0) {
fromIndex = _.max(length + fromIndex, 0);
}

var index = fromIndex - 1;

while (++index < length) {

if (array[index] == value) {
return index;
}
}

return -1;
}

})();

49 changes: 49 additions & 0 deletions collection/gpIncludesNonStrict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(function() {

_.mixin({gpIncludesNonStrict: gpIncludesNonStrict});

/**
* Same as `_.includes` except that
* non-strict equality comparison operator is used for comparison
*
* @see {@link _.includes}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Equality_()}
*
* @example
*
* _.includes([1, 2, 3], '1');
* // => true
*
* _.includes({ 'user': 'fred', 'age': '40' }, 40);
* // => true
*
* _.includes([undefined], null);
* // => true
*
* _.includes([1], true);
* // => true
*
* _.includes('', false);
* // => true
*/
function gpIncludesNonStrict(collection, value, fromIndex, guard) {

collection = _.isArrayLike(collection) ? collection : _.values(collection);
fromIndex = (fromIndex && !guard) ? _.toInteger(fromIndex) : 0;

var length = collection.length;
if (fromIndex < 0) {
fromIndex = _.max(length + fromIndex, 0);
}

if (_.isString(collection) && collection) {
return (fromIndex <= length && collection.indexOf(value, fromIndex) > -1);
} else if (_.isArray()) {
return (!!length && baseIndexOf(collection, value, fromIndex) > -1);
} else {
return collection == value;
}
}

})();

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@goldenplanet/lodash",
"description": "Lodash mixins for OpenBizBox",
"version": "1.0.1",
"version": "1.0.3",
"authors": [
"Steven Pribilinskiy <[email protected]>"
],
"homepage": "https://github.com/goldenplanetdk/lodash-mixins",
"main": "index.js",
"devDependencies": {
"lodash": "~3.10.1"
"lodash": "4.5.0"
}
}

0 comments on commit 5b65cd9

Please sign in to comment.