Passed
Pull Request — master (#2)
by Luís
03:48 queued 02:00
created

src/js/helpers/string/index.js   A

Complexity

Total Complexity 17
Complexity/F 2.13

Size

Lines of Code 114
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 79.31%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 0
nc 1
dl 0
loc 114
ccs 23
cts 29
cp 0.7931
crap 0
rs 10
c 3
b 0
f 1
wmc 17
mnd 3
bc 15
fnc 8
bpm 1.875
cpm 2.125
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A ???.capitalize 0 3 1
A ???.uuid 0 8 1
C ???.excerpt 0 34 8
A ???.resolveUrl 0 7 2
A ???.ucwords 0 3 1
A ???.ucfirst 0 6 1
1
export default {
2
    /**
3
     * Add basepath if isn't complete
4
     * Consider a complete url when it contains '//'
5
     *
6
     * Examples of complete urls:
7
     * http://google.com
8
     * https://google.com
9
     * //google.com
10
     *
11
     * Examples of incomplete urls:
12
     * google.com
13
     * google
14
     * users/create
15
     * /users/create
16
     *
17
     * @param {String} url
18
     * @param {String} basePath basepath to be used, with protocol
19
     * @returns {String}
20
     * 
21
     */
22
    resolveUrl: function (url, basePath = "") {
23 5
        if (url.indexOf("//") === -1) {
24 1
            return basePath + url;
25
        }
26
27 4
        return url;
28
    },
29
30
    /**
31
     * Makes string capitalized
32
     * @param {String} string
33
     * @returns {string}
34
     */
35
    ucfirst: function (string) {
36
        "use strict";
37
38 13
        string = string.toLocaleLowerCase();
39 13
        return string.charAt(0).toLocaleUpperCase() + string.substr(1);
40
    },
41
42
    /**
43
     * @see App.helpers.string.ucfirst
44
     */
45
    capitalize: function (string) {
46 4
        return this.ucfirst(string);
47
    },
48
49
    /**
50
     * Makes every word from string capitalized
51
     * @param {String} string
52
     * @returns {string}
53
     */
54
    ucwords: function (string) {
55 5
        return string.split(" ").map(word => this.ucfirst(word)).join(" ");
56
    },
57
58
    /**
59
     * Will crop the text to fit the maxLength provided. Will try to not break any words
60
     * and add "..." on the end of the string
61
     * 
62
     * @param text
63
     * @param maxLength
64
     * @returns {String}
65
     */
66
    excerpt: function (text, maxLength) {
67 4
        if (isNaN(maxLength)) {
68
            throw new Error("maxLength should be an integer");
69
        }
70
71 4
        maxLength = parseInt(maxLength, 10);
72 4
        if (maxLength < 1) {
73
            throw new Error("maxLength must be greater than 0");
74
        }
75
76 4
        if (typeof text !== "string") {
77
            throw "text must be string";
78
        }
79
80 4
        if (text.length > maxLength) {
81 4
            const exploded = text.split(" ");
82 4
            let counter = 0;
83 4
            let i = 0;
84 4
            let response = [];
85
86 4
            for (i = 0; i < exploded.length; i++) {
87 10
                if (counter + exploded[i].length <= maxLength || i === 0) {
88 6
                    response.push(exploded[i]);
89 6
                    counter += exploded[i].length;
90
                } else {
91 4
                    break;
92
                }
93
            }
94
95 4
            text = response.join(" ") + "...";
96
        }
97
98 4
        return text;
99
    },
100
101
    /**
102
     * generares a UUID
103
     * Ref: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
104
     * @returns {String}
105
     */
106
    uuid: function () {
107
        "use strict";
108
109
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
110
            var r = Math.random()*16|0, v = c === "x" ? r : (r&0x3|0x8);
111
            return v.toString(16);
112
        });
113
    }
114
}
115