Files
lodash/differenceWith.js
2017-01-10 01:46:24 -08:00

38 lines
1.3 KiB
JavaScript

import baseDifference from './.internal/baseDifference.js';
import baseFlatten from './.internal/baseFlatten.js';
import isArrayLikeObject from './isArrayLikeObject.js';
import last from './last.js';
/**
* This method is like `difference` except that it accepts `comparator`
* which is invoked to compare elements of `array` to `values`. The order and
* references of result values are determined by the first array. The comparator
* is invoked with two arguments: (arrVal, othVal).
*
* **Note:** Unlike `pullAllWith`, this method returns a new array.
*
* @since 4.0.0
* @category Array
* @param {Array} array The array to inspect.
* @param {...Array} [values] The values to exclude.
* @param {Function} [comparator] The comparator invoked per element.
* @returns {Array} Returns the new array of filtered values.
* @example
*
* const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
*
* differenceWith(objects, [{ 'x': 1, 'y': 2 }], isEqual);
* // => [{ 'x': 2, 'y': 1 }]
*/
function differenceWith(array, ...values) {
let comparator = last(values);
if (isArrayLikeObject(comparator)) {
comparator = undefined;
}
return isArrayLikeObject(array)
? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator)
: [];
}
export default differenceWith;