-
-
Notifications
You must be signed in to change notification settings - Fork 983
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(xLevelSubTasks): add required parsers
- Loading branch information
1 parent
7ebaaed
commit ef2a5f2
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/app/features/markdown-checklist/checklist-to-markdown.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/app/features/markdown-checklist/is-markdown-checklist.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/app/features/markdown-checklist/is-markdown-checklist.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
src/app/features/markdown-checklist/markdown-checklist.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
src/app/features/markdown-checklist/markdown-to-checklist.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/app/features/markdown-checklist/markdown-to-checklist.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |