src/ub.strings.words.js   A
last analyzed

Complexity

Total Complexity 20
Complexity/F 6.67

Size

Lines of Code 102
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 5 Features 0
Metric Value
cc 0
eloc 48
c 8
b 5
f 0
nc 1
dl 0
loc 102
rs 10
wmc 20
mnd 8
bc 16
fnc 3
bpm 5.3333
cpm 6.6666
noi 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A arrayFuncs.joinWords 0 4 1
A stringFuncs.wordCount 0 4 1
F stringFuncs.splitWords 0 67 18
1
/** global: UB */
2
3
UB.wordSeperators = [" ", ".", ",", ";", "[", "]", "{", "}", "(", ")"];
4
5
6
7
var arrayFuncs = {
8
9
	joinWords: function(){
10
		var words = this;
11
		return words.join(" ");
12
	},
13
14
none:null
15
};
16
17
// register funcs
18
UB.registerFuncs(Array.prototype, arrayFuncs);
19
20
21
22
var stringFuncs = {
23
	
24
	/** advanced: splits by any operator or number char, space & newline.
25
	 * simple: splits by newline & space.
26
	 * always returns an array of non-blank, trimmed words. */
27
	splitWords: function(advanced = true){
28
		var text = this;
29
30
		if (advanced){
31
			
32
			var results = [];
33
			
34
			// find start of word
35
			for (var c = 0, cl = text.length;c<cl;c++){
36
				var cc = text.charAt(c);
37
				var start = c;
38
				
39
				if (cc.isLetter()) {
40
					
41
					// find end of word
42
					for (var e = c + 1;e<cl;e++){
43
						var ec = text.charAt(e);
44
						var isLastChar = (e == (cl - 1));
45
						
46
						if ((!ec.isLetter() && ec != ' ') || isLastChar){
47
							
48
							// skip if "letter [dash] letter"
49
							if (!isLastChar && ec == '-' && text.charAt(e + 1).isLetter()){
50
								continue;
51
							}
52
							
53
							// add word if something found
54
							var end = isLastChar?e:e-1;
55
							if (end > start) {
56
								
57
								var term = text.range(start, end).trim();
58
								if (term.exists()){
59
									
60
									if (splitBySpaceAlso) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable splitBySpaceAlso is declared in the current environment, consider using typeof splitBySpaceAlso === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
61
										term.splitWords().addArray(results);
62
									} else {
63
										results.push(term);
64
									}
65
								}
66
							}
67
							
68
							// continue at ending
69
							c = e;
70
							break;
71
						}
72
					}
73
				}
74
			}
75
			return results;
76
77
		}
78
		
79
		// SPLIT BY NEWLINE & SPACES
80
		results = [];
81
		var lines = text.splitLines(true, true);
82
		for (var s = 0, sl = lines.length; s < sl; s++) {
83
			var line = lines[s];
84
			results = line.split(" ");
85
			for (var w = 0, wl = results.length; w < wl; w++) {
86
				var word = results[w];
87
				if (word.length > 0) {
88
					results.push(word);
89
				}
90
			}
91
		}
92
		return results;
93
	},
94
	
95
	/** Count words in a string */
96
	wordCount: function(){
97
		var text = this;
98
		return text.match(new RegExp("\\b\\w+\\b", "g")).length;
99
	},
100
	none:null
101
};
102
103
// register funcs
104
UB.registerFuncs(String.prototype, stringFuncs);