class-validator-extended
Version:
Additional validators for class-validator.
30 lines (29 loc) • 932 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapUniqueKeys = mapUniqueKeys;
const is_map_1 = require("../../type/is-map");
/**
* @category Predicates
* @param value The value to validate.
* @param projection The function mapping each key to the value that is used for the uniqueness check.
* @typeParam Key The type of the map's keys.
* @typeParam Projection The type returned by `projection`.
*/
function mapUniqueKeys(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.keys()) {
const selected = projection(item);
if (seen.has(selected)) {
return false;
}
seen.add(selected);
}
return true;
}