Parsing of tags

This commit is contained in:
Rene Saarsoo 2020-10-06 16:39:28 +03:00
parent 77661ca6d7
commit 30a5e3b42b
3 changed files with 49 additions and 4 deletions

View File

@ -35,7 +35,7 @@ describe("Parser", () => {
`); `);
}); });
it("parses workout header with all fields", () => { it("parses workout header with name, author, description", () => {
expect( expect(
parse(` parse(`
Name: My Workout Name: My Workout
@ -60,6 +60,46 @@ Description:
`); `);
}); });
it("parses workout header with comma-separated tags", () => {
expect(
parse(`
Name: My Workout
Tags: Recovery, Intervals , FTP
`),
).toMatchInlineSnapshot(`
Object {
"author": "",
"description": "",
"intervals": Array [],
"name": "My Workout",
"tags": Array [
"Recovery",
"Intervals",
"FTP",
],
}
`);
});
it("treats with space-separated tags as single tag", () => {
expect(
parse(`
Name: My Workout
Tags: Recovery Intervals FTP
`),
).toMatchInlineSnapshot(`
Object {
"author": "",
"description": "",
"intervals": Array [],
"name": "My Workout",
"tags": Array [
"Recovery Intervals FTP",
],
}
`);
});
it("throws error for unknown labels", () => { it("throws error for unknown labels", () => {
expect(() => expect(() =>
parse(` parse(`

View File

@ -43,6 +43,11 @@ const parseHeader = (tokens: Token[]): [Header, Token[]] => {
const [description, rest] = extractText(tokens); const [description, rest] = extractText(tokens);
header.description = description; header.description = description;
tokens = rest; tokens = rest;
} else if (token.type === "header" && token.value === "Tags") {
tokens.shift();
const [tags, rest] = extractText(tokens);
header.tags = tags.split(/\s*,\s*/);
tokens = rest;
} else { } else {
// End of header // End of header
break; break;
@ -138,7 +143,7 @@ export const parseTokens = (tokens: Token[]): Workout => {
name: header.name || "Untitled", name: header.name || "Untitled",
author: header.author || "", author: header.author || "",
description: header.description || "", description: header.description || "",
tags: [], tags: header.tags || [],
intervals: parseIntervals(intervalTokens), intervals: parseIntervals(intervalTokens),
}; };
}; };

View File

@ -1,10 +1,10 @@
import { ParseError } from "./ParseError"; import { ParseError } from "./ParseError";
export type HeaderType = "Name" | "Author" | "Description"; export type HeaderType = "Name" | "Author" | "Description" | "Tags";
export type IntervalType = "Warmup" | "Rest" | "Interval" | "Cooldown" | "FreeRide"; export type IntervalType = "Warmup" | "Rest" | "Interval" | "Cooldown" | "FreeRide";
const isHeaderType = (value: string): value is HeaderType => { const isHeaderType = (value: string): value is HeaderType => {
return ["Name", "Author", "Description"].includes(value); return ["Name", "Author", "Description", "Tags"].includes(value);
}; };
const isIntervalType = (value: string): value is IntervalType => { const isIntervalType = (value: string): value is IntervalType => {
return ["Warmup", "Rest", "Interval", "Cooldown", "FreeRide"].includes(value); return ["Warmup", "Rest", "Interval", "Cooldown", "FreeRide"].includes(value);