-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathcommon.js
More file actions
89 lines (80 loc) · 2.33 KB
/
Copy pathcommon.js
File metadata and controls
89 lines (80 loc) · 2.33 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var prefixHttpRegex = new RegExp('^(http|https)://', 'i');
var doubleHttpRegex = new RegExp('^http:/+http(s)*:/');
var validUrlRegex = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'); // port and path
// '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
// '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
// code-highlighting onload handler
hljs.initHighlightingOnLoad();
/**
* set invite code to be used for signup link and description thereof
* @param inviteCode
*/
function setInviteCode(inviteCode) {
$('.invite_code').text(inviteCode);
$('.invite_link').attr('href','https://algorithmia.com/signup?invite=' + inviteCode);
}
/**
* trim and ensure that a URL begins with http(s)://
* @param url
* @returns {string}
*/
function prefixHttp(url) {
if(url) {
url = url.trim();
url = prefixHttpRegex.test(url)?url:'http://'+url;
}
return url;
}
/**
* does this look like a URL?
* @param url
* @returns {boolean}
*/
function isValidUrl(url) {
return validUrlRegex.test(url);
}
/**
* get the portion of a URL before # and strip trailing /
* @param url
* @returns {*}
*/
function getUrlCore(url) {
return url.split('#')[0].replace(/\/$/, '');
}
/***
* add http:// prefix to an input element if empty, and clean up "http://http"
* @param elem jQuery element
*/
var requireHttp = function(elem) {
var requireHttpHandler = function(elem) {
var val = elem.val().replace(/\s/g,''); //even trim inner whitespace
if (val=='') {
elem.val('http://');
} else if (doubleHttpRegex.test(val)) {
elem.val(val.replace(doubleHttpRegex,'http$1:/'));
} else if (!prefixHttpRegex.test(val)) {
elem.val('http://'+val)
}
};
elem.bind('paste', function (e) {
setTimeout(function(){requireHttpHandler(elem)});
});
elem.bind('click keypress', function (e) {
requireHttpHandler(elem);
});
};
/**
* left-pad a number
* @param n original number
* @param width total number of characters desired
* @param padchar (optional) character with which to pad, else '0'
* @returns {*}
*/
function padleft(n, width, padchar) {
padchar = padchar || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(padchar) + n;
}