Completed
Push — master ( a5a3aa...a855fe )
by Chris
01:15
created

jsondash.util.intervalStrToMS   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
c 0
b 0
f 0
dl 0
loc 38
rs 4.909
nc 7
nop 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.getValidParamString = function(arr) {
8
    // Jquery $.serialize and $.serializeArray will
9
    // return empty query parameters, which is undesirable and can
10
    // be error prone for RESTFUL endpoints.
11
    // e.g. `foo=bar&bar=` becomes `foo=bar`
12
    var param_str = '';
13
    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...
14
    $.each(arr, function(i, param){
15
        param_str += (param.name + '=' + param.value);
16
        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...
17
    });
18
    return param_str;
19
};
20
21
/**
22
 * [intervalStrToMS Convert a string formatted to indicate]
23
 * @param  {[String]} ival_fmt [The interval format string e.g. "1d", "2h"]
24
 * @return {[Number]} [The number of milliseconds]
25
 */
26
jsondash.util.intervalStrToMS = function(ival_fmt) {
27
    // Just return number if it's a regular integer.
28
    if(!isNaN(ival_fmt)) {
29
        return ival_fmt;
30
    }
31
    var pieces = ival_fmt.split('-');
32
    var amt = parseInt(pieces[0], 10);
33
    if(pieces.length !== 2 || isNaN(amt) || amt === 0) {
34
        // Force NO value if the format is invalid.
35
        // This would be used to ensure the interval
36
        // is not set in the first place.
37
        return null;
38
    }
39
    var ival = pieces[1].toLowerCase();
40
    var ms2s = 1000;
41
    var ms2min = 60 * ms2s;
42
    var ms2hr = 60 * ms2min;
43
    var ms2day = 24 * ms2hr;
44
45
    // Seconds
46
    if(ival === 's') {
47
        return amt * ms2s;
48
    }
49
    // Minutes
50
    if(ival === 'm') {
51
        return amt * ms2min;
52
    }
53
    // Hours
54
    if(ival === 'h') {
55
        return amt * ms2hr;
56
    }
57
    // Days
58
    if(ival === 'd') {
59
        return amt * ms2day;
60
    }
61
    // Anything else is invalid.
62
    return null;
63
};
64
65
jsondash.util.serializeToJSON = function(arr) {
66
    // Convert form data to a proper json value
67
    var json = {};
68
    $.each(arr, function(_, pair){
69
        json[pair.name] = pair.value;
70
    });
71
    return json;
72
};
73
74
jsondash.util.isOverride = function(config) {
75
    return config.override && config.override === true;
76
};
77
78
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
79
jsondash.util.s4 = function() {
80
    return Math.floor((1 + Math.random()) * 0x10000)
81
    .toString(16)
82
    .substring(1);
83
};
84
85
jsondash.util.guid = function() {
86
    var s4 = jsondash.util.s4;
87
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
88
    s4() + '-' + s4() + s4() + s4();
89
};
90
91
jsondash.util.polygon = function(d) {
92
    return "M" + d.join("L") + "Z";
93
};
94
95
jsondash.util.scaleStr = function(x, y) {
96
    return 'scale(' + x + ',' + y + ')';
97
};
98
99
jsondash.util.translateStr = function(x, y) {
100
    return 'translate(' + x + ',' + y + ')';
101
};
102
103
jsondash.util.isD3Subtype = function(config) {
104
    // Handle specific D3 types that aren't necessarily referenced under
105
    // the D3 namespace in a select field.
106
    if(config.type === 'dendrogram') return true;
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...
107
    if(config.type === 'voronoi') return true;
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...
108
    if(config.type === 'circlepack') return true;
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...
109
    if(config.type === 'treemap') return true;
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...
110
    if(config.type === 'radial-dendrogram') return true;
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...
111
    return false;
112
};
113
114
jsondash.util.isSparkline = function(type) {
115
    return type.substr(0, 10) === 'sparklines';
116
};
117
118
/**
119
 * [getDigitSize return a d3 scale for adjusting font size based
120
 *     on digits and width of container.]
121
 */
122
jsondash.util.getDigitSize = function() {
123
    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...
124
    // scale value is reversed, since we want
125
    // the font-size to get smaller as the number gets longer.
126
    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...
127
        .clamp(true)
128
        .domain([2, 14]) // min/max digits length: $0 - $999,999,999.00
129
        .range([90, 30]); // max/min font-size
130
    window.scale = scale;
131
    return scale;
132
};
133
134
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
135
    module.exports = jsondash;
136
}
137