Issues (81)

src/ub.strings.charrange.js (4 issues)

1
/** global: UB */
2
3
var stringFuncs = {
4
	
5
	// char type
6
	isOfCharRange: function(type){
7
		var char = this;
8
		
9
		// needs to be exactly 1 char!
10
		if (char.length != 1) {
11
			return false;
12
		}
13
		
14
		// check type
15
		if (type == "digit") {
16
			var code = char.charCodeAt(0);
17
			return code >= 48 && code <= 57;
18
		}
19
		if (type == "letter") {
20
			code = char.charCodeAt(0);
21
			if (code >= 65 && code <= 90){
22
				return true;
23
			}
24
			if (code >= 97 && code <= 122){
25
				return true;
26
			}
27
		}
28
		if (type == "space") {
29
			return char == " " || char == "\t" || char == "\r" || char == "\n";
30
		}
31
		if (type == "symbol") {
32
			return UB.commonSymbols.indexOf(char) > -1;
33
		}
34
		
35
		return false;
36
	},
37
	isNoneOfCharRange: function(type){
38
		var str = this;
39
		return !str.containsCharRange(type);
40
	},
41
	isAllOfCharRange: function(type, ifNoChars = false){
42
		var str = this;
43
		
44
		// exit quickly if blank string
45
		if (str == null || str.length === 0) {
46
			return ifNoChars;
47
		}
48
		
49
		// find char of type
50
		for (var c = 0, cl = str.length;c<cl;c++){
51
			if (!str.charAt(c).isOfCharRange(type)) {
52
				return false;
53
			}
54
		}
55
		return true;
56
	},
57 View Code Duplication
	isAllOfCharRange2: function(type1, type2, ifNoChars = false){
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
58
		var str = this;
59
		
60
		// exit quickly if blank string
61
		if (str == null || str.length === 0) {
62
			return ifNoChars;
63
		}
64
		
65
		// find char of type
66
		for (var c = 0, cl = str.length;c<cl;c++){
67
			if (!(str.charAt(c).isOfCharRange(type1) || str.charAt(c).isOfCharRange(type2))) {
68
				return false;
69
			}
70
		}
71
		return true;
72
	},
73
	containsCharRange: function(type, startAt = 0, ifNoChars = false){
74
		var str = this;
75
		
76
		// exit quickly if blank string
77
		if (str == null || str.length === 0) {
78
			return ifNoChars;
79
		}
80
		
81
		// find char of type
82
		for (var c = startAt, cl = str.length;c<cl;c++){
83
			if (str.charAt(c).isOfCharRange(type)) {
84
				return true;
85
			}
86
		}
87
		return false;
88
	},
89
	startsWithCharRange: function(type, ifNoChars = false){
90
		var str = this;
91
		
92
		// exit quickly if blank string
93
		if (str == null || str.length === 0) {
94
			return ifNoChars;
95
		}
96
		
97
		// check if begins with char of type
98
		return str.charAt(0).isOfCharRange(type);
99
	},
100
	endsWithCharRange: function(type, ifNoChars = false){
101
		var str = this;
102
		
103
		// exit quickly if blank string
104
		if (str == null || str.length === 0) {
105
			return ifNoChars;
106
		}
107
		
108
		// check if ends with char of type
109
		return str.charAt(str.length - 1).isOfCharRange(type);
110
	},
111 View Code Duplication
	indexOfCharRange: function(type, startAt = null, forwards = true){
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
112
		var str = this;
113
		
114
		// exit quickly if blank string
115
		if (str == null || str.length === 0) {
116
			return -1;
117
		}
118
		
119
		// find char of type
120
		if (forwards) {
121
			if (startAt == null) {
122
				startAt = 0;
123
			}
124
			for (var c = startAt, cl = str.length;c<cl;c++){
125
				if (str.charAt(c).isOfCharRange(type)) {
126
					return c;
127
				}
128
			}
129
		}else {
130
			if (startAt == null) {
131
				startAt = str.length - 1;
132
			}
133
			for (var c = startAt;c >= 0;c--){
134
				if (str.charAt(c).isOfCharRange(type)) {
135
					return c;
136
				}
137
			}
138
		}
139
		return -1;
140
	},
141
	lastIndexOfCharRange: function(type){
142
		var str = this;
143
		return str.indexOfCharRange(type, null, false);
144
	},
145
	restrictToCharRange: function(type, startAt = 0){
146
		var str = this;
147
		
148
		// exit quickly if blank string
149
		if (str == null || str.length === 0) {
150
			return "";
151
		}
152
		
153
		// extract all chars of given type
154
		var output = [];
155
		for (var c = startAt, cl = str.length;c<cl;c++){
156
			var char = str.charAt(c);
157
			if (char.isOfCharRange(type)) {
158
				output.push(char);
159
			}
160
		}
161
		return output.join("");
162
	},
163 View Code Duplication
	afterCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = true){
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
164
		var text = this;
165
		if (text == null) { return returnAll ? text : ''; }
166
		if (!forward) {
167
			var idx = text.lastIndexOfCharRange(type);
168
		}else {
169
			idx = text.indexOfCharRange(type, startAt);
170
		}
171
		if (idx === -1) { return returnAll ? text : ''; }
172
		if (!inclusive) {
173
			idx++;
174
		}
175
		return text.substr(idx);
176
	},
177 View Code Duplication
	beforeCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = true){
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
178
		var text = this;
179
		if (text == null) { return returnAll ? text : ''; }
180
		if (forward) {
181
			var idx = text.indexOfCharRange(type, startAt);
182
		}else {
183
			idx = text.lastIndexOfCharRange(type);
184
		}
185
		if (idx === -1) { return returnAll ? text : ''; }
186
		if (!inclusive) {
187
			idx--;
188
		}
189
		return text.substr(0, idx);
190
	},
191
	
192
	// digits
193
	removeDigits: function(){
194
		var str = this;
195
		
196
		// exit quickly if blank string
197
		if (str == null || str.length === 0) {
198
			return "";
199
		}
200
		
201
		// extract all chars that are not digits
202
		var output = [];
203
		for (var c = 0, cl = str.length;c<cl;c++){
204
			var char = str.charAt(c);
205
			if (!char.isDigit()) {
206
				output.push(char);
207
			}
208
		}
209
		return output.join("");
210
	},
211
	
212
	none:null
213
};
214
215
// register funcs
216
UB.registerFuncs(String.prototype, stringFuncs);