Passed
Push — master ( 63524c...dd4398 )
by Night
01:10
created

stringFuncs.between   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 11
nop 5
dl 0
loc 27
rs 8
c 0
b 0
f 0
1
/** global: UB */
2
3
var stringFuncs = {
4
5
	
6
	// other search functions
7 View Code Duplication
	after: function(find, returnAll = false, inclusive = false, startAt = 0, forward = true){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
		var text = this;
9
		if (text == null) { return returnAll ? text : ''; }
1 ignored issue
show
Best Practice introduced by
Comparing text to null using the == operator is not safe. Consider using === instead.
Loading history...
10
		if (text.length === 0) {
11
			return text;
12
		}
13
		if (!forward) {
14
			var idx = text.lastIndexOf(find, startAt);
15
		}else {
16
			idx = text.indexOf(find, startAt);
17
		}
18
		if (idx == -1) { return returnAll ? text : ''; }
19
		if (!inclusive) {
20
			idx += find.length;
21
		}
22
		return text.substr(idx);
23
	},
24 View Code Duplication
	before: function(find, returnAll = false, inclusive = false, startAt = 0, forward = true){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
		var text = this;
26
		if (text == null) { return returnAll ? text : ''; }
1 ignored issue
show
Best Practice introduced by
Comparing text to null using the == operator is not safe. Consider using === instead.
Loading history...
27
		if (text.length === 0) {
28
			return text;
29
		}
30
		if (forward) {
31
			var idx = text.indexOf(find, startAt);
32
		}else {
33
			idx = text.lastIndexOf(find, startAt);
34
		}
35
		if (idx == -1) { return returnAll ? text : ''; }
36
		if (inclusive) {
37
			idx += find.length;
38
		}
39
		return text.substr(0, idx);
40
	},
41
	
42
	afterIndex: function(index, inclusive){
43
		var text = this;
44
		if (!inclusive) {
45
			index++;
46
		}
47
		if (index <= 0) {
48
			return text;
49
		}
50
		if (index >= text.length) {
51
			return "";
52
		}
53
		return text.substr(index);
54
	},
55
	beforeIndex: function(index, inclusive){
56
		var text = this;
57
		if (inclusive) {
58
			index++;
59
		}
60
		if (index <= 0) {
61
			return "";
62
		}
63
		if (index >= text.length) {
64
			return text;
65
		}
66
		return text.substr(0, index);
67
	},
68
	
69
	afterFirst: function(find, returnAll = false){
70
		var text = this;
71
		var idx = text.indexOf(find);
72
		if (idx == -1) { return returnAll ? text : ''; }
73
		idx += find.length;
74
		return text.substr(idx);
75
	},
76
	afterNth: function(find, n, returnAll = false){
77
		var text = this;
78
		if (n == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing n to 0 using the == operator is not safe. Consider using === instead.
Loading history...
79
			return text;
80
		}
81
		var i = -2;
82
		var c = 0;
83
		while (i != -1) {
84
			i = text.indexOf(find, i < 0 ? 0 : (i + 1));
85
			c++;
86
			if (i > -1) {
87
				if (c == n) {
88
					return text.substr(i + find.length);
89
				}
90
			}
91
		}
92
		return returnAll ? text : '';
93
	},
94
	afterLast: function(find, returnAll = false){
95
		var text = this;
96
		var idx = text.lastIndexOf(find);
97
		if (idx == -1) { return returnAll ? text : ''; }
98
		idx += find.length;
99
		return text.substr(idx);
100
	},
101
	
102
	beforeFirst: function(find, returnAll = false){
103
		var text = this;
104
		var idx = text.indexOf(find);
105
	        	if (idx == -1) { return returnAll ? text : ''; }
106
	        	return text.substr(0, idx);
107
	},
108
	beforeLast: function(find, returnAll = false){
109
		var text = this;
110
		var idx = text.lastIndexOf(find);
111
	        	if (idx == -1) { return returnAll ? text : ''; }
112
	        	return text.substr(0, idx);
113
	},
114
	between: function(p_start, p_end, startAt = 0, forward = true, returnAll = false){
115
		var text = this;
116
		if (text.length === 0) {
117
			return text;
118
		}
119
		if (forward) {
120
			var startIdx = text.indexOf(p_start, startAt);
121
		}else {
122
			startIdx = text.lastIndexOf(p_start, startAt);
123
		}
124
		if (startIdx != -1) {
125
			startIdx += p_start.length;
126
			var endIdx = text.indexOf(p_end, startIdx);
127
			if (endIdx != -1) {
128
				return text.substr(startIdx, endIdx - startIdx);
129
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
130
				if (returnAll) {
131
					return text;
132
				}
133
			}
134
		}else {
135
			if (returnAll) {
136
				return text;
137
			}
138
		}
139
		return '';
140
	},
141
	betweenLast: function(p_start, p_end){
142
		var text = this;
143
		return text.between(p_start, p_end, text.length, false);
144
	},
145
	betweenAll: function(textStart, textEnd, returnAll = false, returnAllSplitBy = "\n"){
146
		var text = this;
147
	
148
		// return if blank
149
		var betweens = [];
150
		if (textStart.length === 0 || textEnd.length === 0) {
151
			return returnAll ? text.split(returnAllSplitBy) : betweens;
152
		}
153
	
154
		// per occurance of start marker
155
		var start = 0;
156
		while ((start = text.indexOf(textStart, start)) > -1) {
157
			start += textStart.length;
158
	
159
			// find end marker
160
			var end = text.indexOf(textEnd, start + 1);
161
			if (end == -1) {
162
				break;
163
			}
164
	
165
			// add text between start and end marker
166
			betweens.push(text.substr(start, end - start));
167
			start = end + 1;
168
		}
169
		return betweens;
170
	},
171
	
172
173
	none:null
174
};
175
176
// register funcs
177
UB.registerFuncs(String.prototype, stringFuncs);