Skip to content

Commit

Permalink
Merge pull request #2 from atvenu/bug/adding-new-task-causes-exception
Browse files Browse the repository at this point in the history
Bug/adding new task causes exception
  • Loading branch information
seigel authored Nov 2, 2023
2 parents 5fe9d4d + 13d46a5 commit 0b37c7e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
14 changes: 13 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29894,12 +29894,24 @@ const generateChangeList = (current, previous, result=[]) => {
const aMatch = previous.find((entry) => {
return entry.text.replaceAll(MARKDOWN_STRIKETHROUGH, "") === checkboxLine.text.replaceAll(MARKDOWN_STRIKETHROUGH, ""); // check from strike
})
if (aMatch !== null) {
if (aMatch !== undefined) {
if (aMatch.text !== checkboxLine.text || aMatch.checked !== checkboxLine.checked) {
result.push(checkboxLine);
}
} else {
result.push({checked: checkboxLine.checked, text: `[added] ${checkboxLine.text}`});
}
});
// check for removed
previous.forEach((checkboxLine) => {
const aMatch = current.find((entry) => {
return entry.text.replaceAll(MARKDOWN_STRIKETHROUGH, "") === checkboxLine.text.replaceAll(MARKDOWN_STRIKETHROUGH, ""); // check from strike
});
if (aMatch === undefined) {
result.push({checked: checkboxLine.checked, text: `[removed] ${checkboxLine.text}`});
}
});
console.log('>>>', JSON.stringify(result));
return result;
};

Expand Down
14 changes: 13 additions & 1 deletion src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ const generateChangeList = (current, previous, result=[]) => {
const aMatch = previous.find((entry) => {
return entry.text.replaceAll(MARKDOWN_STRIKETHROUGH, "") === checkboxLine.text.replaceAll(MARKDOWN_STRIKETHROUGH, ""); // check from strike
})
if (aMatch !== null) {
if (aMatch !== undefined) {
if (aMatch.text !== checkboxLine.text || aMatch.checked !== checkboxLine.checked) {
result.push(checkboxLine);
}
} else {
result.push({checked: checkboxLine.checked, text: `[added] ${checkboxLine.text}`});
}
});
// check for removed
previous.forEach((checkboxLine) => {
const aMatch = current.find((entry) => {
return entry.text.replaceAll(MARKDOWN_STRIKETHROUGH, "") === checkboxLine.text.replaceAll(MARKDOWN_STRIKETHROUGH, ""); // check from strike
});
if (aMatch === undefined) {
result.push({checked: checkboxLine.checked, text: `[removed] ${checkboxLine.text}`});
}
});
console.log('>>>', JSON.stringify(result));
return result;
};

Expand Down
10 changes: 10 additions & 0 deletions src/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ test('finds both of the checkboxes', () => {

const current = [];
const previous = [];
const added = [];
const removed = [];

beforeAll(() => {
extractCheckmarks("something at the start\n-[x] hi there\nsomething in the middle\n-[ ] was here \nsomething at the end", current);
extractCheckmarks("something at the start\n-[x] hi there\nsomething in the middle\n-[x] was here\nsomething at the end", previous);
extractCheckmarks("something at the start\n-[x] hi there\nsomething in the middle\n-[x] was here\n- [ ] new task\nsomething at the end", added);
extractCheckmarks("something at the start\n-[x] hi there\nsomething in the middle\n-[x] was here\nsomething at the end", removed);
});

describe('calculating changes between the bodies', () => {
Expand All @@ -50,4 +54,10 @@ describe('calculating changes between the bodies', () => {
test('finds a change between the edits', ()=>{
expect(generateChangeList(current, previous)).toStrictEqual([{checked: false, text: "was here"}]);
})
test('handles additional task', ()=>{
expect(generateChangeList(added, previous)).toStrictEqual([{checked: false, text: "[added] new task"}]);
})
test('handles removed task', ()=>{
expect(generateChangeList(removed, added)).toStrictEqual([{checked: false, text: "[removed] new task"}]);
})
});

0 comments on commit 0b37c7e

Please sign in to comment.