-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
118 lines (93 loc) · 2.08 KB
/
helpers.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var Url = require('url')
module.exports = {
merge: merge,
isFullUrl: isFullUrl,
hasQueryString: hasQueryString,
lowerCaseKeys: lowerCaseKeys,
isType: isType,
zip: zip,
matchDomain: matchDomain
}
function merge(dest, src) {
for(var i in src) {
if(src.hasOwnProperty(i)) {
if(typeof dest[i] == 'undefined') {
dest[i] = src[i]
}
}
}
}
function isType(test, type) {
return Object.prototype.toString.call(test).toLowerCase() === '[object '+type+']'
}
function isFullUrl(expressLike) {
// Can't determine from regex, so assume full url
if(expressLike instanceof RegExp) {
return true
}
if(typeof expressLike !== 'string') {
return false
}
if(/^https?:\/\/.*/i.test(expressLike)) {
return true
}
if(/^[\w-]+\..*/.test(expressLike)) {
return true
}
return false
}
function hasQueryString(expressLike) {
if(expressLike instanceof RegExp) {
return true
}
if(typeof expressLike !== 'string') {
return false
}
if( /\?[^\/]+/.test(expressLike)) {
return true
}
}
function lowerCaseKeys(obj) {
for(var key in obj) {
if(hasOwnProperty.call(obj, key)) {
var val = obj[key]
delete obj[key]
obj[key.toLowerCase()] = val
}
}
return obj
}
function corerceIntoUrlObj(str) {
str = typeof str === 'string' ? str : ''
if(!/^https?:\/\//.test(str)) {
str = 'http://' + str
}
return Url.parse(str)
}
function matchDomain(host, testHost) {
host = corerceIntoUrlObj(host).host
testHost = corerceIntoUrlObj(testHost).host
// Design Decision, don't match empty
if(host === '') {
return false
}
if(host === testHost) {
return true
}
var hostDomain = host.split('.').reverse()
var testHostDomain = testHost.split('.').reverse()
var len = host.length
for (var i = 0; i < len; i++) {
if(host[i] !== testHost[i])
return false
}
return true
}
function zip(left, right, fn) {
var len = Math.min(left.length, right.length)
var newArr = []
for (var i = 0; i < len; i++) {
fn ? newArr.push(fn(left[i], right[i])) : null
}
return newArr
}