src/backgrid_modules/core.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2.5

Size

Lines of Code 49
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 49
rs 10
wmc 10
mnd 2
bc 7
fnc 4
bpm 1.75
cpm 2.5
noi 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
A Backgrid.callByNeed 0 8 3
A core.js ➔ lpad 0 9 3
A Backgrid.resolveNameToClass 0 14 3
1
import Backbone from 'backbone';
0 ignored issues
show
introduced by
Definition for rule 'keyword-spacing' was not found
Loading history...
2
import _ from 'underscore';
3
4
function lpad(str, length, padstr) {
5
  var paddingLen = length - (str + '').length;
6
  paddingLen = paddingLen < 0 ? 0 : paddingLen;
7
  var padding = '';
8
  for (var i = 0; i < paddingLen; i++) {
9
    padding = padding + padstr;
10
  }
11
  return padding + str;
12
}
13
14
var Backgrid = {
15
  VERSION: '0.3.7-es6',
16
  Extension: {},
17
18
  resolveNameToClass: function (name, suffix) {
19
    if (_.isString(name)) {
20
      var key = _.map(name.split('-'), function (e) {
21
        return e.slice(0, 1).toUpperCase() + e.slice(1);
22
      }).join('') + suffix;
23
      var klass = Backgrid[key] || Backgrid.Extension[key];
24
      if (_.isUndefined(klass)) {
25
        throw new ReferenceError("Class '" + key + "' not found");
26
      }
27
      return klass;
28
    }
29
30
    return name;
31
  },
32
33
  callByNeed: function () {
34
    var value = arguments[0];
35
    if (!_.isFunction(value)) return value;
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...
36
37
    var context = arguments[1];
38
    var args = [].slice.call(arguments, 2);
39
    return value.apply(context, !!(args + '') ? args : []);
0 ignored issues
show
introduced by
Redundant double negation in a ternary condition.
Loading history...
40
  },
41
  $: Backbone.$
42
43
};
44
_.extend(Backgrid, Backbone.Events);
45
46
export {
47
  lpad,
48
  Backgrid
49
};
50