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

Feature/#230 experience and studies sorted by date #234

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExperienceVm } from './experience-section.vm';
import { dateExtractor, sortByDate } from '../../helpers';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '@/helpers' on import and move it at first line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With import in this way I get test failures


export const sortRolesByDate = (experience: ExperienceVm): ExperienceVm => ({
...experience,
roles: sortByDate(experience.roles, 'startDate'),
});

export const mapSortedRolesIntoExperience = (experience: ExperienceVm[]): ExperienceVm[] =>
experience.map(experienceItem => sortRolesByDate(experienceItem));

export const sortExperienceByDate = (experience: ExperienceVm[]): ExperienceVm[] =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between sortExperienceByDate and sortByDate functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In sortExperienceByDate, specifies that the sorting is performed on the first element of the array as a parameter of dateExtractor

experience.sort(
(a: ExperienceVm, b) => dateExtractor(b.roles[0], 'startDate') - dateExtractor(a.roles[0], 'startDate')
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ManfredAwesomicCV } from '../../model';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '@/model'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

import { ExperienceVm, Type } from './experience-section.vm';
import { types } from './experience-section.contants';
import { mapSortedRolesIntoExperience, sortExperienceByDate } from './experience-section.helpers';

export const mapFromMacCvToExperienceSectionVm = (cv: ManfredAwesomicCV): ExperienceVm[] => {
let jobs: ExperienceVm[] = [];
Expand All @@ -16,7 +17,10 @@ export const mapFromMacCvToExperienceSectionVm = (cv: ManfredAwesomicCV): Experi
jobs = [...jobs, { name: organizationName, description: organizationDescription, type: mapType, roles }];
});

return jobs;
const jobsWithMappedRoles: ExperienceVm[] = mapSortedRolesIntoExperience(jobs);
const jobsSortedByDate: ExperienceVm[] = sortExperienceByDate(jobsWithMappedRoles);

return jobsSortedByDate;
};

export const mapOrganizationType = (type: string, types: Type[]): string => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import { sortStudiesByDate } from './studies-section.helpers';
import { StudiesSectionVm } from './studies-section.vm';

describe('studies-section.helpers specs', () => {
describe('sortStudiesByDate', () => {
it('should return studies section sorted by starting date', () => {
// Arrange
const studies: StudiesSectionVm[] = [
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2009-04-02',
finishDate: '2011-03-20',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
{
studyType: 'Grado oficial',
degreeAchieved: false,
name: 'name',
startDate: '2022-11-17',
finishDate: '2023-05-01',
description: 'description',
institution: {
name: 'name',
location: {
country: 'España',
region: 'region',
address: 'address',
},
},
},
];

// Act
const result: StudiesSectionVm[] = sortStudiesByDate(studies);

const expectedResult: StudiesSectionVm[] = [
{
studyType: 'Grado oficial',
degreeAchieved: false,
name: 'name',
startDate: '2022-11-17',
finishDate: '2023-05-01',
description: 'description',
institution: {
name: 'name',
location: {
country: 'España',
region: 'region',
address: 'address',
},
},
},
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2009-04-02',
finishDate: '2011-03-20',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
];

// Assert
expect(result).toEqual(expectedResult);
});

it('should return studies section sorted by starting date. No changes if already sorted', () => {
// Arrange
const studies: StudiesSectionVm[] = [
{
studyType: 'Grado oficial',
degreeAchieved: false,
name: 'name',
startDate: '2022-11-17',
finishDate: '2023-05-01',
description: 'description',
institution: {
name: 'name',
location: {
country: 'España',
region: 'region',
address: 'address',
},
},
},
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
];

// Act
const result: StudiesSectionVm[] = sortStudiesByDate(studies);

const expectedResult: StudiesSectionVm[] = [
{
studyType: 'Grado oficial',
degreeAchieved: false,
name: 'name',
startDate: '2022-11-17',
finishDate: '2023-05-01',
description: 'description',
institution: {
name: 'name',
location: {
country: 'España',
region: 'region',
address: 'address',
},
},
},
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
];

// Assert
expect(result).toEqual(expectedResult);
});

it('should return same array with one element when array has one element', () => {
// Arrange
const studies: StudiesSectionVm[] = [
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
];

// Act
const result: StudiesSectionVm[] = sortStudiesByDate(studies);

const expectedResult: StudiesSectionVm[] = [
{
studyType: 'Certificación',
degreeAchieved: true,
name: 'name2',
startDate: '2020-12-03',
finishDate: '2022-07-08',
description: 'description2',
institution: {
name: 'name2',
location: {
country: 'Italia',
region: 'region2',
address: 'address2',
},
},
},
];

// Assert
expect(result).toEqual(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { sortByDate } from '../../helpers';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '@/helpers"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with experience. With import in this way I get test failures


import { StudiesSectionVm } from './studies-section.vm';

export const sortStudiesByDate = (studies: StudiesSectionVm[]): StudiesSectionVm[] => sortByDate(studies, 'startDate');
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ManfredAwesomicCV } from '@/model';
import { CountryType, Institution, StudiesSectionVm, StudyTypeWithTranslation } from './studies-section.vm';
import { studiesTypes, countryList } from './studies-section.constants';
import { sortStudiesByDate } from './studies-section.helpers';

export const mapFromMacCvToStudiesSectionVm = (cv: ManfredAwesomicCV): StudiesSectionVm[] => {
const studiesMap: StudiesSectionVm[] =
Expand Down Expand Up @@ -35,7 +36,9 @@ export const mapFromMacCvToStudiesSectionVm = (cv: ManfredAwesomicCV): StudiesSe
};
}) ?? [];

return studiesMap;
const studiesSortedByDate: StudiesSectionVm[] = sortStudiesByDate(studiesMap);

return studiesSortedByDate;
};

export const mapStudiesTypes = (studiesType: string, studiesTypes: StudyTypeWithTranslation[]): string => {
Expand Down
10 changes: 10 additions & 0 deletions packages/manfred-common/src/helpers/date-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const dateExtractor = <T>(item: T, path: keyof T): number => {
const date = item[path];
if (typeof date === 'string') {
return new Date(date).getTime();
}
throw new Error(`Invalid path: ${path.toString()}`);
};

export const sortByDate = <T>(array: T[], path: keyof T): T[] =>
array.sort((a: T, b: T) => dateExtractor(b, path) - dateExtractor(a, path));
1 change: 1 addition & 0 deletions packages/manfred-common/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './date-helpers';
Loading