src/backgrid_modules/formatters/percent.js   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 2.67

Size

Lines of Code 80
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 8
dl 0
loc 80
rs 10
wmc 8
mnd 2
bc 4
fnc 3
bpm 1.3333
cpm 2.6666
noi 14

3 Functions

Rating   Name   Duplication   Size   Complexity  
B _.extend.toRaw 0 8 6
A _.extend.fromRaw 0 5 1
A percent.js ➔ PercentFormatter 0 3 1
1
import _ from 'underscore';
0 ignored issues
show
introduced by
Definition for rule 'keyword-spacing' was not found
Loading history...
2
import {
3
  CellFormatter
4
} from './cell.js';
5
import {
6
  NumberFormatter
7
} from './number.js';
8
9
/**
10
   A number formatter that converts a floating point number, optionally
11
   multiplied by a multiplier, to a percentage string and vice versa.
12
13
   @class Backgrid.PercentFormatter
14
   @extends Backgrid.NumberFormatter
15
   @constructor
16
   @throws {RangeError} If decimals < 0 or > 20.
17
 */
18
var PercentFormatter = function () {
19
  Backgrid.NumberFormatter.apply(this, arguments);
0 ignored issues
show
Bug introduced by
The variable Backgrid seems to be never declared. If this is a global, consider adding a /** global: Backgrid */ 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...
20
};
21
22
PercentFormatter.prototype = new NumberFormatter(),
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
introduced by
Expected an assignment or function call and instead saw an expression.
Loading history...
introduced by
Unexpected use of comma operator.
Loading history...
23
24
  _.extend(PercentFormatter.prototype, {
25
26
    /**
27
       @member Backgrid.PercentFormatter
28
       @cfg {Object} options
29
30
       @cfg {number} [options.multiplier=1] The number used to multiply the model
31
       value for display.
32
33
       @cfg {string} [options.symbol='%'] The symbol to append to the percentage
34
       string.
35
     */
36
    defaults: _.extend({}, NumberFormatter.prototype.defaults, {
37
      multiplier: 1,
38
      symbol: "%"
39
    }),
40
41
    /**
42
       Takes a floating point number, where the number is first multiplied by
43
       `multiplier`, then converted to a formatted string like
44
       NumberFormatter#fromRaw, then finally append `symbol` to the end.
45
46
       @member Backgrid.PercentFormatter
47
       @param {number} rawValue
0 ignored issues
show
Documentation introduced by
The parameter rawValue does not exist. Did you maybe forget to remove this comment?
Loading history...
48
       @param {Backbone.Model} model Used for more complicated formatting
49
       @return {string}
50
    */
51
    fromRaw: function (number, model) {
0 ignored issues
show
Unused Code introduced by
The parameter model 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...
52
      var args = [].slice.call(arguments, 1);
53
      args.unshift(number * this.multiplier);
54
      return (NumberFormatter.prototype.fromRaw.apply(this, args) || "0") + this.symbol;
55
    },
56
57
    /**
58
       Takes a string, possibly appended with `symbol` and/or `decimalSeparator`,
59
       and convert it back to a number for the model like NumberFormatter#toRaw,
60
       and then dividing it by `multiplier`.
61
62
       @member Backgrid.PercentFormatter
63
       @param {string} formattedData
0 ignored issues
show
Documentation Bug introduced by
The parameter formattedData does not exist. Did you maybe mean formattedValue instead?
Loading history...
64
       @param {Backbone.Model} model Used for more complicated formatting
65
       @return {number|undefined} Undefined if the string cannot be converted to
66
       a number.
67
    */
68
    toRaw: function (formattedValue, model) {
0 ignored issues
show
Unused Code introduced by
The parameter model 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...
69
      var tokens = formattedValue.split(this.symbol);
70
      if (tokens && tokens[0] && tokens[1] === "" || tokens[1] == null) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if tokens && tokens.0 && to... "" || tokens.1 == null is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
introduced by
Use ‘===’ to compare with ‘null’.
Loading history...
Bug Best Practice introduced by
Apart from some edge-cases, it is generally advisable to use the strict comparison === instead of ==.

The loose comparison such as == or != might produce some weird results for some values, unless you explicitly want to have this behavior here, better use the strict alternative.

Learn more about loose comparison in Javascript.

Loading history...
71
        var rawValue = NumberFormatter.prototype.toRaw.call(this, tokens[0]);
72
        if (_.isUndefined(rawValue)) return rawValue;
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...
introduced by
Expected { after 'if' condition.
Loading history...
73
        return rawValue / this.multiplier;
74
      }
75
    }
76
77
  });
78
export {
79
  PercentFormatter
80
};
81