-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
69 lines (62 loc) · 1.85 KB
/
main.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
import { fromEvent, combineLatest } from "https://cdn.skypack.dev/rxjs";
import {
debounceTime,
filter,
map,
startWith,
} from "https://cdn.skypack.dev/rxjs/operators";
import _chunk from "https://cdn.skypack.dev/lodash.chunk";
let fromCsvTextArea = ($el) =>
fromEvent($el, "input").pipe(
debounceTime(300),
map((e) =>
e.target.value
.split("\n")
.map((s) => s.trim())
.filter(Boolean)
),
filter((v) => v.length > 0)
);
let $mode = document.getElementById("mode");
let $result = document.getElementById("result");
combineLatest([
fromCsvTextArea(document.getElementById("destinations")),
fromCsvTextArea(document.getElementById("mods")),
fromEvent($mode, "input").pipe(
map((e) => e.target.value),
startWith($mode.value)
),
])
.pipe(
map(([destinations, mods, mode]) =>
destinations.flatMap((d) =>
mods.flatMap((m) => {
switch (mode) {
case "dst-first":
return `${d} ${m}`;
case "mod-first":
return `${m} ${d}`;
default:
return [`${d} ${m}`, `${m} ${d}`];
}
})
)
)
)
.subscribe((r) => ($result.value = r.join("\n")));
let toDataCsv = (values) =>
encodeURI("data:text/csv;charset=utf-8," + values.join("\n"));
let download = (fileName, content) => {
let link = document.createElement("a");
link.setAttribute("href", content);
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
};
let $fileName= document.getElementById("fileName");
let $itemsPerFile = document.getElementById("itemsPerFile");
document.getElementById("download").addEventListener("click", () => {
_chunk($result.value.split("\n"), +$itemsPerFile.value).forEach((values, i) =>
download(`${$fileName.value || 'keywords'}-${i}.csv`, toDataCsv(values))
);
});