forked from stx-labs/stacks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
109 lines (95 loc) · 2.69 KB
/
utils.js
File metadata and controls
109 lines (95 loc) · 2.69 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* @flow */
import { parse as uriParse } from 'uri-js'
export const BLOCKSTACK_HANDLER = 'blockstack'
/**
* Time
* @private
*/
export function nextYear() {
return new Date(
new Date().setFullYear(
new Date().getFullYear() + 1
)
)
}
export function nextMonth() {
return new Date(
new Date().setMonth(
new Date().getMonth() + 1
)
)
}
export function nextHour() {
return new Date(
new Date().setHours(
new Date().getHours() + 1
)
)
}
/**
* Query Strings
* @private
*/
export function updateQueryStringParameter(uri: string, key: string, value: string) {
const re = new RegExp(`([?&])${key}=.*?(&|$)`, 'i')
const separator = uri.indexOf('?') !== -1 ? '&' : '?'
if (uri.match(re)) {
return uri.replace(re, `$1${key}=${value}$2`)
} else {
return `${uri}${separator}${key}=${value}`
}
}
/**
* Versioning
* @param {string} v1 - the left half of the version inequality
* @param {string} v2 - right half of the version inequality
* @returns {bool} iff v1 >= v2
*/
export function isLaterVersion(v1: string, v2: string) {
const v1tuple = v1.split('.').map(x => parseInt(x, 10))
const v2tuple = v2.split('.').map(x => parseInt(x, 10))
for (let index = 0; index < v2.length; index++) {
if (index >= v1.length) {
v2tuple.push(0)
}
if (v1tuple[index] < v2tuple[index]) {
return false
}
}
return true
}
/**
* UUIDs
* @private
*/
export function makeUUID4() {
let d = new Date().getTime()
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now() // use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (d + Math.random() * 16) % 16 | 0
d = Math.floor(d / 16)
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
}
/**
* Checks if both urls pass the same origin check & are absolute
* @param {[type]} uri1 first uri to check
* @param {[type]} uri2 second uri to check
* @return {Boolean} true if they pass the same origin check
* @private
*/
export function isSameOriginAbsoluteUrl(uri1: string, uri2: string) {
const parsedUri1 = uriParse(uri1)
const parsedUri2 = uriParse(uri2)
const port1 = parsedUri1.port | 0 || (parsedUri1.scheme === 'https' ? 443 : 80)
const port2 = parsedUri2.port | 0 || (parsedUri2.scheme === 'https' ? 443 : 80)
const match = {
scheme: parsedUri1.scheme === parsedUri2.scheme,
hostname: parsedUri1.hostname === parsedUri2.hostname,
port: port1 === port2,
absolute: (parsedUri1.reference === 'absolute') && (parsedUri2.reference === 'absolute')
}
return match.scheme && match.hostname && match.port && match.absolute
}