-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
horizon.utils.js
57 lines (51 loc) · 1.63 KB
/
horizon.utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Utilities for common needs which aren't JS builtins. */
horizon.utils = {
// Log function which checks for DEBUG and the existence of a console.
log: function () {
if (horizon.conf.debug && typeof(console) !== "undefined" && typeof(console.log) !== "undefined") {
console.log(arguments);
}
},
capitalize: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
/*
Adds commas to any integer or numbers within a string for human display.
EG:
horizon.utils.humanizeNumbers(1234); -> "1,234"
horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234"
*/
humanizeNumbers: function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
/*
Truncate a string at the desired length. Optionally append an ellipsis
to the end of the string.
EG:
horizon.utils.truncate("String that is too long.", 18, true); ->
"String that is too…"
*/
truncate: function(string, size, includeEllipsis) {
if(string.length > size) {
if(includeEllipsis) {
return string.substring(0, (size - 3)) + "…";
} else {
return string.substring(0, size);
}
} else {
return string;
}
},
loadAngular: function (element) {
angular.injector(['ng', 'hz']).
invoke(['$rootScope', '$compile', function ($rootScope, $compile) {
try {
$compile(element)($rootScope);
$rootScope.$apply();
} catch (err) {}
/*
Compilation fails when it could not find a directive, fails silently on this, it is an angular behaviour.
*/
}]);
}
};