From 3d078383cba90b4b5f37d6703ce23c8395c35c57 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Thu, 12 May 2016 23:52:29 +0200 Subject: [PATCH] Add `findIndexFrom`, `findLastIndexFrom`, `indexOfFrom`, & `lastIndexOfFrom` in FP. --- fp/_mapping.js | 9 +++++-- test/test-fp.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/fp/_mapping.js b/fp/_mapping.js index 178e4d6d4..cf73a2cd8 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -84,8 +84,9 @@ exports.aryMethod = { ], '3': [ 'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith', - 'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'invokeArgs', - 'invokeArgsMap', 'isEqualWith', 'isMatchWith', 'flatMapDepth', 'mergeWith', + 'findIndexFrom', 'findLastIndexFrom', 'getOr', 'indexOfFrom', 'inRange', + 'intersectionBy', 'intersectionWith', 'invokeArgs', 'invokeArgsMap', + 'isEqualWith', 'isMatchWith', 'flatMapDepth', 'lastIndexOfFrom', 'mergeWith', 'orderBy', 'padChars', 'padCharsEnd', 'padCharsStart', 'pullAllBy', 'pullAllWith', 'reduce', 'reduceRight', 'replace', 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'transform', 'unionBy', 'unionWith', @@ -235,9 +236,13 @@ exports.realToAlias = (function() { exports.remap = { 'curryN': 'curry', 'curryRightN': 'curryRight', + 'findIndexFrom': 'findIndex', + 'findLastIndexFrom': 'findLastIndex', 'getOr': 'get', + 'indexOfFrom': 'indexOf', 'invokeArgs': 'invoke', 'invokeArgsMap': 'invokeMap', + 'lastIndexOfFrom': 'lastIndexOf', 'padChars': 'pad', 'padCharsEnd': 'padEnd', 'padCharsStart': 'padStart', diff --git a/test/test-fp.js b/test/test-fp.js index 6a471bf7b..4901cf890 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -1097,6 +1097,47 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('fp.findIndexFrom methods'); + + var findIndexTestCases = { + 'findIndexFrom': [ + { 'fromIndex': 0, 'expected': 1 }, + { 'fromIndex': 2, 'expected': 3 } + ], + 'findLastIndexFrom': [ + { 'fromIndex': -1, 'expected': 3 }, + { 'fromIndex': 2, 'expected': 1 } + ] + }; + + _.forOwn(findIndexTestCases, function(testCases, methodName) { + QUnit.test('`fp.' + methodName + '` should have an argument order of `predicate`, `fromIndex` then `array`', function(assert) { + assert.expect(testCases.length); + + var array = [100, 0, 100, 0]; + + _.each(testCases, function(testCase) { + assert.deepEqual(fp[methodName](fp.eq(0))(testCase.fromIndex)(array), testCase.expected); + }); + }); + + QUnit.test('`fp.' + methodName + '` should have its iteratee capped at 1 argument', function(assert) { + assert.expect(2); + + var array = [100, 100]; + + var iteratee = function(value, index) { + assert.equal(value, 100); + assert.equal(index, undefined, 'iteratee is not capped'); + return true; + }; + + fp[methodName](iteratee)(1)(array); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('fp.flatMapDepth'); (function() { @@ -1221,6 +1262,33 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('fp.indexOfFrom methods'); + + var indexOfTestCases = { + indexOfFrom: [ + {fromIndex: 0, expected: 1}, + {fromIndex: 2, expected: 3} + ], + lastIndexOfFrom: [ + {fromIndex: -1, expected: 3}, + {fromIndex: 2, expected: 1} + ] + }; + + _.each(indexOfTestCases, function(testCases, methodName) { + QUnit.test('`fp.' + methodName + '` should have an argument order of `value`, `fromIndex` then `array`', function(assert) { + assert.expect(testCases.length); + + var array = [100, 0, 100, 0]; + + _.each(testCases, function(testCase) { + assert.deepEqual(fp[methodName](0)(testCase.fromIndex)(array), testCase.expected); + }); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('fp.inRange'); (function() {