@newdash/newdash
Version:
javascript/typescript utility library
71 lines (70 loc) • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const SetCache_1 = __importDefault(require("./SetCache"));
const arrayIncludes_1 = __importDefault(require("./arrayIncludes"));
const arrayIncludesWith_1 = __importDefault(require("./arrayIncludesWith"));
const cacheHas_1 = __importDefault(require("./cacheHas"));
const arrayMap_1 = __importDefault(require("./arrayMap"));
/** Used as the size to enable large array optimizations. */
const LARGE_ARRAY_SIZE = 200;
/**
* @private
* @ignore
* @internal
* @param {*} func
*/
function baseUnary(func) {
return function (value) {
return func(value);
};
}
/**
* The base implementation of methods like `difference` without support
* for excluding multiple arrays.
*
* @private
* @param array The array to inspect.
* @param values The values to exclude.
* @param iteratee The iteratee invoked per element.
* @param comparator The comparator invoked per element.
* @returns Returns the new array of filtered values.
*/
function baseDifference(array, values, iteratee = undefined, comparator = undefined) {
var index = -1, includes = arrayIncludes_1.default, isCommon = true, length = array.length, result = [], valuesLength = values.length;
if (!length) {
return result;
}
if (iteratee) {
values = (0, arrayMap_1.default)(values, baseUnary(iteratee));
}
if (comparator) {
includes = arrayIncludesWith_1.default;
isCommon = false;
}
else if (values.length >= LARGE_ARRAY_SIZE) {
includes = cacheHas_1.default;
isCommon = false;
values = new SetCache_1.default(values);
}
outer: while (++index < length) {
var value = array[index], computed = iteratee == null ? value : iteratee(value);
value = (comparator || value !== 0) ? value : 0;
if (isCommon && computed === computed) {
var valuesIndex = valuesLength;
while (valuesIndex--) {
if (values[valuesIndex] === computed) {
continue outer;
}
}
result.push(value);
}
else if (!includes(values, computed, comparator)) {
result.push(value);
}
}
return result;
}
exports.default = baseDifference;