Passed
Push — master ( 668c29...80bfec )
by Night
01:00
created

arrayFuncs.lastWhere   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
/** global: UB */
2
/** global: Buffer */
3
4
/*! ARRAY UTILS */
5
6
var arrayFuncs = {
7
8
	merge: function(){
9
		var arr = this;
10
		var val1 = arr[0];
11
12
		// if array of strings
13
		if (val1.isString()){
14
			return val1.join("");
15
		}
16
17
		// if array of arrays
18
		if (val1.isArray()){
19
			var merged = [];
20
			for (var a = 0, al = arr.length; a<al; a++){
21
				merged.addArray(arr[a]);
22
			}
23
			return merged;
24
		}
25
26
		// if array of Buffers
27
		//removeIf(nodejs)
28
		if (val1 instanceof Buffer){
29
30
			// calc the total length of all buffers
31
			var buffers = arr;
32
			var len = 0;
33
			for(var b = 0, bl = buffers.length; b < bl; b++) {
34
				len += buffers[b].length;
35
			}
36
37
			// create a new buffer of that length
38
			var mega = new Buffer(len);
39
40
			// write all buffers into the mega buffer
41
			var cur = 0;
42
			for(var b = 0, bl = buffers.length; b < bl; b++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable bl already seems to be declared on line 33. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Comprehensibility Naming Best Practice introduced by
The variable b already seems to be declared on line 33. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
43
				buffers[b].copy(mega, cur, 0);
44
				cur += buffers[b].length;
45
			}
46
			return mega;
47
		}
48
		//endRemoveIf(nodejs)
49
50
		return null;
51
	},
52
	
53
	where: function(callback){
54
		var list = this;
55
		var results = [];
56
		for (var i = 0; i<list.length; i++){
57
			var obj = list[i];
58
			if (callback(obj) == true){
59
				results.push(obj);
60
			}
61
		}
62
		return results;
63
	},
64
	
65
	hasWhere: function(callback){
66
		var list = this;
67
		for (var i = 0; i<list.length; i++){
68
			var obj = list[i];
69
			if (callback(obj) == true){
70
				return true;
71
			}
72
		}
73
		return false;
74
	},
75
	
76
	firstWhere: function(callback){
77
		var list = this;
78
		for (var i = 0; i<list.length; i++){
79
			var obj = list[i];
80
			if (callback(obj) == true){
81
				return obj;
82
			}
83
		}
84
		return null;
85
	},
86
	
87
	lastWhere: function(callback){
88
		var list = this;
89
		for (var i = list.length; i>=0; i--){
90
			var obj = list[i];
91
			if (callback(obj) == true){
92
				return obj;
93
			}
94
		}
95
		return null;
96
	},
97
	
98
	props: function(prop, defaultVal = null){
99
		var list = this;
100
		var results = [];
101
		var deepProp = prop.isArray() || prop.contains(".");
102
		for (var i = 0; i<list.length; i++){
103
			var obj = list[i];
104
			if (deepProp){
105
				results.push(obj != null ? obj[prop] : defaultVal);
106
			}else{
107
				results.push(obj != null ? obj.getPath(prop) : defaultVal);
108
			}
109
		}
110
		return results;
111
	},
112
	
113
	setProps: function(prop, newValue){
114
		var list = this;
115
		var deepProp = prop.isArray() || prop.contains(".");
116
		for (var i = 0; i<list.length; i++){
117
			var obj = list[i];
118
			if (obj != null){
119
				if (deepProp){
120
					obj[prop] = newValue;
121
				}else{
122
					obj.setPath(prop, newValue);
123
				}
124
			}
125
		}
126
		return results;
0 ignored issues
show
Bug introduced by
The variable results seems to be never declared. If this is a global, consider adding a /** global: results */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
127
	},
128
	
129
	setPropsByArray: function(prop, newValues){
130
		var list = this;
131
		var deepProp = prop.isArray() || prop.contains(".");
132
		for (var i = 0; i<list.length; i++){
133
			var obj = list[i];
134
			if (obj != null){
135
				if (deepProp){
136
					obj[prop] = newValues[i];
137
				}else{
138
					obj.setPath(prop, newValues[i]);
139
				}
140
			}
141
		}
142
		return results;
0 ignored issues
show
Bug introduced by
The variable results seems to be never declared. If this is a global, consider adding a /** global: results */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
143
	},
144
	
145
	none:null
146
};
147
148
// register funcs
149
UB.registerFuncs(Array.prototype, arrayFuncs);
150