-
Notifications
You must be signed in to change notification settings - Fork 7
/
trick.ts
92 lines (63 loc) · 1.6 KB
/
trick.ts
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
84
85
86
87
88
89
90
91
92
// * ------------------------------------------------ ternary condition operator
{
const name: string = 'John';
const msg = name === 'admin' ? 'name is invalid' : 'name is valid';
console.log(msg);
}
// * ---------------- alternative
{
const name: string = 'John';
let msg;
if (name === 'admin') {
msg = 'name is invalid';
} else {
msg = 'name is valid';
}
console.log(msg);
}
// * ------------------------------------------------ short-circuit evaluation
{
const alias = null;
const getNickName = () => 'John';
const getUserName = () => 'User_John';
const result = alias || getNickName() || getUserName();
result && console.log(result);
}
// * ---------------- alternative
{
const alias = null;
const getNickName = () => 'John';
const getUserName = () => 'User_John';
let result: string | null = alias;
if (!result) result = getNickName();
if (!result) result = getUserName();
if (result) {
console.log(result);
}
}
// * ------------------------------------------------ quick boolean
{
const name = 'John';
const isNameEmpty = !name;
const isNameNotEmpty = !!name;
console.log(isNameEmpty, isNameNotEmpty);
}
// * ---------------- alternative
{
const name: string = 'John';
const isNameEmpty = name !== '';
const isNameNotEmpty = Boolean(name);
console.log(isNameEmpty, isNameNotEmpty);
}
// * ------------------------------------------------ quick Math.Round
{
const a = ~~2.7;
const b = ~~-2.7;
console.log(a, b);
}
// * ---------------- alternative
{
const a = Math.floor(2.7);
const b = Math.ceil(-2.7);
console.log(a, b);
}