• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wanasit / chrono / 17338155491

30 Aug 2025 02:14AM UTC coverage: 90.812% (+0.01%) from 90.802%
17338155491

push

github

Wanasit Tanakitrungruang
Refactor: Remove dayjs from date/time calculation

1672 of 2119 branches covered (78.91%)

44 of 49 new or added lines in 5 files covered. (89.8%)

1 existing line in 1 file now uncovered.

4942 of 5442 relevant lines covered (90.81%)

390.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.09
/src/common/refiners/ForwardDateRefiner.ts
1
/*
2
    Enforce 'forwardDate' option to on the results. When there are missing component,
3
    e.g. "March 12-13 (without year)" or "Thursday", the refiner will try to adjust the result
4
    into the future instead of the past.
5
*/
6

7
import { ParsingContext, Refiner } from "../../chrono";
8
import { ParsingResult } from "../../results";
9
import * as dates from "../../utils/dates";
96✔
10
import { implySimilarDate } from "../../utils/dates";
96✔
11
import { addDuration } from "../../calculation/duration";
96✔
12

13
export default class ForwardDateRefiner implements Refiner {
96✔
14
    refine(context: ParsingContext, results: ParsingResult[]): ParsingResult[] {
15
        if (!context.option.forwardDate) {
1,811✔
16
            return results;
1,769✔
17
        }
18

19
        results.forEach((result) => {
42✔
20
            let refDate = context.reference.getDateWithAdjustedTimezone();
45✔
21

22
            if (result.start.isOnlyTime() && context.reference.instant > result.start.date()) {
45✔
23
                const refDate = context.reference.getDateWithAdjustedTimezone();
6✔
24
                const refFollowingDay = new Date(refDate);
6✔
25
                refFollowingDay.setDate(refFollowingDay.getDate() + 1);
6✔
26

27
                dates.implySimilarDate(result.start, refFollowingDay);
6✔
28
                context.debug(() => {
6✔
29
                    console.log(
×
30
                        `${this.constructor.name} adjusted ${result} time from the ref date (${refDate}) to the following day (${refFollowingDay})`
31
                    );
32
                });
33
                if (result.end && result.end.isOnlyTime()) {
6✔
34
                    dates.implySimilarDate(result.end, refFollowingDay);
2✔
35
                    if (result.start.date() > result.end.date()) {
2✔
36
                        refFollowingDay.setDate(refFollowingDay.getDate() + 1);
1✔
37
                        dates.implySimilarDate(result.end, refFollowingDay);
1✔
38
                    }
39
                }
40
            }
41

42
            if (result.start.isOnlyWeekdayComponent() && refDate > result.start.date()) {
45✔
43
                let daysToAdd = result.start.get("weekday") - refDate.getDay();
9✔
44
                if (daysToAdd <= 0) {
9✔
45
                    daysToAdd += 7;
9✔
46
                }
47
                refDate = addDuration(refDate, { day: daysToAdd });
9✔
48
                implySimilarDate(result.start, refDate);
9✔
49
                context.debug(() => {
9✔
50
                    console.log(`${this.constructor.name} adjusted ${result} weekday (${result.start})`);
×
51
                });
52

53
                if (result.end && result.end.isOnlyWeekdayComponent()) {
9!
54
                    // Adjust date to the coming week
NEW
55
                    let daysToAdd = result.end.get("weekday") - refDate.getDay();
×
NEW
56
                    if (daysToAdd <= 0) {
×
NEW
57
                        daysToAdd += 7;
×
58
                    }
NEW
59
                    refDate = addDuration(refDate, { day: daysToAdd });
×
NEW
60
                    implySimilarDate(result.end, refDate);
×
61
                    context.debug(() => {
×
62
                        console.log(`${this.constructor.name} adjusted ${result} weekday (${result.end})`);
×
63
                    });
64
                }
65
            }
66

67
            // In case where we know the month, but not which year (e.g. "in December", "25th December"),
68
            // try move to another year (up-to 3 times)
69
            if (result.start.isDateWithUnknownYear() && refDate > result.start.date()) {
45✔
70
                for (let i = 0; i < 3 && refDate > result.start.date(); i++) {
10✔
71
                    result.start.imply("year", result.start.get("year") + 1);
10✔
72
                    context.debug(() => {
10✔
73
                        console.log(`${this.constructor.name} adjusted ${result} year (${result.start})`);
×
74
                    });
75

76
                    if (result.end && !result.end.isCertain("year")) {
10✔
77
                        result.end.imply("year", result.end.get("year") + 1);
4✔
78
                        context.debug(() => {
4✔
79
                            console.log(`${this.constructor.name} adjusted ${result} month (${result.start})`);
×
80
                        });
81
                    }
82
                }
83
            }
84
        });
85

86
        return results;
42✔
87
    }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc