-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2-func-good.js
More file actions
68 lines (56 loc) · 1.66 KB
/
Copy path2-func-good.js
File metadata and controls
68 lines (56 loc) · 1.66 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
'use strict';
// Code improved characteristics:
// - simple, no code duplication
// - linear logic: less "if" (3 vs 6)
// - isolated complexity of "if" logic (readability)
// - decomposed, no mixed responsibility (readability)
// - unpredictability is isolated
// - monomorphic code (opt, object shapes)
// - isolated polymorphic shapes
// - SOLID:SRP (Single Responsibility Principle)
// - SoC (Separation of Concerns)
// - consistent return (check with eslint)
// - function inlining, const inlining
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 };
};
/*
type Item = {
name: string;
price?: number;
};
type RawTuple = [string, number];
type RawObject = {
name: string;
price?: number;
cost?: number;
};
declare function getPrice(item: Item): number | undefined;
declare function normalizeItem(data: RawTuple | RawObject): Item;
*/
// 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);