-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.ts
More file actions
142 lines (109 loc) · 3.02 KB
/
split.ts
File metadata and controls
142 lines (109 loc) · 3.02 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//Below is a series of refactorings
let contractIds = [];
if (opts.contractIds) {
if (opts.contractIds.indexOf(';') === -1) {
contractIds = [opts.contractIds];
} else {
contractIds = opts.contractIds.split(';');
}
}
return contractIds;
//--------------------
let contractIds = [];
if (opts.contractIds) {
//switch to a ternary
return opts.contractIds.indexOf(';') === -1
? [opts.contractIds]
: opts.contractIds.split(';');
}
return contractIds;
//--------------------
if (opts.contractIds) {
return opts.contractIds.indexOf(';') === -1
? [opts.contractIds]
: opts.contractIds.split(';');
} else {
//pull the null case into the else
return [];
}
//--------------------
if (opts.contractIds) {
//use contains
return contains(';', opts.contractIds)
? [opts.contractIds]
: opts.contractIds.split(';');
} else {
return [];
}
const contains = (delimiter: string, str: string) =>
(str === null || str === undefined)
? false
: str.indexOf(delimiter) !== -1
//--------------------
//Use isNothing and invert `if`
if (isNothing(opts.contractIds)) {
return [];
} else {
return contains(';', opts.contractIds)
? [opts.contractIds]
: opts.contractIds.split(';');
}
//add nothing
const isNothing = (str: string) =>
(str === null || str === undefined)
const contains = (delimiter: string, str: string) =>
isNothing
? false
: str.indexOf(delimiter) !== -1
//--------------------
//Use another ternary
return isNothing(opts.contractIds) ? []
: contains(';', opts.contractIds) ? [opts.contractIds]
: opts.contractIds.split(';');
const isNothing = (str: string) =>
(str === null || str === undefined)
const contains = (delimiter: string, str: string) =>
isNothing
? false
: str.indexOf(delimiter) !== -1
//--------------------
const str = opts.contractIds
return isNothing(str) ? []
: contains(';', str) ? [str]
: str.split(';');
const isNothing = (str: string) =>
(str === null || str === undefined)
const contains = (delimiter: string, str: string) =>
isNothing
? false
: str.indexOf(delimiter) !== -1
//taken from some other code... helps split strings properly
function split(string: string, separator: string): string[] {
if (isNothing(str)) {
return [];
}
let arr = string.split(separator);
// without the following check, `"".split(';')` returns `['']`
return arr.length === 1 && arr[0] === '' ? [] : arr;
}
//--------------------
//watch closely to our main function...
// before
return isNothing(str) ? []
: contains(';', str) ? [str]
: str.split(';');
// after
const str = opts.contractIds
return split(';', str);
//...
function split(separator: string, string: string): string[] {
if (str === null || str === undefined) {
return [];
}
let arr = str.split(separator);
return arr.length === 1 && arr[0] === '' ? [] : arr;
}
//--------------------
//Final answer
return split(';', opts.contractIds);
//With heavy unit testing on the new `split` function