Skip to content

Commit

Permalink
feat(xLevelSubTasks): add required parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Dec 15, 2024
1 parent 7ebaaed commit ef2a5f2
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/app/features/markdown-checklist/checklist-to-markdown.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { checklistToMarkdown } from './checklist-to-markdown';
import { MarkdownChecklistTask } from './markdown-checklist.model';

describe('checklistToMarkdown()', () => {
const ITEMS: [MarkdownChecklistTask[], string][] = [
[[{ text: 'task', isChecked: false }], '- [ ] task'],
[[{ text: 'task', isChecked: true }], '- [x] task'],
[
[
{ text: 'task', isChecked: true },
{ text: 'whatever sdasd asd sadf asdfa sdfasdf asdf', isChecked: false },
],
'- [x] task\n- [ ] whatever sdasd asd sadf asdfa sdfasdf asdf',
],
];

ITEMS.forEach((item, i): void => {
const [checkItems, text] = item;
it(`should parse expected value #${i}`, () => {
expect(checklistToMarkdown(checkItems)).toEqual(text);
});
});
});
5 changes: 5 additions & 0 deletions src/app/features/markdown-checklist/checklist-to-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MarkdownChecklistTask } from './markdown-checklist.model';

export const checklistToMarkdown = (tasks: MarkdownChecklistTask[]): string => {
return tasks.map((task) => `- [${task.isChecked ? 'x' : ' '}] ${task.text}`).join('\n');
};
39 changes: 39 additions & 0 deletions src/app/features/markdown-checklist/is-markdown-checklist.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
we want to match:
- [x] task
- [ ] tasks
but not:
Some text yeah
- [ ] task
and not:
- [ ] task
Some text yeah
*/

import { isMarkdownChecklist } from './is-markdown-checklist';

describe('isMarkdownChecklist()', () => {
[
// --
'- [ ] task',
'- [x] task another yeah',
'\n- [ ] task\n\n',
].forEach((text, i) => {
it(`should return true for a valid checklist #${i}`, () => {
expect(isMarkdownChecklist(text)).toBe(true);
});
});

[
// --
'some what - [ ] task',
'- [x] task another yeah\nSomewhat',
'Some what yeah\n- [ ] task\n\n',
].forEach((text, i) => {
it(`should return false for a non valid checklist #${i}`, () => {
expect(isMarkdownChecklist(text)).toBe(false);
});
});
});
26 changes: 26 additions & 0 deletions src/app/features/markdown-checklist/is-markdown-checklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
we want to match:
- [x] task
- [ ] tasks
but not:
Some text yeah
- [ ] task
and not:
- [ ] task
Some text yeah
*/

export const isMarkdownChecklist = (text: string): boolean => {
try {
const lines = text.split('\n');
return lines.every(
(it) => it.trim() === '' || it.startsWith('- [x] ') || it.startsWith('- [ ] '),
);
} catch (e) {
console.error('Checklist parsing failed');
console.error(e);
return false;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface MarkdownChecklistTask {
text: string;
isChecked: boolean;
}
24 changes: 24 additions & 0 deletions src/app/features/markdown-checklist/markdown-to-checklist.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { markdownToChecklist } from './markdown-to-checklist';
import { MarkdownChecklistTask } from './markdown-checklist.model';

describe('markdownToChecklist()', () => {
const ITEMS: [string, MarkdownChecklistTask[]][] = [
// --
['- [ ] task', [{ text: 'task', isChecked: false }]],
['- [x] task', [{ text: 'task', isChecked: true }]],
[
'\n- [x] task \n - [ ] whatever sdasd asd sadf asdfa sdfasdf asdf',
[
{ text: 'task', isChecked: true },
{ text: 'whatever sdasd asd sadf asdfa sdfasdf asdf', isChecked: false },
],
],
];

ITEMS.forEach((item, i): void => {
const [text, checkItems] = item;
it(`should parse expected value #${i}`, () => {
expect(markdownToChecklist(text)).toEqual(checkItems);
});
});
});
16 changes: 16 additions & 0 deletions src/app/features/markdown-checklist/markdown-to-checklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MarkdownChecklistTask } from './markdown-checklist.model';

export const markdownToChecklist = (text: string): MarkdownChecklistTask[] => {
const items: MarkdownChecklistTask[] = [];
text.split('\n').forEach((it: string) => {
const t = it.trim();
const isChecked = t.startsWith('- [x] ');
if (isChecked || t.startsWith('- [ ]')) {
items.push({
text: t.replace('- [x] ', '').replace('- [ ]', '').trim(),
isChecked,
});
}
});
return items;
};

0 comments on commit ef2a5f2

Please sign in to comment.