From 0d6f61913cb0a6a17066d54d145fb92798f313db Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sun, 4 Oct 2020 13:20:04 +0300 Subject: [PATCH] Allow untitled workouts --- src/parser/parser.test.ts | 24 +++++++++++++++++------- src/parser/parser.ts | 6 +----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/parser/parser.test.ts b/src/parser/parser.test.ts index a22002c..effe310 100644 --- a/src/parser/parser.test.ts +++ b/src/parser/parser.test.ts @@ -1,14 +1,24 @@ import { parse } from "."; describe("Parser", () => { - it("throws error for empty file", () => { - expect(() => parse("")).toThrowErrorMatchingInlineSnapshot( - `"Workout is missing a name. Use \`Name:\` to declare one. at line 1 char 1"`, - ); + it("creates Untitled workout from empty file", () => { + expect(parse("")).toMatchInlineSnapshot(` + Object { + "author": "", + "description": "", + "intervals": Array [], + "name": "Untitled", + } + `); - expect(() => parse(" \n \n \t")).toThrowErrorMatchingInlineSnapshot( - `"Workout is missing a name. Use \`Name:\` to declare one. at line 1 char 1"`, - ); + expect(parse(" \n \n \t")).toMatchInlineSnapshot(` + Object { + "author": "", + "description": "", + "intervals": Array [], + "name": "Untitled", + } + `); }); it("parses workout with just Name field", () => { diff --git a/src/parser/parser.ts b/src/parser/parser.ts index e9e518c..ec7dab2 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -138,12 +138,8 @@ const parseIntervals = (tokens: Token[]): Interval[] => { export const parseTokens = (tokens: Token[]): Workout => { const [header, intervalTokens] = parseHeader(tokens); - if (header.name === undefined) { - throw new ParseError("Workout is missing a name. Use `Name:` to declare one.", { row: 0, col: 0 }); - } - return { - name: header.name, + name: header.name || "Untitled", author: header.author || "", description: header.description || "", intervals: parseIntervals(intervalTokens),