Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: array unique flatmap for flattening array returns #2138

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/decorator/array/ArrayUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function arrayUnique(array: unknown[], identifier?: ArrayUniqueIdentifier
if (!Array.isArray(array)) return false;

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

const uniqueItems = array.filter((a, b, c) => c.indexOf(a) === b);
Expand Down
45 changes: 45 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4610,6 +4610,51 @@ describe('ArrayUnique with identifier', () => {
});
});

describe('ArrayUnique with array return values', () => {
const identifier = o => o.name;
const validValues = [
[['world'], ['hello'], ['superman']],
[['world', 'superman'], ['hello']],
[['superman', 'world', 'hello']],
[['1'], ['2'], [null], [undefined]],
].map(list => list.map(name => ({ name })));
const invalidValues: any[] = [
null,
undefined,
[['world'], ['hello'], ['hello']],
[['world'], ['hello', 'hello']],
[['world', 'hello'], ['world']],
[['1'], ['1'], ['1']],
].map(list => list?.map(name => (name != null ? { name } : name)));

class MyClass {
@ArrayUnique(identifier)
someProperty: { name: string }[];
}

it('should not fail if validator.validate said that its valid', () => {
return checkValidValues(new MyClass(), validValues);
});

it('should fail if validator.validate said that its invalid', () => {
return checkInvalidValues(new MyClass(), invalidValues);
});

it('should not fail if method in validator said that its valid', () => {
validValues.forEach(value => expect(arrayUnique(value, identifier)).toBeTruthy());
});

it('should fail if method in validator said that its invalid', () => {
invalidValues.forEach(value => expect(arrayUnique(value, identifier)).toBeFalsy());
});

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

describe('isInstance', () => {
class MySubClass {
// Empty
Expand Down