8000 feat: add adjacent difference array method by banjoadeola17 · Pull Request #6077 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,28 @@
return result;
}

/**
* The base implementation of `_.adjacentDifference`.
*
* @private
* @param {Array} array The array to inspect.
* @param {Function} iteratee The iteratee invoked per element.
* @returns {Array} Returns the array of adjacent differences.
*/
function baseAdjacentDifference(array, iteratee) {
var index = 0,
length = array.length,
result = Array(length - 1),
previous = iteratee(array[0]);

while (++index < length) {
var current = iteratee(array[index]);
result[index - 1] = current - previous;
previous = current;
}
return result;
}

/**
* The base implementation of `baseForOwn` which iterates over `object`
* properties returned by `keysFunc` and invokes `iteratee` for each property.
Expand Down Expand Up @@ -6918,6 +6940,34 @@

/*------------------------------------------------------------------------*/

/**
* Computes the difference between adjacent elements of `array`.
*
* @static
* @memberOf _
* @since 4.18.0
* @category Array
* @param {Array} array The array to inspect.
* @param {Function} [iteratee=identity] The iteratee invoked per element.
* @returns {Array} Returns the array of adjacent differences.
* @example
*
* _.adjacentDifference([10, 13, 15, 20]);
* // => [3, 2, 5]
*
* _.adjacentDifference([{x:1},{x:4},{x:6}], o => o.x);
* // => [3, 2]
*/
function adjacentDifference(array, iteratee) {
var length = array == null ? 0 : array.length;
if (length < 2) {
return [];
}
iteratee = iteratee == null ? identity : iteratee;
return baseAdjacentDifference(array, iteratee);
}


/**
* Creates an array of elements split into groups the length of `size`.
* If `array` can't be split evenly, the final chunk will be the remaining
Expand Down Expand Up @@ -16671,6 +16721,7 @@
lodash.bindKey = bindKey;
lodash.castArray = castArray;
lodash.chain = chain;
lodash.adjacentDifference = adjacentDifference;
lodash.chunk = chunk;
lodash.compact = compact;
lodash.concat = concat;
Expand Down
0