rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
25 lines • 806 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayPushUnique = void 0;
const fp_identity_js_1 = require("../../fp/impl/fp-identity.js");
/**
* @public
* Like `Array.push` but checks if the value is unique first.
*
* @returns true if an element was pushed.
*
* @remarks
* See {@link arrayPushUnique}.
*/
function arrayPushUnique(items, itemToPush, getComparisonValue = fp_identity_js_1.fpIdentity) {
const comparisonValue = getComparisonValue(itemToPush);
for (let i = 0, iEnd = items.length; i < iEnd; ++i) {
if (getComparisonValue(items[i]) === comparisonValue) {
return false;
}
}
items.push(itemToPush);
return true;
}
exports.arrayPushUnique = arrayPushUnique;
//# sourceMappingURL=array-push-unique.js.map