UNPKG

klog.js

Version:

A JavaScript implementation of the Klog time tracking file format

106 lines (105 loc) 2.87 kB
import type { DurationNode, EntryNode, FileNode, RecordNode, TimeNode, TimeRangeNode } from "./types.js"; import { Record } from "./record.js"; interface RuleToNode { file: FileNode; record: RecordNode; date: Date; entry: EntryNode; timeRange: TimeRangeNode; time: TimeNode; duration: DurationNode; } interface ParseAST { (source: string): FileNode; <T extends keyof RuleToNode>(source: string, rule: T): RuleToNode[T]; } /** * Parses a Klog source string into an AST. * @param source - The Klog source string to parse. * @param rule - The grammar rule to apply. If not provided, defaults to "file". Provided mostly for testing. * @throws {Error} The parsing failed. * @example * ``` * const source = ` * 2021-06-20 * 08:00 - 15:00 Work * -1h Lunch * `; * const ast = parseAST(source); * console.log(JSON.stringify(ast, null, 2)); * // { * // "type": "file", * // "records": [ * // { * // "type": "record", * // "date": "2021-06-19T14:00:00.000Z", * // "shouldTotal": null, * // "summary": null, * // "entries": [ * // { * // "type": "entry", * // "value": { * // "type": "timeRange", * // "open": false, * // "format": 0, * // "start": { * // "type": "time", * // "hour": 8, * // "minute": 0, * // "shift": 0, * // "format": "24h" * // }, * // "end": { * // "type": "time", * // "hour": 15, * // "minute": 0, * // "shift": 0, * // "format": "24h" * // } * // }, * // "summary": "Work" * // }, * // { * // "type": "entry", * // "value": { * // "type": "duration", * // "value": -60, * // "sign": "-" * // }, * // "summary": "Lunch" * // } * // ] * // } * // ] * // } * ``` */ export declare const parseAST: ParseAST; /** * Parses a Klog source string into an array of `Record` classes. * @param source - The Klog source string to parse. * @example * ``` * const source = ` * 2021-06-20 * 08:00 - 15:00 Work * -1h Lunch * `; * const records = parse(source); * console.log(records); * // [ * // Record { * // date: new Date('2021-06-20'), * // entries: [ * // new Entry(new Range(new Time(8, 0), new Time(15, 0)), new Summary("Work")), * // new Entry(new Duration(-1, 0), new Summary("Lunch")) * // ], * // summary: null, * // shouldTotal: null, * // dateFormat: RecordDateFormat.Dashes * // } * // ] * ``` */ export declare const parse: (source: string) => Record[]; export {};