-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
96 lines (80 loc) · 2.18 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
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
export function $(selector) {
return document.querySelector(selector);
}
export function $$(selector) {
return Array.from(document.querySelectorAll(selector));
}
export function countWords(string) {
return string.split(/\n/)
.map((l) => l.trim())
.filter((l) => l !== '')
.join('. ')
.split(/(\b[^\s]+\b)/)
.map((w) => w.trim())
.filter((w) => ['', '.', "'", ','].indexOf(w) === -1)
.length;
}
export function elementFromHtml(html) {
const template = document.createElement('template');
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
}
export function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function tryFor(times = 10, timeout = 1000, fn) {
function go() {
fn();
if (times > 0) {
window.requestIdleCallback(go, { timeout });
} else {
times--;
}
}
go();
}
export function waitFor(fn, timeout, tries = 10) {
return new Promise((resolve, reject) => {
function check() {
if (fn() || tries === 0) {
resolve();
} else {
tries--;
window.requestIdleCallback(check, { timeout });
}
}
check();
});
}
export function waitForSelector(selector, timeout) {
function check() {
return !!$(selector);
}
return waitFor(check, timeout);
}
export class Storage {
constructor(data) {
this._store = window.localStorage;
this._key = '__nagfree__';
if (!!this._store.getItem(this._key)) {
const jsonData = this._store.getItem(this._key);
this._data = JSON.parse(jsonData);
} else {
this._data = data;
this.save();
}
}
get(key) {
return this._data[key];
}
set(key, val) {
this._data[key] = val;
alert(JSON.stringify(this._data));
this.save();
}
save() {
const json = JSON.stringify(this._data);
this._store.setItem(this._key, json);
}
}