-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
140 lines (111 loc) · 2.85 KB
/
Copy pathutils.js
File metadata and controls
140 lines (111 loc) · 2.85 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// https://stackoverflow.com/a/38641281/943814
export const naturalSorter = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base'
});
export function parseGemData(tsv) {
return tsv.split('\n')
.filter(line => line.length > 0)
.map(line => line.split('\t'))
.map(columns => ({
name: columns[0],
modTime: columns[1],
size: columns[2],
}));
}
export function el(elType, options) {
const dom = document.createElement(elType);
if (options) {
for (const key in options) {
if (key.startsWith('on')) {
const eventName = key.slice(2);
dom.addEventListener(eventName, options[key]);
}
}
if (options.classList) {
for (const cls of options.classList) {
dom.classList.add(cls);
}
}
}
return dom;
}
export function ValueInput(name, init) {
const dom = el('div');
dom.classList.add('value-input');
const labelEl = el('span');
labelEl.classList.add('value-input__label');
labelEl.innerText = `${name}:`;
dom.appendChild(labelEl);
const inputEl = el('input');
inputEl.classList.add('value-input__text');
inputEl.setAttribute('type', 'text');
dom.appendChild(inputEl);
if (init) {
inputEl.value = init;
}
function getValue() {
return inputEl.value;
}
return {
dom,
getValue,
};
}
export function MarginBox(child) {
const dom = el('div');
dom.classList.add('margin-box');
dom.appendChild(child);
return dom;
}
export function OptionInput(label, options, initialOption) {
const dom = el('div');
dom.classList.add('option-input');
const labelEl = el('div');
labelEl.classList.add('option-input__label');
labelEl.innerText = label + ':';
dom.appendChild(labelEl);
const select = el('select', {
onchange: (e) => {
select.dispatchEvent(new CustomEvent('option-selected', {
bubbles: true,
detail: {
option: select.value,
},
}));
},
});
dom.appendChild(select);
for (const option of options) {
const optionEl = el('option');
optionEl.setAttribute('value', option);
optionEl.innerText = option;
select.appendChild(optionEl);
}
if (initialOption) {
select.value = initialOption;
}
return dom;
}
export async function* entryIterator(entriesDirUrl, token, comparator) {
const db = await fetch(`${entriesDirUrl}db.json?access_token=${token}`)
.then(r => r.json());
for (let id = db.lastId; id >= 1; id -= 1) {
const entryPath = idToPath(id);
yield `${entriesDirUrl}${entryPath}`;
}
}
export function idToPath(id) {
const idStr = id.toString().split('').reverse().join('');
let path = '';
for (let i = 0; i < idStr.length; i++) {
if (i % 3 === 0) {
path = '/' + path;
}
path = idStr[i] + path;
}
return path;
}
export function entryPathToId(path) {
return parseInt(path.replace('/', ''));
}