UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ArrayUnique = exports.arrayUnique = exports.ARRAY_UNIQUE = void 0;
4const ValidateBy_1 = require("../common/ValidateBy");
5exports.ARRAY_UNIQUE = 'arrayUnique';
6/**
7 * Checks if all array's values are unique. Comparison for objects is reference-based.
8 * If null or undefined is given then this function returns false.
9 */
10function arrayUnique(array, identifier) {
11 if (!Array.isArray(array))
12 return false;
13 if (identifier) {
14 array = array.map(o => (o != null ? identifier(o) : o));
15 }
16 const uniqueItems = array.filter((a, b, c) => c.indexOf(a) === b);
17 return array.length === uniqueItems.length;
18}
19exports.arrayUnique = arrayUnique;
20/**
21 * Checks if all array's values are unique. Comparison for objects is reference-based.
22 * If null or undefined is given then this function returns false.
23 */
24function ArrayUnique(identifierOrOptions, validationOptions) {
25 const identifier = typeof identifierOrOptions === 'function' ? identifierOrOptions : undefined;
26 const options = typeof identifierOrOptions !== 'function' ? identifierOrOptions : validationOptions;
27 return (0, ValidateBy_1.ValidateBy)({
28 name: exports.ARRAY_UNIQUE,
29 validator: {
30 validate: (value, args) => arrayUnique(value, identifier),
31 defaultMessage: (0, ValidateBy_1.buildMessage)(eachPrefix => eachPrefix + "All $property's elements must be unique", options),
32 },
33 }, options);
34}
35exports.ArrayUnique = ArrayUnique;
36//# sourceMappingURL=ArrayUnique.js.map
\No newline at end of file