aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.ts22
-rw-r--r--src/number.ts87
2 files changed, 42 insertions, 67 deletions
diff --git a/src/main.ts b/src/main.ts
index ad83591..184d26d 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -5,12 +5,12 @@ import * as dom from './dom'
const itemEntries =
dom.nodeListToArray(document.querySelectorAll('.g-Recipe__Content ul > li'))
- .map(itemNode => ({ tag: 'li', node: itemNode }))
+ .map(itemNode => ({ name: 'li', node: itemNode }))
const h1 = document.querySelector<HTMLElement>('.g-Recipe__Content h1')
if (h1 !== null) {
- itemEntries.push({ tag: 'h1', node: h1 })
+ itemEntries.push({ name: 'h1', node: h1 })
}
const inputs = setupInputs(itemEntries)
@@ -20,7 +20,7 @@ inputs.map(input => {
if (e.target !==null) {
const parsed = number.parse((e.target as HTMLInputElement).value)
- if (parsed !== undefined && parsed.before === '' && parsed.after === '') {
+ if (parsed !== undefined) {
const factor = parsed.number / input.number
inputs.map(input2 => {
if (input.node !== input2.node) {
@@ -32,8 +32,8 @@ inputs.map(input => {
}
})
-interface InputEntry {
- tag: string;
+interface InputTag {
+ name: string;
node: HTMLElement;
}
@@ -42,18 +42,18 @@ interface InputResult {
node: HTMLInputElement
}
-function setupInputs(xs: InputEntry[]): InputResult[] {
+function setupInputs(tags: InputTag[]): InputResult[] {
const res: InputResult[] = []
- xs.forEach(x => {
- const parsed = number.parse(x.node.innerText)
+ tags.forEach(tag => {
+ const parsed = number.parseInsideText(tag.node.innerText)
if (parsed !== undefined) {
- const numberNode = number.node(x.tag, parsed)
- dom.replace(x.node, numberNode.node)
+ const numberNode = number.node(tag.name, parsed)
+ dom.replace(tag.node, numberNode.node)
res.push({
number: parsed.number,
- node: numberNode.number
+ node: numberNode.numberInput
})
}
})
diff --git a/src/number.ts b/src/number.ts
index 3021dc9..6663329 100644
--- a/src/number.ts
+++ b/src/number.ts
@@ -1,82 +1,57 @@
import { h } from './dom'
-export interface Parsed {
+interface ParseInsideTextResult {
before: string;
number: number;
after: string;
}
-export function parse(str: string): Parsed | undefined {
- let start;
- for (start = 0; start < str.length; start++) {
- if (isDigit(str.charAt(start))) {
- break
- }
- }
-
- if (start === str.length) {
- return undefined
- }
-
- // Integer part
- let integerPart = '';
- let end = start;
- for (; end < str.length; end++) {
- const c = str.charAt(end)
+export function parseInsideText(str: string): ParseInsideTextResult | undefined {
+ let res = str.match(/^([^\d]*)(\d+)((\.|,)(\d+))?(.*)/)
- if (!isDigit(c)) {
- break
- } else {
- integerPart += c
+ if (res !== null && res.length === 7) {
+ return {
+ before: res[1],
+ number: parseFloat(res[2] + '.' + res[5]),
+ after: res[6]
}
+ } else {
+ return undefined;
}
+}
- // Decimal sign
- if (end < str.length && (str.charAt(end) === '.' || str.charAt(end) === ',')) {
- end++
- }
+interface ParseResult {
+ number: number;
+ remaining: string;
+}
- // Decimal part
- let decimalPart = '';
- for (; end < str.length; end++) {
- const c = str.charAt(end)
+export function parse(str: string): ParseResult | undefined {
+ let res = str.match(/^(\d+)((\.|,)(\d+))?(.*$)/)
- if (!isDigit(c)) {
- break
- } else {
- decimalPart += c
+ if (res !== null && res.length === 6) {
+ return {
+ number: parseFloat(res[1] + '.' + res[4]),
+ remaining: res[5]
}
+ } else {
+ return undefined;
}
-
-
- return {
- before: str.substring(0, start),
- number: parseFloat(integerPart + (decimalPart !== '' ? '.' + decimalPart : '')),
- after: str.substring(end, str.length)
- }
-}
-
-function isDigit(c: string) {
- return c >= '0' && c <= '9'
}
export interface Node {
node: Element;
- number: HTMLInputElement;
+ numberInput: HTMLInputElement;
}
-export function node(tag: string, parsedNumber: Parsed): Node {
- const numberElement = h(
- 'input',
- {
- 'class': 'g-Number',
- 'value': prettyPrint(parsedNumber.number)
- }
- ) as 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, {}, [parsedNumber.before, numberElement, parsedNumber.after]),
- number: numberElement,
+ node: h(tag, {}, [content.before, numberInput, content.after]),
+ numberInput: numberInput
}
}