-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3-func-args.js
More file actions
37 lines (31 loc) · 924 Bytes
/
Copy path3-func-args.js
File metadata and controls
37 lines (31 loc) · 924 Bytes
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
'use strict';
// Implementation
const getPrice = ({ item }) => item.price;
const normalizeItem = ({ data, options = { convert: true } }) => {
const isTuple = Array.isArray;
const obj = isTuple(data) ? { name: data[0], price: data[1] } : data;
const { name, price, cost } = obj;
const value = price ?? cost;
const convert = options.convert && typeof value === 'string';
const amount = convert ? parseFloat(value) : value;
return { name, price: amount };
};
// Usage
const goods = [
{ name: 'Laptop', price: 1500 },
{ name: 'Phone' },
{ price: 25, name: 'Mouse' },
{ name: 'Keyboard', price: 0 },
{ name: 'HDMI cable', cost: 10 },
['Bag', 50],
['Case', '150'],
['Box'],
{ name: 'Mouse pad' },
];
goods[8].price = 5;
const output = goods.map((data) => {
const item = normalizeItem({ data });
const price = getPrice({ item });
return { data, item, price };
});
console.table(output);