Completed
Push — master ( 973247...b98c45 )
by Chris
01:18
created

flask_jsondash/static/js/utils.js   A

Complexity

Total Complexity 31
Complexity/F 1.94

Size

Lines of Code 133
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
c 3
b 0
f 0
nc 96
dl 0
loc 133
rs 9.8
wmc 31
mnd 1
bc 25
fnc 16
bpm 1.5625
cpm 1.9375
noi 6

11 Functions

Rating   Name   Duplication   Size   Complexity  
D jsondash.util.intervalStrToMS 0 38 9
A jsondash.util.serializeToJSON 0 8 1
A jsondash.util.getValidParamString 0 13 1
A jsondash.util.getCSSClasses 0 15 4
A jsondash.util.isOverride 0 3 1
A jsondash.util.polygon 0 3 1
A jsondash.util.scaleStr 0 3 1
A jsondash.util.s4 0 5 1
A jsondash.util.translateStr 0 3 1
A jsondash.util.guid 0 5 1
A jsondash.util.getDigitSize 0 11 1
1
/**
2
 * Utility functions.
3
 */
4
5
jsondash = jsondash || {util: {}};
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable jsondash is declared in the current environment, consider using typeof jsondash === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Bug introduced by
The variable jsondash seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.jsondash.
Loading history...
6
7
jsondash.util.getCSSClasses = function(conf, defaults) {
8
    var classes = {};
9
    if(conf.classes === undefined && defaults !== undefined) {
10
        $.each(defaults, function(i, klass){
11
            classes[klass] = true;
12
        });
13
        return classes;
14
    }
15
    if(conf.classes !== undefined) {
16
        $.each(conf.classes, function(i, klass){
17
            classes[klass] = true;
18
        });
19
    }
20
    return classes;
21
};
22
23
jsondash.util.getValidParamString = function(arr) {
24
    // Jquery $.serialize and $.serializeArray will
25
    // return empty query parameters, which is undesirable and can
26
    // be error prone for RESTFUL endpoints.
27
    // e.g. `foo=bar&bar=` becomes `foo=bar`
28
    var param_str = '';
29
    arr = arr.filter(function(param, i){return param.value !== '';});
0 ignored issues
show
Unused Code introduced by
The parameter i is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
30
    $.each(arr, function(i, param){
31
        param_str += (param.name + '=' + param.value);
32
        if(i < arr.length - 1 && arr.length > 1) param_str += '&';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
33
    });
34
    return param_str;
35
};
36
37
/**
38
 * [intervalStrToMS Convert a string formatted to indicate]
39
 * @param  {[String]} ival_fmt [The interval format string e.g. "1d", "2h"]
40
 * @return {[Number]} [The number of milliseconds]
41
 */
42
jsondash.util.intervalStrToMS = function(ival_fmt) {
43
    // Just return number if it's a regular integer.
44
    if(!isNaN(ival_fmt)) {
45
        return ival_fmt;
46
    }
47
    var pieces = ival_fmt.split('-');
48
    var amt = parseInt(pieces[0], 10);
49
    if(pieces.length !== 2 || isNaN(amt) || amt === 0) {
50
        // Force NO value if the format is invalid.
51
        // This would be used to ensure the interval
52
        // is not set in the first place.
53
        return null;
54
    }
55
    var ival = pieces[1].toLowerCase();
56
    var ms2s = 1000;
57
    var ms2min = 60 * ms2s;
58
    var ms2hr = 60 * ms2min;
59
    var ms2day = 24 * ms2hr;
60
61
    // Seconds
62
    if(ival === 's') {
63
        return amt * ms2s;
64
    }
65
    // Minutes
66
    if(ival === 'm') {
67
        return amt * ms2min;
68
    }
69
    // Hours
70
    if(ival === 'h') {
71
        return amt * ms2hr;
72
    }
73
    // Days
74
    if(ival === 'd') {
75
        return amt * ms2day;
76
    }
77
    // Anything else is invalid.
78
    return null;
79
};
80
81
jsondash.util.serializeToJSON = function(arr) {
82
    // Convert form data to a proper json value
83
    var json = {};
84
    $.each(arr, function(_, pair){
85
        json[pair.name] = pair.value;
86
    });
87
    return json;
88
};
89
90
jsondash.util.isOverride = function(config) {
91
    return config.override && config.override === true;
92
};
93
94
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
95
jsondash.util.s4 = function() {
96
    return Math.floor((1 + Math.random()) * 0x10000)
97
    .toString(16)
98
    .substring(1);
99
};
100
101
jsondash.util.guid = function() {
102
    var s4 = jsondash.util.s4;
103
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
104
    s4() + '-' + s4() + s4() + s4();
105
};
106
107
jsondash.util.polygon = function(d) {
108
    return "M" + d.join("L") + "Z";
109
};
110
111
jsondash.util.scaleStr = function(x, y) {
112
    return 'scale(' + x + ',' + y + ')';
113
};
114
115
jsondash.util.translateStr = function(x, y) {
116
    return 'translate(' + x + ',' + y + ')';
117
};
118
119
/**
120
 * [getDigitSize return a d3 scale for adjusting font size based
121
 *     on digits and width of container.]
122
 */
123
jsondash.util.getDigitSize = function() {
124
    var BOX_PADDING = 20;
0 ignored issues
show
Unused Code introduced by
The variable BOX_PADDING seems to be never used. Consider removing it.
Loading history...
125
    // scale value is reversed, since we want
126
    // the font-size to get smaller as the number gets longer.
127
    var scale = d3.scale.linear()
0 ignored issues
show
Bug introduced by
The variable d3 seems to be never declared. If this is a global, consider adding a /** global: d3 */ 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...
128
        .clamp(true)
129
        .domain([2, 14]) // min/max digits length: $0 - $999,999,999.00
130
        .range([90, 30]); // max/min font-size
131
    window.scale = scale;
132
    return scale;
133
};
134
135
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
136
    module.exports = jsondash;
137
}
138