Skip to content

Commit de397c4

Browse files
committed
Application of reducer. Implementation of arrow function expression.
1 parent cc0314c commit de397c4

1 file changed

Lines changed: 46 additions & 29 deletions

File tree

example-1.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
11
import {createAll, cleanConsole} from './data';
22
const companies = createAll();
33

4-
// https://flaviocopes.com/how-to-uppercase-first-letter-javascript/
5-
const capitalize = (s) => {
4+
/**
5+
* Capitalize a string
6+
* from https://flaviocopes.com/how-to-uppercase-first-letter-javascript/
7+
* @param {String} s String to capitalize
8+
* @return {String}
9+
*/
10+
function capitalize(s) {
611
if (typeof s !== 'string') return '';
712
return s.charAt(0).toUpperCase() + s.slice(1);
813
};
914

1015
export default function f1(entity) {
11-
return entity.map((company) => {
12-
const users = company.users.map((user) => {
13-
const newUser = {...user};
16+
return entity.map((company) => ({
17+
...company,
18+
name: capitalize(company.name),
19+
users: company.users.map(
20+
(user) => {
21+
return Object.keys(user).reduce(
22+
(accumulator, propertyName) => {
23+
let value = user[propertyName];
1424

15-
Object.keys(user).map(function(key) {
16-
// Replace for empty string
17-
if (newUser[key] === undefined) newUser[key] = '';
25+
// Replace for empty string
26+
if (value === undefined) value = '';
1827

19-
// Capitalize name
20-
if (['firstName', 'lastName'].includes(key) === true) newUser[key] = capitalize(newUser[key]);
21-
});
28+
// Capitalize name
29+
if (
30+
[
31+
'firstName',
32+
'lastName',
33+
].includes(propertyName)
34+
) value = capitalize(value);
2235

23-
return newUser;
24-
}).sort((a, b) => a.lastName.localeCompare(b.lastName));
25-
return {
26-
...company,
27-
name: capitalize(company.name),
28-
users,
29-
};
30-
}).sort(function(a, b) {
36+
return {
37+
...accumulator,
38+
[propertyName]: value,
39+
};
40+
},
41+
{},
42+
);
43+
},
44+
).sort(
45+
(a, b) => a.lastName.localeCompare(b.lastName),
46+
),
47+
})).sort((a, b) => {
3148
return b.users.length - a.users.length;
3249
});
3350
};
3451

3552
cleanConsole(1, companies);
3653
console.log('---- EXAMPLE 1 --- ', f1(companies));
3754

38-
// -----------------------------------------------------------------------------
39-
// INSTRUCCIONES EN ESPAÑOL
40-
41-
// Crear una función tomando la variable "companies" como parámetro y reemplazando
42-
// todos los valores "undefined" en "usuarios" por un string vacío.
43-
// El nombre de cada "company" debe tener una letra mayúscula al principio, así como
44-
// el apellido y el nombre de cada "user".
45-
// Las "companies" deben ordenarse por su total de "user" (orden decreciente)
46-
// y los "users" de cada "company" deben aparecer en orden alfabético.
47-
4855
// -----------------------------------------------------------------------------
4956
// INSTRUCTIONS IN ENGLISH
5057

@@ -55,6 +62,16 @@ console.log('---- EXAMPLE 1 --- ', f1(companies));
5562
// The "companies" must be sorted by their number of "user" (decreasing order)
5663
// and the "users" of each "company" must be listed in alphabetical order.
5764

65+
// -----------------------------------------------------------------------------
66+
// INSTRUCCIONES EN ESPAÑOL
67+
68+
// Crear una función tomando la variable "companies" como parámetro y reemplazando
69+
// todos los valores "undefined" en "usuarios" por un string vacío.
70+
// El nombre de cada "company" debe tener una letra mayúscula al principio, así como
71+
// el apellido y el nombre de cada "user".
72+
// Las "companies" deben ordenarse por su total de "user" (orden decreciente)
73+
// y los "users" de cada "company" deben aparecer en orden alfabético.
74+
5875
// -----------------------------------------------------------------------------
5976
// INSTRUCTIONS EN FRANÇAIS
6077

0 commit comments

Comments
 (0)