Passed
Push — master ( 9624f5...20d5c6 )
by Night
58s
created

arrayFuncs.setPropsByArray   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 15
rs 9.85
c 0
b 0
f 0
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
	props: function(prop, defaultVal = null){
66
		var list = this;
67
		var results = [];
68
		var deepProp = prop.isArray() || prop.contains(".");
69
		for (var i = 0; i<list.length; i++){
70
			var obj = list[i];
71
			if (deepProp){
72
				results.push(obj != null ? obj[prop] : defaultVal);
73
			}else{
74
				results.push(obj != null ? obj.getPath(prop) : defaultVal);
75
			}
76
		}
77
		return results;
78
	},
79
	
80
	setProps: function(prop, newValue){
81
		var list = this;
82
		var deepProp = prop.isArray() || prop.contains(".");
83
		for (var i = 0; i<list.length; i++){
84
			var obj = list[i];
85
			if (obj != null){
86
				if (deepProp){
87
					obj[prop] = newValue;
88
				}else{
89
					obj.setPath(prop, newValue);
90
				}
91
			}
92
		}
93
		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...
94
	},
95
	
96
	setPropsByArray: function(prop, newValues){
97
		var list = this;
98
		var deepProp = prop.isArray() || prop.contains(".");
99
		for (var i = 0; i<list.length; i++){
100
			var obj = list[i];
101
			if (obj != null){
102
				if (deepProp){
103
					obj[prop] = newValues[i];
104
				}else{
105
					obj.setPath(prop, newValues[i]);
106
				}
107
			}
108
		}
109
		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...
110
	},
111
	
112
	none:null
113
};
114
115
// register funcs
116
UB.registerFuncs(Array.prototype, arrayFuncs);
117