Skip to content

Commit

Permalink
🐛 Fix Break/End label matching (starts with)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jan 7, 2025
1 parent 88a40ee commit 1317e54
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 318 deletions.
562 changes: 283 additions & 279 deletions README.md

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default class Parser {
this.settings = settings;
this.planItemFactory = new PlanItemFactory(settings);
this.PLAN_START = `# ${this.settings.plannerLabel}`;
const startSafe = this.sanitize(settings.breakLabel);
const breakSafe = this.sanitize(settings.breakLabel);
const endSafe = this.sanitize(settings.endLabel);
this.PLAN_BREAK = new RegExp(`(?<=^|\\s)${startSafe}(?=\\s|$)`);
this.PLAN_END = new RegExp(`(?<=^|\\s)${endSafe}(?=\\s|$)`);
this.PLAN_BREAK = new RegExp(`^${breakSafe}(?=\\b|$)`, "i");
this.PLAN_END = new RegExp(`^${endSafe}(?=\\b|$)`, "i");
}

sanitize(input: string): string {
Expand Down Expand Up @@ -80,8 +80,10 @@ export default class Parser {
// console.log(match);
const value = match;
const text = value.groups.text;
// console.log(text);
const isBreak = this.matches(text, this.PLAN_BREAK);
const isEnd = this.matches(text, this.PLAN_END);

const time = new Date();
time.setHours(Number.parseInt(value.groups.hours));
time.setMinutes(Number.parseInt(value.groups.minutes));
Expand Down Expand Up @@ -122,6 +124,6 @@ export default class Parser {
}

private matches(input: string, regex: RegExp): boolean {
return regex.test(input.trim().toUpperCase());
return regex.test(input.trim());
}
}
5 changes: 2 additions & 3 deletions tests/fixtures/test-keep.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- [x] 8:00 morning stuff
- [-] 9:00 breakfast
- [>] 10:00 meeting
- [ ] 11:00 ☕️ Coffee Break
- [/] 11:00 ☕️ Coffee Break : Reading
- [ ] 12:10 reading
- [ ] 14:00 🛑 Finish

- [ ] 14:00 🛑 Finish : Things
2 changes: 2 additions & 0 deletions tests/fixtures/test-ooo.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
- [x] 8:00 morning stuff

### Middle

- [ ] 11:00 ☕️ Coffee Break
- [ ] 12:10 reading [Markdown](markdown-link)

### Other

- [-] 9:00 breakfast [[wikilink]]
- [>] 10:00 meeting [[wikilink|alias]]
- [ ] 14:00 🛑 Finish
1 change: 0 additions & 1 deletion tests/fixtures/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
- [ ] 13:00 ☕️ COFFEE BREAK
- [ ] 13:10 meeting
- [ ] 14:00 🛑 Finish

83 changes: 53 additions & 30 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Moment from "moment";
Object.defineProperty(window, "moment", { value: Moment });

import { test, expect, describe } from "vitest";
import * as fs from 'fs';
import path from 'path';
import * as fs from "node:fs";
import path from "node:path";

import Parser from '../src/parser';
import { DayPlannerSettings } from '../src/settings';
Expand All @@ -30,8 +30,7 @@ describe('parser', () => {
date.setSeconds(0);

const updatedContent = parser.parseContent(fileContents, summary, date);
const updated = updatedContent.split('\n');
console.log(updated);
const updated = updatedContent.split("\n");

expect(summary.empty).to.be.false;
expect(summary.invalid).to.be.false;
Expand Down Expand Up @@ -108,8 +107,7 @@ describe('parser', () => {
date.setSeconds(0);

const updatedContent = parser.parseContent(fileContents, summary, date);
const updated = updatedContent.split('\n');
console.log(updated);
const updated = updatedContent.split("\n");

expect(summary.empty).to.be.false;
expect(summary.invalid).to.be.false;
Expand All @@ -129,55 +127,69 @@ describe('parser', () => {
expect(updated[summary.items[8].line]).to.eql("- [ ] 14:00 END");
});

test('should keep configured values', async () => {
const fileContents = fs.readFileSync(path.join(__dirname, 'fixtures/test-keep.md')).toString();
test("should keep configured values", async () => {
const fileContents = fs
.readFileSync(path.join(__dirname, "fixtures/test-keep.md"))
.toString();

const settings = new DayPlannerSettings();
settings.preserveValues = '->';
settings.preserveValues = "->";
settings.markCurrent = true;
settings.correctLabels = false;
settings.breakLabel = "☕️ Coffee Break";
settings.endLabel = "🛑 Finish";

const parser = new Parser(settings);
const summary = new PlanSummaryData([], true);
const date = new Date();

date.setHours(12)
date.setMinutes(25)
date.setHours(12);
date.setMinutes(25);
date.setSeconds(0);

const updatedContent = parser.parseContent(fileContents, summary, date);
const updated = updatedContent.split('\n');
console.log(updated);
const updated = updatedContent.split("\n");

expect(summary.empty).to.be.false;
expect(summary.invalid).to.be.false;
expect(summary.items).to.have.lengthOf(6);

expect(summary.items[0].text).to.eql('morning stuff');
expect(summary.items[0].text).to.eql("morning stuff");
expect(summary.items[0].line).to.eql(2);

expect(updated[summary.items[0].line]).to.eql("- [x] 08:00 morning stuff");
expect(updated[summary.items[0].line]).to.eql(
"- [x] 08:00 morning stuff",
);
expect(updated[summary.items[1].line]).to.eql("- [-] 09:00 breakfast");
expect(updated[summary.items[2].line]).to.eql("- [>] 10:00 meeting");
expect(updated[summary.items[3].line]).to.eql("- [x] 11:00 ☕️ Coffee Break");
expect(updated[summary.items[3].line]).to.eql(
"- [x] 11:00 ☕️ Coffee Break : Reading",
);
expect(updated[summary.items[4].line]).to.eql("- [/] 12:10 reading");
expect(updated[summary.items[5].line]).to.eql("- [ ] 14:00 🛑 Finish");
expect(updated[summary.items[5].line]).to.eql(
"- [ ] 14:00 🛑 Finish : Things",
);

expect(summary.items[3].isBreak).to.be.true;
expect(summary.items[5].isEnd).to.be.true;
});

test('preserve out of order', async () => {
const fileContents = fs.readFileSync(path.join(__dirname, 'fixtures/test-ooo.md')).toString();
test("preserve out of order", async () => {
const fileContents = fs
.readFileSync(path.join(__dirname, "fixtures/test-ooo.md"))
.toString();

const settings = new DayPlannerSettings();
settings.preserveValues = '->';
settings.preserveValues = "->";
settings.markCurrent = true;
settings.correctLabels = false;

const parser = new Parser(settings);
const summary = new PlanSummaryData([], true);
const date = new Date();

date.setHours(12)
date.setMinutes(25)
date.setHours(12);
date.setMinutes(25);
date.setSeconds(0);

const updatedContent = parser.parseContent(fileContents, summary, date);
Expand All @@ -186,30 +198,41 @@ describe('parser', () => {
- [x] 08:00 morning stuff
### Middle
- [x] 11:00 ☕️ Coffee Break
- [/] 12:10 reading [Markdown](markdown-link)
### Other
- [-] 09:00 breakfast [[wikilink]]
- [>] 10:00 meeting [[wikilink|alias]]
- [ ] 14:00 🛑 Finish
`);

const updated = updatedContent.split('\n');
console.log(updated);
const updated = updatedContent.split("\n");

expect(summary.empty).to.be.false;
expect(summary.invalid).to.be.false;
expect(summary.items).to.have.lengthOf(6);

expect(summary.items[0].text).to.eql('morning stuff');
expect(summary.items[0].text).to.eql("morning stuff");
expect(summary.items[0].line).to.eql(2);

expect(updated[summary.items[0].line]).to.eql("- [x] 08:00 morning stuff");
expect(updated[summary.items[1].line]).to.eql('- [-] 09:00 breakfast [[wikilink]]');
expect(updated[summary.items[2].line]).to.eql('- [>] 10:00 meeting [[wikilink|alias]]');
expect(updated[summary.items[3].line]).to.eql("- [x] 11:00 ☕️ Coffee Break");
expect(updated[summary.items[4].line]).to.eql('- [/] 12:10 reading [Markdown](markdown-link)');
expect(updated[summary.items[0].line]).to.eql(
"- [x] 08:00 morning stuff",
);
expect(updated[summary.items[1].line]).to.eql(
"- [-] 09:00 breakfast [[wikilink]]",
);
expect(updated[summary.items[2].line]).to.eql(
"- [>] 10:00 meeting [[wikilink|alias]]",
);
expect(updated[summary.items[3].line]).to.eql(
"- [x] 11:00 ☕️ Coffee Break",
);
expect(updated[summary.items[4].line]).to.eql(
"- [/] 12:10 reading [Markdown](markdown-link)",
);
expect(updated[summary.items[5].line]).to.eql("- [ ] 14:00 🛑 Finish");
});
});
1 change: 0 additions & 1 deletion tests/plan-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('plan-data', () => {
describe('PlanItemFactory', () => {
const matchIndex = 1;
const charIndex = 0;
const status = 'x';
const isBreak = false;
const isEnd = false;
const time = new Date('2021-04-11T11:10:00.507Z');
Expand Down

0 comments on commit 1317e54

Please sign in to comment.