Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/decorator/array/ArrayUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ export type ArrayUniqueIdentifier<T = any> = (o: T) => any;
export function arrayUnique(array: unknown[], identifier?: ArrayUniqueIdentifier): boolean {
if (!Array.isArray(array)) return false;

if (identifier) {
array = array.map(o => (o != null ? identifier(o) : o));
}

const uniqueItems = array.filter((a, b, c) => c.indexOf(a) === b);
return array.length === uniqueItems.length;
const values = identifier ? array.map(o => (o != null ? identifier(o) : o)) : array;
return new Set(values).size === values.length;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5025,6 +5025,24 @@ describe('ArrayUnique', () => {
invalidValues.forEach(value => expect(arrayUnique(value)).toBeFalsy());
});

it('should compare objects by reference', () => {
const object = { name: 'test' };

expect(arrayUnique([object, { name: 'test' }])).toBeTruthy();
expect(arrayUnique([object, object])).toBeFalsy();
});

it('should use SameValueZero semantics for NaN', () => {
expect(arrayUnique([1, NaN, 2])).toBeTruthy();
expect(arrayUnique([NaN, NaN])).toBeFalsy();
});

it('should treat sparse array items as undefined', () => {
expect(arrayUnique([, 1])).toBeTruthy();
expect(arrayUnique([, , 1])).toBeFalsy();
expect(arrayUnique([undefined, , 1])).toBeFalsy();
});

it('should return error object with proper data', () => {
const validationType = 'arrayUnique';
const message = "All someProperty's elements must be unique";
Expand Down Expand Up @@ -5069,6 +5087,19 @@ describe('ArrayUnique with identifier', () => {
invalidValues.forEach(value => expect(arrayUnique(value, identifier)).toBeFalsy());
});

it('should call the identifier for every non-null value before checking uniqueness', () => {
const visitedNames: string[] = [];
const trackingIdentifier = (value: { name: string }): string => {
visitedNames.push(value.name);
return value.name;
};

expect(
arrayUnique([{ name: 'duplicate' }, { name: 'duplicate' }, { name: 'last' }], trackingIdentifier)
).toBeFalsy();
expect(visitedNames).toEqual(['duplicate', 'duplicate', 'last']);
});

it('should return error object with proper data', () => {
const validationType = 'arrayUnique';
const message = "All someProperty's elements must be unique";
Expand Down