-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (33 loc) · 1 KB
/
Copy pathindex.js
File metadata and controls
39 lines (33 loc) · 1 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
export default function splitString(input, separator) {
if(typeof separator !== "string" || typeof input !== 'string'){
throw new Error({
message:"Invalid data passed",
functionName: 'splitString',
arguments
})
}
// Early bail if inputs are 0 length
if ( input.length === 0 || separator.length === 0) {
return [];
}
let strle = separator.length;
let output = [];
let lastIndex = 0;
for (let i = 0; i <= input.length - strle; ) {
let x = i + strle;
if (input.slice(i, x) === separator) {
// This is case where there are contiguous amount of separator
if (lastIndex !== i) {
output.push(input.slice(lastIndex, i));
}
output.push(separator);
lastIndex = i = x;
} else {
i++;
}
}
if (lastIndex < input.length) {
output.push(input.slice(lastIndex, input.length));
}
return output;
}