-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-1.js
More file actions
executable file
·83 lines (71 loc) · 2.99 KB
/
example-1.js
File metadata and controls
executable file
·83 lines (71 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {createAll, cleanConsole} from './data';
const companies = createAll();
// -----------------------------------------------------------------------------
// INSTRUCTIONS IN ENGLISH
// Create a function taking the variable "companies" as a parameter and replacing
// all values "undefined" in "users" by an empty string.
// The name of each "company" must have a capital letter at the beginning as well as
// the last name and first name of each "user".
// The "companies" must be sorted by their number of "user" (decreasing order)
// and the "users" of each "company" must be listed in alphabetical order.
// -----------------------------------------------------------------------------
// INSTRUCCIONES EN ESPAÑOL
// Crear una función tomando la variable "companies" como parámetro y reemplazando
// todos los valores "undefined" en "usuarios" por un string vacío.
// El nombre de cada "company" debe tener una letra mayúscula al principio, así como
// el apellido y el nombre de cada "user".
// Las "companies" deben ordenarse por su total de "user" (orden decreciente)
// y los "users" de cada "company" deben aparecer en orden alfabético.
// -----------------------------------------------------------------------------
// INSTRUCTIONS EN FRANÇAIS
// Créer une fonction prenant en paramètre la variable "companies" et remplaçant
// toutes les valeurs "undefined" dans les "users" par un string vide.
// Le nom de chaque "company" doit avoir une majuscule au début ainsi que
// le nom et le prénom de chaque "user".
// Les "companies" doivent être triées par leur nombre de "user" (ordre décroissant)
// et les "users" de chaque "company" doivent être classés par ordre alphabétique.
/**
* Capitalize a string
* from https://flaviocopes.com/how-to-uppercase-first-letter-javascript/
* @param {String} s String to capitalize
* @return {String}
*/
function capitalize(s) {
if (typeof s !== 'string') return '';
return s.charAt(0).toUpperCase() + s.slice(1);
};
export default function f1(entity) {
return entity.map((company) => ({
...company,
name: capitalize(company.name),
users: company.users.map(
(user) => {
return Object.keys(user).reduce(
(accumulator, propertyName) => {
let value = user[propertyName];
// Replace for empty string
if (value === undefined) value = '';
// Capitalize name
if (
[
'firstName',
'lastName',
].includes(propertyName)
) value = capitalize(value);
return {
...accumulator,
[propertyName]: value,
};
},
{},
);
},
).sort(
(a, b) => a.lastName.localeCompare(b.lastName),
),
})).sort((a, b) => {
return b.users.length - a.users.length;
});
};
cleanConsole(1, companies);
console.log('---- EXAMPLE 1 --- ', f1(companies));