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
|
import { h } from './dom'
interface ParseInsideTextResult {
before: string;
number: number;
after: string;
}
export function parseInsideText(str: string): ParseInsideTextResult | undefined {
let res = str.match(/^([^\d]*)(\d+)((\.|,)(\d+))?(.*)/)
if (res !== null && res.length === 7) {
return {
before: res[1],
number: parseFloat(res[2] + '.' + res[5]),
after: res[6]
}
} else {
return undefined;
}
}
interface ParseResult {
number: number;
remaining: string;
}
export function parse(str: string): ParseResult | undefined {
let res = str.match(/^(\d+)((\.|,)(\d+))?(.*$)/)
if (res !== null && res.length === 6) {
return {
number: parseFloat(res[1] + '.' + res[4]),
remaining: res[5]
}
} else {
return undefined;
}
}
export interface Node {
node: Element;
numberInput: HTMLInputElement;
}
export function node(tag: string, content: ParseInsideTextResult): Node {
let numberInput = h('input', {
'class': 'g-Number',
'value': prettyPrint(content.number)
}) as HTMLInputElement
return {
node: h(tag, {}, [content.before, numberInput, content.after]),
numberInput: numberInput
}
}
export function prettyPrint(n: number): string {
const xs = n.toString().split('.')
if (xs.length == 2) {
return xs[0] + ',' + xs[1].substring(0, 2)
} else {
return xs[0]
}
}
|