Skip to content

Commit

Permalink
fix(classes, core): add ability to map Date[] types (#399)
Browse files Browse the repository at this point in the history
* fix(classes, core): add ability to map Date[] types

instantiate.util.ts@classes and map@core weren't mapping Date[] types correctly. Add checks for this
case.

#397

* fix(pojos): adding pojos support for Date[] types

Pojos instantiate util wasn't correctly handling Date[] types, adding support for them.

#397

Co-authored-by: Roberto <macbookpro@Robertos-MacBook-Pro.local>
  • Loading branch information
roblopz and Roberto authored Jan 11, 2022
1 parent 5a56529 commit cb27b1a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/classes/src/lib/utils/instantiate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export function instantiate<TModel extends Dictionary<TModel>>(
// if is Date, assign a new Date value if valueAtKey is defined, otherwise, undefined
if (isDateConstructor(metaResult)) {
const value = isDefined(valueAtKey)
? new Date(valueAtKey as number)
? Array.isArray(valueAtKey)
? [...valueAtKey]
: new Date(valueAtKey as number)
: undefined;
setMutate(instance as Record<string, unknown>, key, value);
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/map/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Original error: ${originalError}`;
if (Array.isArray(mapInitializedValue)) {
const [first] = mapInitializedValue;
// if first item is a primitive
if (typeof first !== 'object') {
if (typeof first !== 'object' || first instanceof Date) {
setMember(() => mapInitializedValue.slice());
continue;
}
Expand Down
16 changes: 16 additions & 0 deletions packages/integration-test/src/lib/assert-vm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ export function assertVm<
expect(vm.last).toEqual(user.lastName);
expect(vm.full).toEqual(user.firstName + ' ' + user.lastName);

if (user.logins.length) {
const lastModelLogin = user.logins[user.logins.length - 1];

expect(Array.isArray(vm.logins)).toBe(true);
expect(vm.logins).toHaveLength(user.logins.length);

for (let i = 0; i < user.logins.length; i++)
expect(vm.logins[i].getDate()).toEqual(user.logins[i].getDate());

expect(vm.lastLogin.toDateString()).toEqual(lastModelLogin.toDateString());
} else {
expect(Array.isArray(vm.logins)).toBe(true);
expect(vm.logins).toHaveLength(0);
expect(vm.lastLogin).toBeFalsy();
}

expect(vm.profile.birthday).toEqual(user.profile.birthday.toDateString());
expect(vm.profile.bio).toEqual(user.profile.bio);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class User {
profile!: UserProfile;
@AutoMap({ typeFn: () => Job })
job!: Job;
@AutoMap()
logins: Date[];
}

export class UserVm {
Expand All @@ -26,4 +28,8 @@ export class UserVm {
jobTitle!: string;
@AutoMap()
jobAnnualSalary!: number;
@AutoMap()
logins: Date[];
@AutoMap()
lastLogin?: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const fullNameResolver: Resolver<
},
};

const lastLoginResolver: Resolver<
User,
UserVm | PascalUserVm | SnakeUserVm,
Date
> = {
resolve(source: User): Date {
return source.logins?.length
? source.logins[source.logins.length - 1]
: null;
},
};

export const userProfile: MappingProfile = (mapper) => {
mapper
.createMap(User, UserVm)
Expand All @@ -25,7 +37,8 @@ export const userProfile: MappingProfile = (mapper) => {
(d) => d.last,
mapFrom((s) => s.lastName)
)
.forMember((d) => d.full, mapFrom(fullNameResolver));
.forMember((d) => d.full, mapFrom(fullNameResolver))
.forMember((d) => d.lastLogin, mapFrom(lastLoginResolver));

mapper
.createMap(User, PascalUserVm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export function getUser(
const user = new User();
user.firstName = 'Chau';
user.lastName = 'Tran';
user.logins = [
new Date('01/10/2021'),
new Date('05/11/2021'),
new Date('12/12/2021'),
];

const userJob = new Job();
userJob.title = 'Developer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface User {
lastName: string;
profile: UserProfile;
job: Job;
logins: Date[];
}

export interface UserVm {
Expand All @@ -16,6 +17,8 @@ export interface UserVm {
profile: UserProfileVm;
jobTitle: string;
jobAnnualSalary: number;
logins: Date[];
lastLogin?: Date;
}

export function createUserMetadata() {
Expand All @@ -24,6 +27,7 @@ export function createUserMetadata() {
lastName: String,
profile: 'UserProfile',
job: 'Job',
logins: Date,
});
createMetadataMap('UserVm', {
first: String,
Expand All @@ -32,5 +36,7 @@ export function createUserMetadata() {
profile: 'UserProfileVm',
jobTitle: String,
jobAnnualSalary: Number,
logins: Date,
lastLogin: Date,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export const userProfile: MappingProfile = (mapper) => {
.forMember(
(d) => d.full,
mapFrom((s) => s.firstName + ' ' + s.lastName)
)
.forMember(
(d) => d.lastLogin,
mapFrom((s) => (s.logins?.length ? s.logins[s.logins.length - 1] : null))
);

mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export function getUser(
lastName: 'Tran',
job: userJob,
profile: userProfile,
logins: [
new Date('01/10/2021'),
new Date('05/11/2021'),
new Date('12/12/2021'),
],
...(partials.user ?? {}),
} as User;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/pojos/src/lib/utils/instantiate.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export function instantiate<TModel extends Dictionary<TModel>>(

if (isDateConstructor(metaResult)) {
const value = isDefined(valueAtKey)
? new Date(valueAtKey as number)
? Array.isArray(valueAtKey)
? [...valueAtKey]
: new Date(valueAtKey as number)
: undefined;
setMutate(obj as Record<string, unknown>, key, value);
continue;
Expand Down

0 comments on commit cb27b1a

Please sign in to comment.