diff --git a/README.md b/README.md index 8edbc90..5818e41 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ An options object can be passed as the second parameter to `cronstrue.toString`. - **dayOfWeekStartIndexZero: boolean** - Whether to interpret cron expression DOW `1` as Sunday or Monday. (Default: true) - **monthStartIndexZero: boolean** - Whether to interpret January as `0` or `1`. (Default: false) - **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) +- **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) - **locale: string** - The locale to use (Default: "en") - **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) diff --git a/src/expressionDescriptor.ts b/src/expressionDescriptor.ts index bc41277..f95ac0a 100644 --- a/src/expressionDescriptor.ts +++ b/src/expressionDescriptor.ts @@ -26,6 +26,7 @@ export class ExpressionDescriptor { * dayOfWeekStartIndexZero = true, * monthStartIndexZero = false, * use24HourTimeFormat = false, + * trimHoursLeadingZero = false, * locale = 'en' * }={}] * @returns {string} @@ -38,6 +39,7 @@ export class ExpressionDescriptor { dayOfWeekStartIndexZero = true, monthStartIndexZero = false, use24HourTimeFormat, + trimHoursLeadingZero = false, locale = null, logicalAndDayFields = false, }: Options = {} @@ -51,6 +53,7 @@ export class ExpressionDescriptor { dayOfWeekStartIndexZero: dayOfWeekStartIndexZero, monthStartIndexZero: monthStartIndexZero, use24HourTimeFormat: use24HourTimeFormat, + trimHoursLeadingZero: trimHoursLeadingZero, locale: locale, logicalAndDayFields: logicalAndDayFields, }; @@ -706,9 +709,15 @@ export class ExpressionDescriptor { second = `:${("00" + secondExpression).substring(secondExpression.length)}`; } - return `${setPeriodBeforeTime ? period : ""}${("00" + hour.toString()).substring(hour.toString().length)}:${( - "00" + minute.toString() - ).substring(minute.toString().length)}${second}${!setPeriodBeforeTime ? period : ""}`; + const hourStr = hour.toString(); + const paddedHour = ("00" + hourStr).substring(hourStr.length); + const minuteStr = minute.toString(); + const paddedMinute = ("00" + minuteStr).substring(minuteStr.length); + const displayHour = this.options.trimHoursLeadingZero ? hourStr : paddedHour; + + return `${setPeriodBeforeTime ? period : ""}${displayHour}:${paddedMinute}${second}${ + !setPeriodBeforeTime ? period : "" + }`; } protected transformVerbosity(description: string, useVerboseFormat: boolean) { diff --git a/src/options.ts b/src/options.ts index a273e3e..b3fa77d 100644 --- a/src/options.ts +++ b/src/options.ts @@ -4,6 +4,7 @@ export interface Options { dayOfWeekStartIndexZero?: boolean; monthStartIndexZero?: boolean; use24HourTimeFormat?: boolean; + trimHoursLeadingZero?: boolean; locale?: string | null; /** * The day of month and day of week are logical AND'ed (as opposed to OR'ed). diff --git a/test/cronstrue.ts b/test/cronstrue.ts index 364916c..7014ead 100644 --- a/test/cronstrue.ts +++ b/test/cronstrue.ts @@ -188,6 +188,35 @@ describe("Cronstrue", function () { assert.equal(cronstrue.toString(this.test?.title as string), "At 03:30 AM"); }); + it("30 10 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 10:30 AM"); + }); + + it("31 10 * * *", function () { + assert.equal( + cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }), + "At 10:31" + ); + }); + + it("29 9 * * *", function () { + assert.equal(cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true }), "At 9:29 AM"); + }); + + it("30 9 * * *", function () { + assert.equal( + cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }), + "At 9:30" + ); + }); + + it("0 0 * * *", function () { + assert.equal( + cronstrue.toString(this.test?.title as string, { trimHoursLeadingZero: true, use24HourTimeFormat: true }), + "At 0:00" + ); + }); + it("10 11 * * *", function () { assert.equal(cronstrue.toString(this.test?.title as string, { verbose: true }), "At 11:10 AM, every day"); }); diff --git a/test/i18n.ts b/test/i18n.ts index 5143e09..6c8fd06 100644 --- a/test/i18n.ts +++ b/test/i18n.ts @@ -1,7 +1,6 @@ import "mocha"; import cronstrue from "../src/cronstrue-i18n"; -import chai = require("chai"); -let assert = chai.assert; +import { assert } from "chai"; describe("i18n", function () { describe("de", function () {