From 64ac14a0ea7ddd57462c0766954186884fbf3cc5 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 15:38:42 -0500 Subject: [PATCH 01/10] :art: es6 functions and oneliners --- index.html | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/index.html b/index.html index 508ebb3d..a4b36821 100644 --- a/index.html +++ b/index.html @@ -101,7 +101,7 @@ * ... * ] */ - function getPersonList() { + const getPersonList = () => { return new Promise((resolve, reject) => { fetch('https://willowtreeapps.com/api/v1.0/profiles') .then(response => { @@ -124,24 +124,18 @@ ***************************************************/ - function getLastName(person) { - return person.lastName; - } + const getLastName = person => person.lastName; - const getFirstName = (person) => { - return person.firstName; - }; + const getFirstName = person => person.firstName; // headshot URLs are scheme relative // // prepend http: to prevent invalid schemes like file:// or uri:// - const getImageUrl = (person) => { - return `http:${person.headshot.url}`; - }; + const getImageUrl = person => `http:${person.headshot.url}`; /** * Fisher-Yates shuffle */ - function shuffleList(list) { + const shuffleList = (list) => { // Make a copy & don't mutate the passed in list let result = list.slice(1); @@ -162,11 +156,10 @@ * Remove any people that do not have the name we are * searching for. */ - function filterByName(searchForName, personList) { - return personList.filter((person) => { - return person.firstName === searchForName || person.lastName === searchForName; - }); - } + const filterByName = (searchForName, personList) => + personList.filter(person => + person.firstName === searchForName || person.lastName === searchForName + ); /** @@ -185,26 +178,19 @@ * > [{ name: 'Jon' }, { name: 'Kevin' }, { name: 'Sam' }] * */ - function sortObjListByProp(prop) { - return function(objList) { - // Make a copy & don't mutate the passed in list + const sortObjListByProp = prop => + (objList) => { + // Make a copy & don't mutate the passed in list let result = objList.slice(1); result.sort((a, b) => { - if (a[prop] < b[prop]) { - return -1; - } - - if (a[prop] > b[prop]) { - return 1; - } - + if (a[prop] < b[prop]) return -1; + if (a[prop] > b[prop]) return 1; return 1; }); - return result; + return result; }; - } const sortByFirstName = sortObjListByProp('firstName'); From 39abc5149a418fb1214eccab891c954633f4f45b Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 15:53:09 -0500 Subject: [PATCH 02/10] :goat: change filterByName to remove case sensitivity & improve interaction --- index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index a4b36821..660d42a8 100644 --- a/index.html +++ b/index.html @@ -158,7 +158,8 @@ */ const filterByName = (searchForName, personList) => personList.filter(person => - person.firstName === searchForName || person.lastName === searchForName + person.firstName.toLowerCase().includes(searchForName.toLowerCase()) || + person.lastName.toLowerCase().includes(searchForName.toLowerCase()) ); From eff63732936b6a6c9d45d606bb5d0e379ef73a4e Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:05:02 -0500 Subject: [PATCH 03/10] :art: change promise format to async await --- index.html | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 660d42a8..4bf78c0d 100644 --- a/index.html +++ b/index.html @@ -101,19 +101,24 @@ * ... * ] */ - const getPersonList = () => { - return new Promise((resolve, reject) => { - fetch('https://willowtreeapps.com/api/v1.0/profiles') - .then(response => { - if (response.status !== 200) { - reject(new Error("Error!")); - } - - response.json().then(imageList => { - resolve(imageList); - }); - }); - }); + const getPersonList = async () => { + try { + const res = await fetch('https://willowtreeapps.com/api/v1.0/profiles'); + return await res.json(); + } catch(err) { + console.log(err); + } + + // .then(response => { + // if (response.status !== 200) { + // reject(new Error("Error!")); + // } + + // response.json().then(imageList => { + // resolve(imageList); + // }); + // }); + // }); } @@ -238,12 +243,12 @@ }; }, - componentDidMount() { - getPersonList().then((personList) => - this.setState({ - personList, - visiblePersonList: personList - })); + async componentDidMount() { + const personList = await getPersonList(); + this.setState({ + personList, + visiblePersonList: personList + }); }, _shuffleList() { From 10a3a4dc2406be3588bb64c0dd7c8ba3f9307950 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:05:35 -0500 Subject: [PATCH 04/10] remove extraneous code --- index.html | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/index.html b/index.html index 4bf78c0d..b8821c57 100644 --- a/index.html +++ b/index.html @@ -108,17 +108,6 @@ } catch(err) { console.log(err); } - - // .then(response => { - // if (response.status !== 200) { - // reject(new Error("Error!")); - // } - - // response.json().then(imageList => { - // resolve(imageList); - // }); - // }); - // }); } From 4659d3de41e37fef352dbb29ec59581adfeddbb1 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:15:44 -0500 Subject: [PATCH 05/10] :art::goat: improve shuffeList --- index.html | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index b8821c57..1e841023 100644 --- a/index.html +++ b/index.html @@ -131,17 +131,12 @@ */ const shuffleList = (list) => { // Make a copy & don't mutate the passed in list - let result = list.slice(1); + let result = list.slice(); - let tmp, j, i = list.length - 1 - - for (; i > 0; i -= 1) { - j = Math.floor(Math.random() * (i + 1)); - tmp = list[i]; - list[i] = list[j]; - list[j] = tmp; + for (let i = result.length - 1; i > 0; i--) { + let j = Math.floor(Math.random() * (i + 1)); + [result[i], result[j]] = [result[j], result[i]]; } - return result; } From 3d937fb0ce14e76665d15b3a1c7a6062b9eb040e Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:18:18 -0500 Subject: [PATCH 06/10] fix sortByLastName --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 1e841023..2d81f169 100644 --- a/index.html +++ b/index.html @@ -184,7 +184,7 @@ const sortByFirstName = sortObjListByProp('firstName'); - const sortByLastName = (personList) => sortByFirstName(personList).reverse(); + const sortByLastName = sortObjListByProp('lastName'); /*================================================== From cd0684cc34d4f4a8eca0ef61fd954876cf2bf5cc Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:20:59 -0500 Subject: [PATCH 07/10] :art: improve sortObjListByProp --- index.html | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 2d81f169..67316e45 100644 --- a/index.html +++ b/index.html @@ -171,15 +171,9 @@ const sortObjListByProp = prop => (objList) => { // Make a copy & don't mutate the passed in list - let result = objList.slice(1); - - result.sort((a, b) => { - if (a[prop] < b[prop]) return -1; - if (a[prop] > b[prop]) return 1; - return 1; - }); - - return result; + let result = objList.slice(); + result.sort((a, b) => a[prop] < b[prop] ? -1 : 1); + return result; }; const sortByFirstName = sortObjListByProp('firstName'); From cca4b00e23c3e75f39ed07f4a7d6d7a5ef8d69da Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:22:44 -0500 Subject: [PATCH 08/10] :art: improve sortObjListByProp even more --- index.html | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 67316e45..9d40ff7b 100644 --- a/index.html +++ b/index.html @@ -169,12 +169,11 @@ * */ const sortObjListByProp = prop => - (objList) => { - // Make a copy & don't mutate the passed in list - let result = objList.slice(); - result.sort((a, b) => a[prop] < b[prop] ? -1 : 1); - return result; - }; + // Make a copy & don't mutate the passed in list + objList => + objList + .slice() + .sort((a, b) => a[prop] < b[prop] ? -1 : 1); const sortByFirstName = sortObjListByProp('firstName'); From 2eff49e9a07d56c04e96d10479e3aa8387c48725 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:30:02 -0500 Subject: [PATCH 09/10] :tada: add feature reverse sort --- index.html | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 9d40ff7b..762c3c6c 100644 --- a/index.html +++ b/index.html @@ -175,6 +175,9 @@ .slice() .sort((a, b) => a[prop] < b[prop] ? -1 : 1); + const shouldReverse = (shuffled, visible) => + shuffled[0] === visible[0] ? shuffled.reverse() : shuffled; + const sortByFirstName = sortObjListByProp('firstName'); const sortByLastName = sortObjListByProp('lastName'); @@ -236,13 +239,19 @@ _sortByFirst() { this.setState({ - visiblePersonList: sortByFirstName(this.state.personList) + visiblePersonList: shouldReverse( + sortByFirstName(this.state.personList), + this.state.visiblePersonList + ) }); }, _sortByLast() { this.setState({ - visiblePersonList: sortByLastName(this.state.personList) + visiblePersonList: shouldReverse( + sortByLastName(this.state.personList), + this.state.visiblePersonList + ) }); }, From c01d5d4b94496c5cc6280b93166f7737bd04959d Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 7 Feb 2018 16:36:39 -0500 Subject: [PATCH 10/10] make spacing consistent --- index.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 762c3c6c..0614f845 100644 --- a/index.html +++ b/index.html @@ -103,10 +103,10 @@ */ const getPersonList = async () => { try { - const res = await fetch('https://willowtreeapps.com/api/v1.0/profiles'); - return await res.json(); + const res = await fetch('https://willowtreeapps.com/api/v1.0/profiles'); + return await res.json(); } catch(err) { - console.log(err); + console.log(err); } } @@ -146,10 +146,10 @@ * searching for. */ const filterByName = (searchForName, personList) => - personList.filter(person => - person.firstName.toLowerCase().includes(searchForName.toLowerCase()) || - person.lastName.toLowerCase().includes(searchForName.toLowerCase()) - ); + personList.filter(person => + person.firstName.toLowerCase().includes(searchForName.toLowerCase()) || + person.lastName.toLowerCase().includes(searchForName.toLowerCase()) + ); /**