Completed
Push — master ( 58e7a9...dddd97 )
by Chris
45s
created

jsondash.util.isInDemoMode   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 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
 * [reformatQueryParams Reformat params into a query string.]
39
 * @param  {[type]} old [List of query params]
0 ignored issues
show
Documentation Bug introduced by
The parameter old does not exist. Did you maybe mean oldp instead?
Loading history...
40
 * @param  {[type]} new [List of query params]
0 ignored issues
show
Documentation Bug introduced by
The parameter new does not exist. Did you maybe mean newp instead?
Loading history...
41
 * @return {[type]}     [The new string (e.g. 'foo=bar&baz=1')]
42
        For example:
43
        old: foo=1&baz=1
44
        new: foo=2&quux=1
45
        expected: foo=2&quux=1&baz=1
46
 */
47
jsondash.util.reformatQueryParams = function(oldp, newp) {
48
    var _combined = {};
0 ignored issues
show
Unused Code introduced by
The assignment to variable _combined seems to be never used. Consider removing it.
Loading history...
49
    var combined  = '';
50
    var oldparams = {};
51
    var newparams = {};
52
    $.each(oldp.split('&'), function(i, param){
53
        param = param.split('=');
54
        oldparams[param[0]] = param[1];
55
    });
56
    $.each(newp.split('&'), function(i, param){
57
        param = param.split('=');
58
        newparams[param[0]] = param[1];
59
    });
60
    _combined = $.extend(oldparams, newparams);
61
    $.each(_combined, function(k, v){
62
        if(v !== undefined) {
63
            combined += k + '=' + v + '&';
64
        }
65
    });
66
    // Replace last ampersan if it exists.
67
    if(combined.charAt(combined.length - 1) === '&') {
68
        return combined.substring(0, combined.length - 1);
69
    }
70
    return combined;
71
};
72
73
/**
74
 * [isInDemoMode Check if app is in demo mode.]
75
 */
76
jsondash.util.isInDemoMode = function() {
77
    var parts = window.location.href.split('?');
78
    var matches = parts.filter(function(part, _){
0 ignored issues
show
Unused Code introduced by
The parameter _ 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...
79
        return part === 'jsondash_demo_mode=1' || part === 'jsondash_demo_mode=true';
80
    });
81
    return matches.length > 0;
82
};
83
84
/**
85
 * [intervalStrToMS Convert a string formatted to indicate an interval to milliseconds]
86
 * @param  {[String]} ival_fmt [The interval format string e.g. "1-d", "2-h"]
87
 * @return {[Number]} [The number of milliseconds]
88
 */
89
jsondash.util.intervalStrToMS = function(ival_fmt) {
90
    if(ival_fmt === undefined || ival_fmt === '') {
91
        return null;
92
    }
93
    // Just return number if it's a regular integer.
94
    if(!isNaN(ival_fmt)) {
95
        return ival_fmt;
96
    }
97
    var pieces = ival_fmt.split('-');
98
    var amt = parseInt(pieces[0], 10);
99
    if(pieces.length !== 2 || isNaN(amt) || amt === 0) {
100
        // Force NO value if the format is invalid.
101
        // This would be used to ensure the interval
102
        // is not set in the first place.
103
        return null;
104
    }
105
    var ival = pieces[1].toLowerCase();
106
    var ms2s = 1000;
107
    var ms2min = 60 * ms2s;
108
    var ms2hr = 60 * ms2min;
109
    var ms2day = 24 * ms2hr;
110
111
    // Seconds
112
    if(ival === 's') {
113
        return amt * ms2s;
114
    }
115
    // Minutes
116
    if(ival === 'm') {
117
        return amt * ms2min;
118
    }
119
    // Hours
120
    if(ival === 'h') {
121
        return amt * ms2hr;
122
    }
123
    // Days
124
    if(ival === 'd') {
125
        return amt * ms2day;
126
    }
127
    // Anything else is invalid.
128
    return null;
129
};
130
131
jsondash.util.serializeToJSON = function(arr) {
132
    // Convert form data to a proper json value
133
    var json = {};
134
    $.each(arr, function(_, pair){
135
        json[pair.name] = pair.value;
136
    });
137
    return json;
138
};
139
140
jsondash.util.isOverride = function(config) {
141
    return config.override && config.override === true;
142
};
143
144
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
145
jsondash.util.s4 = function() {
146
    return Math.floor((1 + Math.random()) * 0x10000)
147
    .toString(16)
148
    .substring(1);
149
};
150
151
jsondash.util.guid = function() {
152
    var s4 = jsondash.util.s4;
153
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
154
    s4() + '-' + s4() + s4() + s4();
155
};
156
157
jsondash.util.polygon = function(d) {
158
    return "M" + d.join("L") + "Z";
159
};
160
161
jsondash.util.scaleStr = function(x, y) {
162
    return 'scale(' + x + ',' + y + ')';
163
};
164
165
jsondash.util.translateStr = function(x, y) {
166
    return 'translate(' + x + ',' + y + ')';
167
};
168
169
/**
170
 * [getDigitSize return a d3 scale for adjusting font size based
171
 *     on digits and width of container.]
172
 */
173
jsondash.util.getDigitSize = function() {
174
    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...
175
    // scale value is reversed, since we want
176
    // the font-size to get smaller as the number gets longer.
177
    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...
178
        .clamp(true)
179
        .domain([2, 14]) // min/max digits length: $0 - $999,999,999.00
180
        .range([90, 30]); // max/min font-size
181
    window.scale = scale;
182
    return scale;
183
};
184
185
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
186
    module.exports = jsondash;
187
}
188