class-validator-extended
Version:
Additional validators for class-validator.
30 lines (29 loc) • 926 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setUnique = setUnique;
const is_set_1 = require("../../type/is-set");
/**
* @category Predicates
* @param value The value to validate.
* @param projection The function mapping each value to the value that is used for the uniqueness check.
* @typeParam Value The type of the set's values.
* @typeParam Projection The type returned by `projection`.
*/
function setUnique(value, projection) {
if (typeof projection !== 'function') {
throw new TypeError('Parameter "projection" must be a function');
}
if (!(0, is_set_1.isSet)(value)) {
return false;
}
const seen = new Set();
const set = value;
for (const item of set.keys()) {
const selected = projection(item);
if (seen.has(selected)) {
return false;
}
seen.add(selected);
}
return true;
}