fix: Show tasks completion on document list items (#2342)
Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
21
shared/utils/getTasks.js
Normal file
21
shared/utils/getTasks.js
Normal 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 };
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user