| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var SetCache = require('./_SetCache'), |
| 2 | arrayIncludes = require('./_arrayIncludes'), |
| 3 | arrayIncludesWith = require('./_arrayIncludesWith'), |
| 4 | arrayMap = require('./_arrayMap'), |
| 5 | baseUnary = require('./_baseUnary'), |
| 6 | cacheHas = require('./_cacheHas'); |
| 7 | |
| 8 | /** Used as the size to enable large array optimizations. */ |
| 9 | var LARGE_ARRAY_SIZE = 200; |
| 10 | |
| 11 | /** |
| 12 | * The base implementation of methods like `_.difference` without support |
| 13 | * for excluding multiple arrays or iteratee shorthands. |
| 14 | * |
| 15 | * @private |
| 16 | * @param {Array} array The array to inspect. |
| 17 | * @param {Array} values The values to exclude. |
| 18 | * @param {Function} [iteratee] The iteratee invoked per element. |
| 19 | * @param {Function} [comparator] The comparator invoked per element. |
| 20 | * @returns {Array} Returns the new array of filtered values. |
| 21 | */ |
| 22 | function baseDifference(array, values, iteratee, comparator) { |
| 23 | var index = -1, |
| 24 | includes = arrayIncludes, |
| 25 | isCommon = true, |
| 26 | length = array.length, |
| 27 | result = [], |
| 28 | valuesLength = values.length; |
| 29 | |
| 30 | if (!length) { |
| 31 | return result; |
| 32 | } |
| 33 | if (iteratee) { |
| 34 | values = arrayMap(values, baseUnary(iteratee)); |
| 35 | } |
| 36 | if (comparator) { |
| 37 | includes = arrayIncludesWith; |
| 38 | isCommon = false; |
| 39 | } |
| 40 | else if (values.length >= LARGE_ARRAY_SIZE) { |
| 41 | includes = cacheHas; |
| 42 | isCommon = false; |
| 43 | values = new SetCache(values); |
| 44 | } |
| 45 | outer: |
| 46 | while (++index < length) { |
| 47 | var value = array[index], |
| 48 | computed = iteratee == null ? value : iteratee(value); |
| 49 | |
| 50 | value = (comparator || value !== 0) ? value : 0; |
| 51 | if (isCommon && computed === computed) { |
| 52 | var valuesIndex = valuesLength; |
| 53 | while (valuesIndex--) { |
| 54 | if (values[valuesIndex] === computed) { |
| 55 | continue outer; |
| 56 | } |
| 57 | } |
| 58 | result.push(value); |
| 59 | } |
| 60 | else if (!includes(values, computed, comparator)) { |
| 61 | result.push(value); |
| 62 | } |
| 63 | } |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | module.exports = baseDifference; |