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

???.excerpt   C

Complexity

Conditions 8
Paths 3

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.2518

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
nc 3
nop 2
dl 0
loc 34
ccs 16
cts 19
cp 0.8421
crap 8.2518
rs 5.3846
c 1
b 0
f 0
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