Skip to content

Commit a10a2a1

Browse files
APimenta4bradymholtCopilot
authored
Add option to trim leading zeros in hours description (#379)
* add new option to trim leading zeros in hours Signed-off-by: AFONSO PIMENTA <afonsocpimenta04@gmail.com> * add new option to README.md Signed-off-by: AFONSO PIMENTA <afonsocpimenta04@gmail.com> * update deprecated chai assert import Signed-off-by: AFONSO PIMENTA <afonsocpimenta04@gmail.com> * Dry up .toString() calls Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: AFONSO PIMENTA <afonsocpimenta04@gmail.com> Co-authored-by: Brady Holt <brady.holt@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 716317f commit a10a2a1

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ An options object can be passed as the second parameter to `cronstrue.toString`.
128128
- **dayOfWeekStartIndexZero: boolean** - Whether to interpret cron expression DOW `1` as Sunday or Monday. (Default: true)
129129
- **monthStartIndexZero: boolean** - Whether to interpret January as `0` or `1`. (Default: false)
130130
- **use24HourTimeFormat: boolean** - If true, descriptions will use a [24-hour clock](https://en.wikipedia.org/wiki/24-hour_clock) (Default: false but some translations will default to true)
131+
- **trimHoursLeadingZero: boolean** - Whether to trim the leading zero that may appear in the hours description; e.g. "02:00 AM" -> "2:00 AM", "08:00" -> "8:00" (Default: false)
131132
- **locale: string** - The locale to use (Default: "en")
132133
- **logicalAndDayFields: boolean** - If true, descriptions for cron expressions with both day of month and day of week specified will follow a logical-AND wording rather than logical-OR wording; e.g. "...between day 11 and 17 of the month, only on Friday" rather than "...between day 11 and 17 of the month, and on Friday" (Default: false)
133134

src/expressionDescriptor.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class ExpressionDescriptor {
2626
* dayOfWeekStartIndexZero = true,
2727
* monthStartIndexZero = false,
2828
* use24HourTimeFormat = false,
29+
* trimHoursLeadingZero = false,
2930
* locale = 'en'
3031
* }={}]
3132
* @returns {string}
@@ -38,6 +39,7 @@ export class ExpressionDescriptor {
3839
dayOfWeekStartIndexZero = true,
3940
monthStartIndexZero = false,
4041
use24HourTimeFormat,
42+
trimHoursLeadingZero = false,
4143
locale = null,
4244
logicalAndDayFields = false,
4345
}: Options = {}
@@ -51,6 +53,7 @@ export class ExpressionDescriptor {
5153
dayOfWeekStartIndexZero: dayOfWeekStartIndexZero,
5254
monthStartIndexZero: monthStartIndexZero,
5355
use24HourTimeFormat: use24HourTimeFormat,
56+
trimHoursLeadingZero: trimHoursLeadingZero,
5457
locale: locale,
5558
logicalAndDayFields: logicalAndDayFields,
5659
};
@@ -706,9 +709,15 @@ export class ExpressionDescriptor {
706709
second = `:${("00" + secondExpression).substring(secondExpression.length)}`;
707710
}
708711

709-
return `${setPeriodBeforeTime ? period : ""}${("00" + hour.toString()).substring(hour.toString().length)}:${(
710-
"00" + minute.toString()
711-
).substring(minute.toString().length)}${second}${!setPeriodBeforeTime ? period : ""}`;
712+
const hourStr = hour.toString();
713+
const paddedHour = ("00" + hourStr).substring(hourStr.length);
714+
const minuteStr = minute.toString();
715+
const paddedMinute = ("00" + minuteStr).substring(minuteStr.length);
716+
const displayHour = this.options.trimHoursLeadingZero ? hourStr : paddedHour;
717+
718+
return `${setPeriodBeforeTime ? period : ""}${displayHour}:${paddedMinute}${second}${
719+
!setPeriodBeforeTime ? period : ""
720+
}`;
712721
}
713722

714723
protected transformVerbosity(description: string, useVerboseFormat: boolean) {

src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface Options {
44
dayOfWeekStartIndexZero?: boolean;
55
monthStartIndexZero?: boolean;
66
use24HourTimeFormat?: boolean;
7+
trimHoursLeadingZero?: boolean;
78
locale?: string | null;
89
/**
910
* The day of month and day of week are logical AND'ed (as opposed to OR'ed).

test/cronstrue.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,35 @@ describe("Cronstrue", function () {
188188
assert.equal(cronstrue.toString(this.test?.title as string), "At 03:30 AM");
189189
});
190190

191+
it("30 10 * * *", function () {
192+
assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 10:30 AM");
193+
});
194+
195+
it("31 10 * * *", function () {
196+
assert.equal(
197+
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
198+
"At 10:31"
199+
);
200+
});
201+
202+
it("29 9 * * *", function () {
203+
assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 9:29 AM");
204+
});
205+
206+
it("30 9 * * *", function () {
207+
assert.equal(
208+
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
209+
"At 9:30"
210+
);
211+
});
212+
213+
it("0 0 * * *", function () {
214+
assert.equal(
215+
cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }),
216+
"At 0:00"
217+
);
218+
});
219+
191220
it("10 11 * * *", function () {
192221
assert.equal(cronstrue.toString(this.test?.title as string, { verbose: true }), "At 11:10 AM, every day");
193222
});

test/i18n.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import "mocha";
22
import cronstrue from "../src/cronstrue-i18n";
3-
import chai = require("chai");
4-
let assert = chai.assert;
3+
import { assert } from "chai";
54

65
describe("i18n", function () {
76
describe("de", function () {

0 commit comments

Comments
 (0)