From 450a3047b4c6e58ed9f8b27137d280c659109921 Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 10:13:15 -0700 Subject: [PATCH 1/7] WIP: writing green phase tests --- src/index.test.ts | 4 ++-- src/index.ts | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 53b0d50..4d19205 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -43,7 +43,7 @@ describe('add age to ageless people', () => { //Arrange const AGELESS_PEOPLE: ReadonlyArray> = [{ name: "Aron" }, { name: "Stormi" }] - const expected = [{ name: "Aron", age: 42 }, { name: "Stormi", age: 24 }]; + const expected = [{ name: "Aron", age: 24 }, { name: "Stormi", age: 24 }]; //Act const actual = addAge(AGELESS_PEOPLE); @@ -67,7 +67,7 @@ describe('remove age with map', () => { expect(actual).toEqual(expected); }); - it('should return an array of objects with the age key/value pairs removed', () => { + it('should return an array of objects with the age key removed', () => { //Arrange const PEOPLE_LIST = [{ name: "Aron", age: 42 }, { name: "Stormi", age: 24 }]; diff --git a/src/index.ts b/src/index.ts index ad94211..4bf6b80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,23 +2,34 @@ export interface People { readonly name: string; - readonly age: number; + age: number; } export const copyPeopleArray = (people: ReadonlyArray) => { - + const copy = [...people]; + return copy; } export const addAge = (people: ReadonlyArray>) => { - + const arrayWithAge = people.map(person => ({ + ...person, + age: 24 + })) + return arrayWithAge; } export const removeAge = (people: ReadonlyArray) => { - + const arrayWithoutAge = people.map(person => ({ + name: person.name + })) + return arrayWithoutAge; } export const removeAgeWithDestructure = (people: ReadonlyArray) => { - + const arrayWithoutAge = people.map(person => { + const {name} = person + name: name; + }) } export const removeAgeSimplifiedReturn = (people: ReadonlyArray>) => { @@ -30,5 +41,5 @@ export const changeNameAlias = (people: ReadonlyArray) => { } export const arrayOfStringNames = (people: ReadonlyArray) => { - + } \ No newline at end of file From eb5a0971884e8cb7c2e1ab3be1cbbabc73ac3dac Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 12:54:59 -0700 Subject: [PATCH 2/7] WIP writing functions to pass tests --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 4bf6b80..f1ea7c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ export const removeAgeWithDestructure = (people: ReadonlyArray) => { const {name} = person name: name; }) + return arrayWithoutAge; } export const removeAgeSimplifiedReturn = (people: ReadonlyArray>) => { From 93c3295f3c2a48dc955921e58474b259d6818d34 Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 13:49:41 -0700 Subject: [PATCH 3/7] update addAge to take record of ages --- src/index.test.ts | 5 +++-- src/index.ts | 13 ++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 4d19205..3badff2 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -43,10 +43,11 @@ describe('add age to ageless people', () => { //Arrange const AGELESS_PEOPLE: ReadonlyArray> = [{ name: "Aron" }, { name: "Stormi" }] - const expected = [{ name: "Aron", age: 24 }, { name: "Stormi", age: 24 }]; + const ages: Record = {aron: 42, stormi: 24}; + const expected = [{ name: "Aron", age: 42 }, { name: "Stormi", age: 24 }]; //Act - const actual = addAge(AGELESS_PEOPLE); + const actual = addAge(AGELESS_PEOPLE, ages); //Assert expect(actual).toEqual(expected); diff --git a/src/index.ts b/src/index.ts index f1ea7c2..83f6378 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,11 +10,14 @@ export const copyPeopleArray = (people: ReadonlyArray) => { return copy; } -export const addAge = (people: ReadonlyArray>) => { - const arrayWithAge = people.map(person => ({ - ...person, - age: 24 - })) +export const addAge = (people: ReadonlyArray>, ages: Record) => { + const arrayWithAge = people.map(person => { + const name = person.name?.toLocaleLowerCase(); + if(name != null) { + return {...person, + age: ages[name]} + } + }) return arrayWithAge; } From 4d74dd6478a700611766c2f0b5ddba7695615a94 Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 13:51:44 -0700 Subject: [PATCH 4/7] fix typo --- src/index.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 1b455cc..3fa1522 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -41,13 +41,11 @@ describe('copyPeopleArray', () => { describe('addAge', () => { it('should return an array of objects with added age to each person in the ageless array', () => { - //Arrange - const AGELESS_PEOPLE: ReadonlyArray> = [{ name: "Aron" }, { name: "Stormi" }] + const ageless_people: ReadonlyArray> = [{ name: "Aron" }, { name: "Stormi" }] const ages: Record = {aron: 42, stormi: 24}; const expected = [{ name: "Aron", age: 42 }, { name: "Stormi", age: 24 }]; - //Act - const actual = addAge(AGELESS_PEOPLE, ages); + const actual = addAge(ageless_people, ages); expect(actual).toEqual(expected); }); From 5ba45695153df73103d6f356b9921feed0200a92 Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 14:07:35 -0700 Subject: [PATCH 5/7] fix typos, fix test to pass --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 83f6378..d597f5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ export const copyPeopleArray = (people: ReadonlyArray) => { export const addAge = (people: ReadonlyArray>, ages: Record) => { const arrayWithAge = people.map(person => { - const name = person.name?.toLocaleLowerCase(); + const name = person.name?.toLowerCase(); if(name != null) { return {...person, age: ages[name]} @@ -30,14 +30,14 @@ export const removeAge = (people: ReadonlyArray) => { export const removeAgeWithDestructure = (people: ReadonlyArray) => { const arrayWithoutAge = people.map(person => { - const {name} = person - name: name; + const {name} = person; + return {name: name} }) return arrayWithoutAge; } export const removeAgeSimplifiedReturn = (people: ReadonlyArray>) => { - + } export const changeNameAlias = (people: ReadonlyArray) => { From a62f35362ab4410f94bf8dd2cffbd223fb0d5eab Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 14:24:41 -0700 Subject: [PATCH 6/7] add passing tests --- src/index.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index d597f5c..bc0efb3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,13 +37,26 @@ export const removeAgeWithDestructure = (people: ReadonlyArray) => { } export const removeAgeSimplifiedReturn = (people: ReadonlyArray>) => { - + const arrayOfNames = people.map(person => { + const {name} = person; + return {name} + }) + return arrayOfNames; } export const changeNameAlias = (people: ReadonlyArray) => { - + const idArray = people.map(person => { + const {name: id} = person; + return {id, age: person.age} + }) + return idArray; } export const arrayOfStringNames = (people: ReadonlyArray) => { - + const stringArray = people.map(person => { + if (person.name != null) { + return person.name; + } + }) + return stringArray; } \ No newline at end of file From b483a14ad882fb552c98b0bd06dbd9c48650cb81 Mon Sep 17 00:00:00 2001 From: Sarah Reimann Date: Wed, 16 Aug 2023 14:29:26 -0700 Subject: [PATCH 7/7] remove comment --- src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index bc0efb3..d1b10b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -//Define class here - export interface People { readonly name: string; age: number;