-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
49 lines (41 loc) · 1.38 KB
/
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
// misc helpers
// this module should have zero dependencies upon other modules in this prj (depending on 3rd party modules
// (e.g. stuff installed via npm) is fine though)
export function UUID() // technically this doesn't confirm to the GUID RFC, but for our purposes, it's fine
{
let values = new Uint8Array(16);
window.crypto.getRandomValues(values);
return Array.from(values, b => ('0' + b.toString(16)).slice(-2)).join('');
}
export function Now()
{
return new Date().getTime()/1000;
}
// trims and lowercases a string, replaces spaces with underscores
export function Slugify(s)
{
return s.trim().replaceAll(' ', '_').toLowerCase().replaceAll('(', '').replaceAll(')', '');
}
// given a page ID like 'foo__bar', converts it to a path like 'foo/bar'
const RE_FIND_DOUBLE_UNDER = new RegExp('__', 'g');
export function PageIDToPath(id)
{
return '/' + id.replace(RE_FIND_DOUBLE_UNDER, '/');
}
// opposite of PageIDToPath
const RE_FIND_SLASHES = new RegExp('/', 'g');
export function PagePathToID(path)
{
if (path[0] == '/')
path = path.substr(1);
return path.replace(RE_FIND_SLASHES, '__');
}
// given a number of bytes, returns a nicer-looking string in KB/MB/GB
export function FileSizeStr(n)
{
if (n < 1024) return n + ' bytes';
n = Math.round(n / 1024);
if (n < 1024) return n + ' KB';
n = Math.round(n / 1024);
return n + ' MB';
}