diff --git a/src/index.ts b/src/index.ts index b4fbfe7..0aca86c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,5 @@ import * as fs from "fs"; -import { parseRule } from "./parser"; - -const isDefined = (x: T | undefined): x is T => Boolean(x); +import { parseFile } from "./parser"; const filename = process.argv[2]; @@ -9,13 +7,9 @@ console.log(`Parsing: ${filename}`); const file = fs.readFileSync(filename, "utf8"); -file - .split(/\n/) - .map(parseRule) - .filter(isDefined) - .forEach((rule) => { - console.log(rule.type); - rule.params.forEach((p) => { - console.log(` ${p.type}: ${p.value}`); - }); +parseFile(file).forEach((rule) => { + console.log(rule.type); + rule.params.forEach((p) => { + console.log(` ${p.type}: ${p.value}`); }); +}); diff --git a/src/parser.ts b/src/parser.ts index a645e3f..c42123d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -30,8 +30,9 @@ const parseParams = (type: RuleType, text: string): Param[] => { switch (type) { case RuleType.Name: case RuleType.Author: - case RuleType.Description: + case RuleType.Description: { return [{ type: ParamType.Text, value: text }]; + } case RuleType.Warmup: case RuleType.Rest: case RuleType.Interval: @@ -40,7 +41,7 @@ const parseParams = (type: RuleType, text: string): Param[] => { } }; -export const parseRule = (line: string): Rule | undefined => { +const parseRule = (line: string): Rule | undefined => { const matches = line.match(/^(\w+):(.*)$/); if (!matches) { return undefined; @@ -55,3 +56,21 @@ export const parseRule = (line: string): Rule | undefined => { params: parseParams(type, matches[2].trim()), }; }; + +export const parseFile = (file: string): Rule[] => { + const rules: Rule[] = []; + + file.split("\n").forEach((line) => { + const rule = parseRule(line); + if (rule) { + rules.push(rule); + return; + } + const lastRule = rules[rules.length - 1]; + if (lastRule && lastRule.type === RuleType.Description) { + lastRule.params.push({ type: ParamType.Text, value: line.trim() }); + } + }); + + return rules; +};