fix: Show tasks completion on document list items (#2342)

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
Saumya Pandey
2021-07-27 11:31:27 +05:30
committed by GitHub
parent 8ee018a759
commit a81fbd8608
11 changed files with 247 additions and 4 deletions

21
shared/utils/getTasks.js Normal file
View File

@ -0,0 +1,21 @@
// @flow
const CHECKBOX_REGEX = /\[(X|\s|_|-)\]\s(.*)?/gi;
export default function getTasks(text: string) {
const matches = [...text.matchAll(CHECKBOX_REGEX)];
let total = matches.length;
if (!total) {
return {
completed: 0,
total: 0,
};
} else {
const notCompleted = matches.reduce(
(accumulator, match) =>
match[1] === " " ? accumulator + 1 : accumulator,
0
);
return { completed: total - notCompleted, total };
}
}