src/scripts/services/format.js   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 2.67

Size

Lines of Code 42
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 8
c 0
b 0
f 0
nc 2
mnd 3
bc 7
fnc 3
dl 0
loc 42
rs 10
bpm 2.3333
cpm 2.6666
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.service(ꞌformatꞌ) 0 35 1
1
/**
2
 format
3
 Service that formats elements as HTML.
4
5
 @namespace Services
6
 */
7
'use strict';
8
9
angular
10
  .module('game')
11
  .service('format', ['util',
12
  'state',
13
    function(util) {
14
      /*
15
       * Formats in HTML a compound i.e. a collection of resources of
16
       * the form x + y + z
17
       */
18
      this.compoundFormat = function(number, compound, player) {
19
        let compoundHTML = '';
20
        let keys = Object.keys(compound);
21
        for (let i = 0; i < keys.length; i++) {
22
          if (Number.isInteger(number) && number > 1) {
23
            compoundHTML += util.prettifyNumber(number * compound[keys[i]], player) +
24
              ' ';
25
          } else if (compound[keys[i]] !== 1) {
26
            compoundHTML += util.prettifyNumber(compound[keys[i]], player) + ' ';
27
          }
28
          compoundHTML += util.getHTML(keys[i]) + ' ';
29
          if (i < keys.length - 1) {
30
            compoundHTML += '+ ';
31
          }
32
        }
33
        return compoundHTML.trim();
34
      };
35
36
      /*
37
       * Formats a reaction i.e. a transformation from one compound to
38
       * another
39
       */
40
      this.reactionFormat = function(number, reaction, player) {
41
        let reactionHTML = '';
42
        reactionHTML += this.compoundFormat(number, reaction.reactant, player);
43
        reactionHTML += ' <span class=\'icon\'>&#8594;</span> ';
44
        reactionHTML += this.compoundFormat(number, reaction.product, player);
45
        return reactionHTML;
46
      };
47
    }
48
  ]);
49