class-validator-extended
Version:
Additional validators for class-validator.
30 lines (29 loc) • 928 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapUnique = mapUnique;
const is_map_1 = require("../../type/is-map");
/**
* @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 map's values.
* @typeParam Projection The type returned by `projection`.
*/
function mapUnique(value, projection) {
if (typeof projection !== 'function') {
throw new TypeError('Parameter "projection" must be a function');
}
if (!(0, is_map_1.isMap)(value)) {
return false;
}
const seen = new Set();
const map = value;
for (const item of map.values()) {
const selected = projection(item);
if (seen.has(selected)) {
return false;
}
seen.add(selected);
}
return true;
}