src/create_grouped_icon.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 70
Function Count 2

Duplication

Duplicated Lines 40
Ratio 57.14 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
eloc 48
nc 1
dl 40
loc 70
rs 10
c 1
b 0
f 0
wmc 2
mnd 0
bc 2
fnc 2
bpm 1
cpm 1
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A create_grouped_icon.js ➔ createGroupedIcon 0 26 1
A create_grouped_icon.js ➔ generateGroupedCanvas 40 40 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/** global: google */
2
import { IconObject } from "./icon_object.js";
3
4
import { getColors } from "./parsers.js";
5 View Code Duplication
const generateGroupedCanvas = function(options) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
    let text_x,
7
        canvas = options.canvas || document.createElement("canvas"),
8
        context = canvas.getContext("2d"),
9
        font = options.font || "fontello",
10
        fontsize = options.fontsize || 26;
11
12
    canvas.width = 54;
13
    canvas.height = 48;
14
    context.clearRect(0, 0, canvas.width, canvas.height);
15
16
    let colors = getColors(options),
17
        color0 = colors[0];
18
    context.beginPath();
19
20
    context.font = "normal normal normal " + fontsize + "px " + font;
21
22
    context.textBaseline = "top";
23
    let textWidth = context.measureText(options.unicodelabel);
24
    text_x = Math.floor(canvas.width / 2 - textWidth.width / 2);
25
26
    context.shadowOffsetX = -2;
27
    context.shadowOffsetY = -2;
28
    context.shadowBlur = 0;
29
30
    context.fillStyle = "#FFFFFF";
31
    context.shadowColor = "#666666";
32
33
    context.fillText(options.unicodelabel, text_x - 4, 2);
34
    context.fillText(options.unicodelabel, text_x, 5);
35
    context.fillStyle = color0;
36
    context.fillText(options.unicodelabel, text_x + 4, 8);
37
38
    context.strokeStyle = "#FFFFFF";
39
    context.strokeText(options.unicodelabel, text_x + 4, 8);
40
41
    canvas.fillColor = color0;
42
43
    return canvas;
44
};
45
46
export function createGroupedIcon(theoptions) {
47
    theoptions.scale = theoptions.scale || 1;
48
    theoptions.fontsize = theoptions.fontsize || 26;
49
50
    let markerCanvas = generateGroupedCanvas(theoptions),
51
        markerOpts = {};
52
53
    let scale = theoptions.scale;
54
55
    theoptions.type = "transparent";
56
57
    Object.assign(markerOpts, theoptions);
58
59
    Object.assign(markerOpts, {
60
        origin: { x: 0, y: 0 },
61
        anchor: { x: 27 * scale, y: 24 * scale },
62
        size: { width: 54, height: 48 },
63
        scaledSize: { width: 54 * scale, height: 48 * scale }
64
    });
65
66
    let url = markerCanvas.toDataURL(),
67
        fillColor = markerCanvas.fillColor,
68
        iconObj = new IconObject(url, fillColor, markerOpts);
69
70
    return iconObj;
71
}
72