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Y,EAAC,UAAU,CAAC,EAAE,CAAC,UAAU,GAAG,yCAAyC,EAAE,OAAO,CAAC;SAC5G;KACF,EACD,OAAO,CACR,CAAC;AACJ,CAAC;AAjBD,kCAiBC","sourcesContent":["import { ValidationOptions } from '../ValidationOptions';\nimport { buildMessage, ValidateBy } from '../common/ValidateBy';\n\nexport const ARRAY_UNIQUE = 'arrayUnique';\nexport type ArrayUniqueIdentifier<T = any> = (o: T) => any;\n\n/**\n * Checks if all array's values are unique. Comparison for objects is reference-based.\n * If null or undefined is given then this function returns false.\n */\nexport function arrayUnique(array: unknown[], identifier?: ArrayUniqueIdentifier): boolean {\n if (!Array.isArray(array)) return false;\n\n if (identifier) {\n array = array.map(o => (o != null ? identifier(o) : o));\n }\n\n const uniqueItems = array.filter((a, b, c) => c.indexOf(a) === b);\n return array.length === uniqueItems.length;\n}\n\n/**\n * Checks if all array's values are unique. Comparison for objects is reference-based.\n * If null or undefined is given then this function returns false.\n */\nexport function ArrayUnique<T = any>(\n identifierOrOptions?: ArrayUniqueIdentifier<T> | ValidationOptions,\n validationOptions?: ValidationOptions\n): PropertyDecorator {\n const identifier = typeof identifierOrOptions === 'function' ? identifierOrOptions : undefined;\n const options = typeof identifierOrOptions !== 'function' ? identifierOrOptions : validationOptions;\n\n return ValidateBy(\n {\n name: ARRAY_UNIQUE,\n validator: {\n validate: (value, args): boolean => arrayUnique(value, identifier),\n defaultMessage: buildMessage(eachPrefix => eachPrefix + \"All $property's elements must be unique\", options),\n },\n },\n options\n );\n}\n"]}
\No newline at end of file