src/helpers.js   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 2.13

Size

Lines of Code 86
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
eloc 61
nc 1
dl 0
loc 86
rs 10
c 2
b 0
f 0
wmc 17
mnd 2
bc 16
fnc 8
bpm 2
cpm 2.125
noi 1

5 Functions

Rating   Name   Duplication   Size   Complexity  
A helpers.js ➔ compact 0 14 4
A helpers.js ➔ parseHalf 0 3 1
A helpers.js ➔ rgbaString 0 16 2
A helpers.js ➔ omit 0 13 4
A helpers.js ➔ serializeOptions 0 19 2
1
export function hslaString(hslcolor) {
2
	if (hslcolor.a !== undefined) {
3
		return (
4
			"hsla(" +
5
			hslcolor.h +
6
			"," +
7
			hslcolor.s +
8
			"%," +
9
			hslcolor.l +
10
			"%," +
11
			parseFloat(hslcolor.a, 10) +
12
			")"
13
		);
14
	}
15
	return "hsl(" + hslcolor.h + "," + hslcolor.s + "%," + hslcolor.l + "%)";
16
}
17
18
export function rgbaString(hexcolor) {
19
	if (hexcolor.a !== undefined) {
20
		return (
21
			"rgba(" +
22
			hexcolor.r +
23
			"," +
24
			hexcolor.g +
25
			"," +
26
			hexcolor.b +
27
			"," +
28
			parseFloat(hexcolor.a, 10) +
29
			")"
30
		);
31
	}
32
	return "rgb(" + hexcolor.r + "," + hexcolor.g + "," + hexcolor.b + ")";
33
}
34
35
export function parseHalf(foo) {
36
	return parseInt(foo / 2, 10);
37
}
38
39
export function compact(array) {
40
	let index = -1,
41
		length = array ? array.length : 0,
42
		resIndex = 0,
43
		result = [];
44
45
	while (++index < length) {
46
		let value = array[index];
47
		if (value) {
48
			result[resIndex++] = value;
49
		}
50
	}
51
	return result;
52
}
53
54
export function omit(obj, fn) {
55
	var target = {};
56
	for (var i in obj) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
57
		if (fn(i)) {
58
			continue;
59
		}
60
		if (!Object.prototype.hasOwnProperty.call(obj, i)) {
61
			continue;
62
		}
63
		target[i] = obj[i];
64
	}
65
	return target;
66
}
67
68
export function serializeOptions(options) {
69
	if (typeof options !== "object") {
70
		return null;
71
	}
72
	var cleanOptions = omit(options, function(prop) {
73
			return prop.indexOf("gm_") === 0;
74
		}),
75
		sortedOpts = Object.entries(cleanOptions)
76
			.filter(function(item) {
77
				return (
78
					typeof item[1] !== "function" &&
79
					typeof item[1] !== "object" &&
80
					item[1] !== null &&
81
					typeof item[1] !== "undefined"
82
				);
83
			})
84
			.sort();
85
	return JSON.stringify(sortedOpts);
86
}
87