klog.js
Version:
A JavaScript implementation of the Klog time tracking file format
56 lines (55 loc) • 1.67 kB
JavaScript
import { Duration } from "./duration.js";
import { Range } from "./range.js";
import { Summary } from "./summary.js";
/**
* A single time entry.
*/
export class Entry {
value;
summary;
/**
* Create a new entry.
*/
constructor(
/** The value of the entry. */
value,
/** Optional summary or tags associated with the entry. */
summary = null) {
this.value = value;
this.summary = summary;
}
/** @internal */
static fromAST = (node) => {
const ValueType = node.value.type === "duration" ? Duration : Range;
return new this(ValueType.fromAST(node.value), node.summary ? new Summary(node.summary) : null);
};
/**
* Convert the entry's value to a duration.
*/
toDuration() {
if (this.value instanceof Duration)
// Return a clone instead of returning a reference
return Duration.fromMinutes(this.value.toMinutes());
else if (this.value.open)
return new Duration(0, 0);
else
return this.value.toDuration();
}
/**
* Convert the entry's value to minutes.
*/
toMinutes() {
return this.value.toMinutes();
}
/**
* Render the entry as a Klog string.
* @param indent - The indentation to use for the entry. May be `null` for no indentation.
*/
toString(indent = " ") {
// TODO: automatically move to next line if over certain length? See what Klog does
const summary = this.summary
? " " + this.summary.toString(indent, false)
: "";
return `${indent || ""}${this.value}${summary}`;
}
}