Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (74 loc) · 1.5 KB
/
Copy pathindex.js
File metadata and controls
79 lines (74 loc) · 1.5 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
import m from "mithril";
import "./style.css";
const state = {
input: "food for thought",
rules: [
{
match: "foo(.+)",
replace: `bar($1)`
}
]
};
const ruleUi = rule =>
m("div.rule", [
m("textarea.match", {
placeholder: "match",
value: rule.match,
oninput: m.withAttr("value", value => {
rule.match = value;
})
}),
m("textarea.replace", {
placeholder: "replace",
value: rule.replace,
oninput: m.withAttr("value", value => {
rule.replace = value;
})
}),
m(
"button",
{
onclick: () => {
state.rules = state.rules.filter(r => r !== rule);
}
},
"remove"
)
]);
const execute = state => {
let text = state.input;
state.rules.forEach(rule => {
if (!rule.match) return;
const matchRe = new RegExp(rule.match, "gm");
text = text.replace(matchRe, rule.replace);
});
return text;
};
const view = () => {
return m("main", [
m("textarea#input", {
value: state.input,
placeholder: "input",
oninput: m.withAttr("value", value => {
state.input = value;
})
}),
m("div#rules", [
m(
"button",
{
onclick: () => {
state.rules.push({ match: "", replace: "" });
}
},
"add rule"
),
state.rules.map(ruleUi)
]),
m("textarea#output", {
value: execute(state),
readonly: true
})
]);
};
m.mount(document.body, { view });