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