Allow untitled workouts

This commit is contained in:
Rene Saarsoo 2020-10-04 13:20:04 +03:00
parent adebc3d460
commit 0d6f61913c
2 changed files with 18 additions and 12 deletions

View File

@ -1,14 +1,24 @@
import { parse } from "."; import { parse } from ".";
describe("Parser", () => { describe("Parser", () => {
it("throws error for empty file", () => { it("creates Untitled workout from empty file", () => {
expect(() => parse("")).toThrowErrorMatchingInlineSnapshot( expect(parse("")).toMatchInlineSnapshot(`
`"Workout is missing a name. Use \`Name:\` to declare one. at line 1 char 1"`, Object {
); "author": "",
"description": "",
"intervals": Array [],
"name": "Untitled",
}
`);
expect(() => parse(" \n \n \t")).toThrowErrorMatchingInlineSnapshot( expect(parse(" \n \n \t")).toMatchInlineSnapshot(`
`"Workout is missing a name. Use \`Name:\` to declare one. at line 1 char 1"`, Object {
); "author": "",
"description": "",
"intervals": Array [],
"name": "Untitled",
}
`);
}); });
it("parses workout with just Name field", () => { it("parses workout with just Name field", () => {

View File

@ -138,12 +138,8 @@ const parseIntervals = (tokens: Token[]): Interval[] => {
export const parseTokens = (tokens: Token[]): Workout => { export const parseTokens = (tokens: Token[]): Workout => {
const [header, intervalTokens] = parseHeader(tokens); 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 { return {
name: header.name, name: header.name || "Untitled",
author: header.author || "", author: header.author || "",
description: header.description || "", description: header.description || "",
intervals: parseIntervals(intervalTokens), intervals: parseIntervals(intervalTokens),