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.
21 lines • 635 B
JavaScript
import { fpIdentity } from "../../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}.
*/
export function arrayPushUnique(items, itemToPush, getComparisonValue = 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;
}
//# sourceMappingURL=array-push-unique.js.map