-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsplit.js
More file actions
159 lines (134 loc) · 4.46 KB
/
split.js
File metadata and controls
159 lines (134 loc) · 4.46 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
*
MDN Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
The split() method splits a String object into an array of strings by separating the
string into substrings, using a specified separator string to determine where to make each split.
The method has two optional arguments:
- separator: defines the points at which each split should occur.
- limit: integer specifying a limit for the number of splits.
It returns an array of strings split at each point defined by the separator.
Example 1: no arguments passed
"No arguments".split();
must return:
[ "No arguments" ]
Example 2: passing separator and limit
"Oh brave new world that has such people in it.".split(" ", 4);
must return:
[ "Oh", "brave", "new", "world" ]
Example 3: array
"ca,bc,a,bca,bca,bc".split(["a", "b"])
must return:
[ "c", "c,", "c", "c", "c" ]
Example 4: regex
"foobarfoobarfoobar".split(/bar/)
must return:
[ "foo", "foo", "foo", "" ]
Example 5: regex with capturing group
"Hello 1 word. Sentence number 2.".split(/(\d)/)
must return:
[ "Hello ", "1", " word. Sentence number ", "2", "." ]
*/
String.prototype.split = function mySplit(separator, limit) {
const returnValue = [];
let copyThis = '';
// join characters into a string
for (let i = 0; i < this.length; i += 1) {
copyThis += this[i];
}
// check if there is a null separator
if (separator === null || separator === undefined) {
if (limit !== 0) {
returnValue.push(copyThis);
}
} else {
let finalSeparator = separator;
// check if separator is regex
if (
Object.prototype.toString.call(finalSeparator) === '[object RegExp]'
) {
let isDone = false;
let counter = 0;
while (!isDone) {
// checks if theres a limit
if (limit !== null && limit !== undefined) {
if (counter >= limit) {
isDone = true;
break;
}
}
counter += 1;
// executes regex
const regexResult = finalSeparator.exec(copyThis);
// if it has found something, adds prior text to array
// and removes the first occasion of regex
if (regexResult !== null) {
let text = '';
for (let i = 0; i < regexResult.index; i += 1) {
text += copyThis[i];
}
copyThis = copyThis.substr(regexResult.index + regexResult[0].length, copyThis.length);
returnValue.push(text);
// check if it has a capturing group on regex
if (regexResult.length > 1) {
returnValue.push(regexResult[0]);
}
} else {
returnValue.push(copyThis);
isDone = true;
}
}
} else {
// check if separator is array
if (
Object.prototype.toString.call(finalSeparator) === '[object Array]'
) {
// transforms to a string
finalSeparator = '';
for (let i = 0; i < separator.length; i += 1) {
finalSeparator += separator[i];
if (i < separator.length - 1) {
finalSeparator += ',';
}
}
}
// check if separator is empty string
if (finalSeparator !== '') {
let isDone = false;
let counter = 0;
while (!isDone) {
if (limit !== null && limit !== undefined && limit >= 0) {
if (counter >= limit) {
isDone = true;
break;
}
}
counter += 1;
const index = copyThis.indexOf(finalSeparator);
// if it has found the index of separator, add prior text
// and remove the first occasion of separator
if (index !== -1) {
let text = '';
for (let i = 0; i < index; i += 1) {
text += copyThis[i];
}
copyThis = copyThis.substr(index + finalSeparator.length, copyThis.length);
returnValue.push(text);
} else {
returnValue.push(copyThis);
isDone = true;
}
}
} else {
// if it is an empty string, just separate characters from string
// also check if theres a limit defined
const finalLimit = limit !== null && limit !== undefined && limit >= 0
? Math.min(limit, this.length)
: this.length;
for (let i = 0; i < finalLimit; i += 1) {
returnValue.push(copyThis[i]);
}
}
}
}
return returnValue;
};