Passed
Push — develop ( 4f6000...8f0915 )
by
unknown
03:18
created

helper.js ➔ ???   D

Complexity

Conditions 12
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 12
nc 4
nop 1
dl 0
loc 22
rs 4.8
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A helper.js ➔ ... ➔ ??? 0 4 1

How to fix   Complexity   

Complexity

Complex classes like helper.js ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import i18n from './i18n.js'
2
import color from 'color'
3
import moment from 'moment'
4
5
export const libHandleFetchResult = fetchResult => {
6
  switch (fetchResult.status) {
7
    case 200:
8
    case 304:
9
      const resultJson = fetchResult.clone().json()
10
      return new Promise((resolve, reject) => resolve({
0 ignored issues
show
Unused Code introduced by
The parameter reject 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...
11
        apiResponse: fetchResult,
12
        body: resultJson
13
      }))
14
    case 204:
15
      return fetchResult
16
    case 400:
17
    case 404:
18
    case 409:
19
    case 500:
20
    case 501:
21
    case 502:
22
    case 503:
23
    case 504:
24
      return new Promise((resolve, reject) => reject(fetchResult)) // @TODO : handle errors from api result
25
  }
0 ignored issues
show
Comprehensibility introduced by
There is no default case in this switch, so nothing gets returned when all cases fail. You might want to consider adding a default or return undefined explicitly.
Loading history...
26
}
27
28
export const libAddAllResourceI18n = (i18n, translation) => {
29
  Object.keys(translation).forEach(lang =>
30
    Object.keys(translation[lang]).forEach(namespace =>
31
      i18n.addResources(lang, namespace, translation[lang][namespace])
32
    )
33
  )
34
}
35
36
export const libGenerateAvatarFromPublicName = publicName => {
37
  // code from https://stackoverflow.com/questions/3426404/create-a-hexadecimal-colour-based-on-a-string-with-javascript
38
  const stringToHashCode = str => str.split('').reduce((acc, char) => char.charCodeAt(0) + ((acc << 5) - acc), 0)
39
40
  const intToRGB = i => {
41
    const c = (i & 0x00FFFFFF).toString(16).toUpperCase()
42
    return '00000'.substring(0, 6 - c.length) + c
43
  }
44
45
  const hexcolor = '#' + intToRGB(stringToHashCode(publicName))
46
47
  let canvas = document.createElement('canvas')
48
49
  // http://code.google.com/p/explorercanvas/wiki/Instructions#Dynamically_created_elements
50
  if (!canvas.getContext) G_vmlCanvasManager.initElement(canvas)
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...
Bug introduced by
The variable G_vmlCanvasManager seems to be never declared. If this is a global, consider adding a /** global: G_vmlCanvasManager */ 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...
51
52
  let ctx = canvas.getContext('2d')
53
  canvas.width = 44
54
  canvas.height = 44
55
56
  const { r, g, b } = color(hexcolor).desaturate(0.75).rgb()
57
58
  ctx.beginPath()
59
  ctx.arc(22, 22, 20, 0, 2 * Math.PI, false)
60
  ctx.fillStyle = 'rgba(' + [r, g, b, 1].join() + ')'
61
  ctx.fill()
62
  ctx.stroke()
63
64
  return canvas.toDataURL('image/png', '')
65
}
66
67
export const libDisplayDate = (dateToDisplay, lang) => {
68
  i18n.changeLanguage(lang)
69
70
  const todayMoment = moment(new Date())
71
  const dateToDisplayMoment = moment(new Date(dateToDisplay))
72
73
  const diffDay = todayMoment.diff(dateToDisplayMoment, 'days')
74
  if (diffDay > 0) return i18n.t('{{nb}} days ago', {nb: diffDay})
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...
75
76
  const diffHour = todayMoment.diff(dateToDisplayMoment, 'hours')
77
  if (diffHour > 0) return i18n.t('{{nb}} hours ago', {nb: diffHour})
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...
78
79
  const diffMinute = todayMoment.diff(dateToDisplayMoment, 'minutes')
80
  if (diffMinute > 0) return i18n.t('{{nb}} minutes ago', {nb: diffMinute})
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...
81
82
  const diffSeconde = todayMoment.diff(dateToDisplayMoment, 'seconds')
83
  return i18n.t('{{nb}} seconds ago', {nb: diffSeconde})
84
}
85