Skip to content

Instantly share code, notes, and snippets.

@nickfun
Created October 26, 2022 21:21
Show Gist options
  • Save nickfun/672fadb5f9bc5f3b0a0038252831f997 to your computer and use it in GitHub Desktop.
Save nickfun/672fadb5f9bc5f3b0a0038252831f997 to your computer and use it in GitHub Desktop.
typescript todo.sh parser playground
const ex: string = "x 2022-01-01 2022-10-04 say hello to mom category:family when i visit later";
interface Todo {
done: boolean,
doneDate?: string,
startDate?: string,
body: string,
meta: { [name: string]: string}
}
/*
0 looking at done flag
1 looking at end date
2 looking at start date
3 looking at body
*/
function parse(input: string): Todo {
const tokens = input.split(" ");
var state = 0
const todo: Todo = {
done: false,
doneDate: "",
startDate: "",
body: "",
meta: {}
}
for( var i =0; i < tokens.length; i++) {
const t = tokens[i];
switch(state) {
case 0: {
if (t == "x") {
todo.done = true;
state = 1;
} else {
state = 2;
i--;
}
break;
}
case 1: {
if (isDate(t)) {
todo.doneDate = t;
state = 2;
} else {
state = 3;
i--;
}
break;
}
case 2: {
if (isDate(t)) {
todo.startDate = t;
state = 3;
} else {
state = 3;
i--;
}
break;
}
case 3: {
if (t.includes(":")) {
const parts = t.split(":");
todo.meta[ parts[0] ] = parts[1];
}
todo.body = todo.body + " " + t
break;
}
}
}
return todo;
}
function isDate(d: string): boolean {
const r = /\d-\d-\d/;
const result = (d.startsWith("2022") || d.startsWith("2021") || d.startsWith("2022"))
// console.log(d, result);
return result;
}
[
'x 2022-01-01 2021-12-12 hello world',
'go to the store',
'eat more category:healthy',
'2021-23-12 pick up laundry',
ex
].forEach(function (s: string) {
console.log(parse(s));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment