-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathbase.mjs
40 lines (36 loc) · 1.07 KB
/
base.mjs
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
import { cookies } from './cookies.mjs'
// Return value after `val` in the array `a`, looping around
export function next(a, val) {
var index = a.indexOf(val);
return index == -1 || index == (a.length - 1) ? a[0] : a[index + 1];
}
// Return value before `val` in the array `a`, looping around
export function prev(a, val) {
var index = a.indexOf(val);
return index <= 0 ? a[a.length - 1] : a[index - 1];
}
/*
Escape the string in an app-specific way:
<x> become <em>x</em>
[x] becomes <span class="optional">x</span>
`x` becomes <code>x</code>
\r becomes <br>
*/
export function esc(s) {
return s
.replace(/</g, 'zyx')
.replace(/>/g, '</em>')
.replace(/zyx/g, '<em>')
.replace(/\[/g, '<span class="optional">')
.replace(/]/g, '</span>')
.replace(/`(.*?)`/g, '<code>' + "$1" + '</code>')
.replace(/\r/g, '<br>');
}
// Return a two charact language code
export function detectLanguage(/*pass in navi*/gator) {
return cookies.read('lang') ||
(cookies.read('language') ||
gator.language ||
gator.userLanguage ||
'en').slice(0, 2)
}