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

Complexity

Total Complexity 14
Complexity/F 4.67

Size

Lines of Code 106
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 16
dl 0
loc 106
rs 10
wmc 14
mnd 1
bc 7
fnc 3
bpm 2.3333
cpm 4.6666
noi 14

3 Functions

Rating   Name   Duplication   Size   Complexity  
A _.extend.fromRaw 0 11 4
A number.js ➔ NumberFormatter 0 7 3
C _.extend.toRaw 0 25 7
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
/**
6
   A floating point number formatter. Doesn't understand scientific notation at
7
   the moment.
8
9
   @class Backgrid.NumberFormatter
10
   @extends Backgrid.CellFormatter
11
   @constructor
12
   @throws {RangeError} If decimals < 0 or > 20.
13
*/
14
var NumberFormatter = function (options) {
15
  _.extend(this, this.defaults, options || {});
16
17
  if (this.decimals < 0 || this.decimals > 20) {
18
    throw new RangeError("decimals must be between 0 and 20");
19
  }
20
};
21
NumberFormatter.prototype = new CellFormatter();
22
_.extend(NumberFormatter.prototype, {
23
24
  /**
25
     @member Backgrid.NumberFormatter
26
     @cfg {Object} options
27
28
     @cfg {number} [options.decimals=2] Number of decimals to display. Must be an integer.
29
30
     @cfg {string} [options.decimalSeparator='.'] The separator to use when
31
     displaying decimals.
32
33
     @cfg {string} [options.orderSeparator=','] The separator to use to
34
     separator thousands. May be an empty string.
35
   */
36
  defaults: {
37
    decimals: 2,
38
    decimalSeparator: '.',
39
    orderSeparator: ','
40
  },
41
42
  HUMANIZED_NUM_RE: /(\d)(?=(?:\d{3})+$)/g,
43
44
  /**
45
     Takes a floating point number and convert it to a formatted string where
46
     every thousand is separated by `orderSeparator`, with a `decimal` number of
47
     decimals separated by `decimalSeparator`. The number returned is rounded
48
     the usual way.
49
50
     @member Backgrid.NumberFormatter
51
     @param {number} number
52
     @param {Backbone.Model} model Used for more complicated formatting
53
     @return {string}
54
  */
55
  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...
56
    if (_.isNull(number) || _.isUndefined(number)) return '';
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...
57
58
    number = parseFloat(number).toFixed(~~this.decimals);
0 ignored issues
show
introduced by
Unexpected use of '~'.
Loading history...
59
60
    var parts = number.split('.');
61
    var integerPart = parts[0];
62
    var decimalPart = parts[1] ? (this.decimalSeparator || '.') + parts[1] : '';
63
64
    return integerPart.replace(this.HUMANIZED_NUM_RE, '$1' + this.orderSeparator) + decimalPart;
65
  },
66
67
  /**
68
     Takes a string, possibly formatted with `orderSeparator` and/or
69
     `decimalSeparator`, and convert it back to a number.
70
71
     @member Backgrid.NumberFormatter
72
     @param {string} formattedData
73
     @param {Backbone.Model} model Used for more complicated formatting
74
     @return {number|undefined} Undefined if the string cannot be converted to
75
     a number.
76
  */
77
  toRaw: function (formattedData, 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...
78
    formattedData = formattedData.trim();
79
80
    if (formattedData === '') return null;
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...
81
82
    var rawData = '';
83
84
    var thousands = formattedData.split(this.orderSeparator);
85
    for (var i = 0; i < thousands.length; i++) {
86
      rawData += thousands[i];
87
    }
88
89
    var decimalParts = rawData.split(this.decimalSeparator);
90
    rawData = '';
91
    for (var i = 0; i < decimalParts.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 85. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
introduced by
"i" is already defined
Loading history...
92
      rawData = rawData + decimalParts[i] + '.';
93
    }
94
95
    if (rawData[rawData.length - 1] === '.') {
96
      rawData = rawData.slice(0, rawData.length - 1);
97
    }
98
99
    var result = (rawData * 1).toFixed(~~this.decimals) * 1;
0 ignored issues
show
introduced by
Unexpected use of '~'.
Loading history...
100
    if (_.isNumber(result) && !_.isNaN(result)) return result;
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...
Complexity Best Practice introduced by
There is no return statement if _.isNumber(result) && !_.isNaN(result) 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
Expected { after 'if' condition.
Loading history...
101
  }
102
103
});
104
export {
105
  NumberFormatter
106
};
107