Skip to content

Commit

Permalink
chore: Upgrade ts and improve types/tests of invalid scenarios (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Jul 23, 2023
1 parent 24f0ded commit 6ce349e
Show file tree
Hide file tree
Showing 26 changed files with 1,501 additions and 1,502 deletions.
2 changes: 2 additions & 0 deletions __tests__/date-fns-jalali.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ describe("DateFnsJalali", () => {
expect(utils.getFormatHelperText(utils.formats.keyboardDateTime12h)).toBe(
"yyyy/mm/dd hh:mm (a|p)m"
);

expect(utils.getFormatHelperText("testtest")).toBe("testtest");
});
});
});
29 changes: 29 additions & 0 deletions __tests__/edge-cases.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { utilsTest, formats, TEST_TIMESTAMP } from "./test-utils";

describe("DateTime calculations", () => {
utilsTest("getFormatHelperText - invalid input", (_, utils) => {
if (utils.lib === "luxon") {
return;
}
// if the format provided here is not a format we must simply return as is
// in case user is using a custom specified format text
expect(utils.getFormatHelperText("testtest")).toBe("testtest");
});

utilsTest("isEqual - invalid input", (date, utils) => {
expect(utils.isEqual(null, null)).toBe(true);
expect(utils.isEqual(date, null)).toBe(false);
expect(utils.isEqual(null, date)).toBe(false);
});

utilsTest("parse - invalid input", (_, utils) => {
const result = utils.parse("jf.bioifjwe.-", utils.formats.fullDateTime12h);
expect(utils.isValid(result)).toBe(false);
});

utilsTest("getDiff - invalid input", (date, utils) => {
const result = utils.getDiff(date, "fwe", "minutes");
expect(result).toBe(0);
});

});
16 changes: 16 additions & 0 deletions __tests__/inheritance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IUtils } from "@date-io/core/IUtils";
import DayjsUtils from "../packages/dayjs/src/dayjs-utils";
import { Dayjs } from "dayjs";

class CustomDateTime extends DayjsUtils implements IUtils<Dayjs> {
getYear = () => 2007;
getWeekdays = () => {
const start = this.dayjs().startOf("week");
return [0, 1].map((diff) => this.formatByString(start.add(diff, "day"), "dd"));
};
}

test("Should be possible to override methods of utility classes", () => {
const utils = new CustomDateTime();
expect(utils.getWeekdays()).toEqual(["Su", "Mo"]);
});
12 changes: 6 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.test.ts"],
collectCoverageFrom: ["packages/**/src/**/*"],
globals: {
'ts-jest': {
tsConfig: './__tests__/tsconfig.test.json'
}
}
"ts-jest": {
tsconfig: "./__tests__/tsconfig.test.json",
},
},
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
"dayjs": "^1.8.17",
"full-icu": "^1.3.0",
"husky": "^5.0.9",
"jest": "^25.1.0",
"jest": "29.6.1",
"lerna": "^3.19.0",
"lint-staged": "^10.0.0",
"prettier": "^2.0.1",
"rollup": "^2.0.2",
"rollup-plugin-typescript": "^1.0.1",
"ts-jest": "^25.1.0",
"typescript": "^3.7.2"
"ts-jest": "^29.1.1",
"typescript": "^5.0.0"
},
"husky": {
"hooks": {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/IUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export interface IUtils<TDate extends ExtendableDateType> {

getCurrentLocaleCode(): string;
is12HourCycleInCurrentLocale(): boolean;
/** Returns user readable format (taking into account localized format tokens), useful to render helper text for input (e.g. placeholder). For luxon always returns empty string. */
/**
* Returns user readable format (taking into account localized format tokens), useful to render helper text for input (e.g. placeholder).
* If helper can not be created and **for luxon** always returns empty string.
* */
getFormatHelperText(format: string): string;

isNull(value: TDate | null): boolean;
Expand Down
10 changes: 8 additions & 2 deletions packages/core/dev-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ exports.createRollupConfig = (typescript) => {
format: "esm",
exports: "auto",
},
plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript, target: 'ES6', })],
plugins: [
nodeResolve({ extensions }),
typescriptPlugin({ typescript, target: "ES6" }),
],
},
{
external,
Expand All @@ -26,7 +29,10 @@ exports.createRollupConfig = (typescript) => {
format: "cjs",
exports: "auto",
},
plugins: [nodeResolve({ extensions }), typescriptPlugin({ typescript, target: 'ES6', })],
plugins: [
nodeResolve({ extensions }),
typescriptPlugin({ typescript, target: "ES6" }),
],
},
];
};
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"license": "MIT",
"devDependencies": {
"rollup": "^2.0.2",
"typescript": "^3.7.2"
"typescript": "^5.0.0"
},
"gitHead": "687eba751bd706e7d704a39a7caa3e0afbfa40aa"
}
2 changes: 1 addition & 1 deletion packages/date-fns-jalali/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"devDependencies": {
"date-fns-jalali": "^2.19.0-2",
"rollup": "^2.0.2",
"typescript": "^3.7.2"
"typescript": "^5.0.0"
},
"gitHead": "687eba751bd706e7d704a39a7caa3e0afbfa40aa"
}
59 changes: 35 additions & 24 deletions packages/date-fns-jalali/src/date-fns-jalali-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import startOfWeek from "date-fns-jalali/startOfWeek";
import startOfYear from "date-fns-jalali/startOfYear";
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils";
import isWithinInterval from "date-fns-jalali/isWithinInterval";
// @ts-ignore
import longFormatters from "date-fns-jalali/_lib/format/longFormatters";
import defaultLocale from "date-fns-jalali/locale/fa-IR";

Expand Down Expand Up @@ -116,7 +117,7 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage.
public is12HourCycleInCurrentLocale = () => {
if (this.locale) {
return /a/.test(this.locale.formatLong.time());
return /a/.test(this.locale.formatLong?.time());
}

// By default date-fns-jalali is using fa-IR locale with am/pm enabled
Expand All @@ -127,19 +128,22 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31
const longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
const locale = this.locale || defaultLocale;
return format
.match(longFormatRegexp)
.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === "p" || firstCharacter === "P") {
const longFormatter = longFormatters[firstCharacter];
return longFormatter(token, locale.formatLong, {});
}
return token;
})
.join("")
.replace(/(aaa|aa|a)/g, "(a|p)m")
.toLocaleLowerCase();

return (
format
.match(longFormatRegexp)
?.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === "p" || firstCharacter === "P") {
const longFormatter = longFormatters[firstCharacter];
return longFormatter(token, locale.formatLong, {});
}
return token;
})
.join("")
.replace(/(aaa|aa|a)/g, "(a|p)m")
.toLocaleLowerCase() ?? format
);
};

public getCurrentLocaleCode = () => {
Expand Down Expand Up @@ -179,25 +183,27 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
};

public getDiff = (value: Date, comparing: Date | string, unit?: Unit) => {
// we output 0 if the compare date is string and parsing is not valid
const dateToCompare = this.date(comparing) ?? value;
switch (unit) {
case "years":
return differenceInYears(value, this.date(comparing));
return differenceInYears(value, dateToCompare);
case "quarters":
return differenceInQuarters(value, this.date(comparing));
return differenceInQuarters(value, dateToCompare);
case "months":
return differenceInMonths(value, this.date(comparing));
return differenceInMonths(value, dateToCompare);
case "weeks":
return differenceInWeeks(value, this.date(comparing));
return differenceInWeeks(value, dateToCompare);
case "days":
return differenceInDays(value, this.date(comparing));
return differenceInDays(value, dateToCompare);
case "hours":
return differenceInHours(value, this.date(comparing));
return differenceInHours(value, dateToCompare);
case "minutes":
return differenceInMinutes(value, this.date(comparing));
return differenceInMinutes(value, dateToCompare);
case "seconds":
return differenceInSeconds(value, this.date(comparing));
return differenceInSeconds(value, dateToCompare);
default: {
return differenceInMilliseconds(value, this.date(comparing));
return differenceInMilliseconds(value, dateToCompare);
}
}
};
Expand Down Expand Up @@ -375,7 +381,12 @@ export default class DateFnsJalaliUtils implements IUtils<Date> {
};

public formatNumber = (numberToFormat: string) => {
return numberToFormat.replace(/\d/g, (match) => symbolMap[match]).replace(/,/g, "،");
return (
numberToFormat
// safe to cast since we are matching only digits
.replace(/\d/g, (match) => symbolMap[match as unknown as keyof typeof symbolMap])
.replace(/,/g, "،")
);
};

public getMinutes = (date: Date) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/date-fns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"devDependencies": {
"date-fns": "2.16.1",
"rollup": "^2.0.2",
"typescript": "^3.7.2"
"typescript": "^5.0.0"
},
"gitHead": "687eba751bd706e7d704a39a7caa3e0afbfa40aa"
}
55 changes: 32 additions & 23 deletions packages/date-fns/src/date-fns-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import parseISO from "date-fns/parseISO";
import formatISO from "date-fns/formatISO";
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils";
import isWithinInterval from "date-fns/isWithinInterval";
// @ts-ignore
import longFormatters from "date-fns/_lib/format/longFormatters";
import defaultLocale from "date-fns/locale/en-US";

Expand Down Expand Up @@ -103,7 +104,7 @@ export default class DateFnsUtils implements IUtils<Date> {
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage.
public is12HourCycleInCurrentLocale = () => {
if (this.locale) {
return /a/.test(this.locale.formatLong.time());
return /a/.test(this.locale.formatLong?.time());
}

// By default date-fns is using en-US locale with am/pm enabled
Expand All @@ -114,19 +115,22 @@ export default class DateFnsUtils implements IUtils<Date> {
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31
const longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
const locale = this.locale || defaultLocale;
return format
.match(longFormatRegexp)
.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === "p" || firstCharacter === "P") {
const longFormatter = longFormatters[firstCharacter];
return longFormatter(token, locale.formatLong, {});
}
return token;
})
.join("")
.replace(/(aaa|aa|a)/g, "(a|p)m")
.toLocaleLowerCase();

return (
format
.match(longFormatRegexp)
?.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === "p" || firstCharacter === "P") {
const longFormatter = longFormatters[firstCharacter];
return longFormatter(token, locale.formatLong, {});
}
return token;
})
.join("")
.replace(/(aaa|aa|a)/g, "(a|p)m")
.toLocaleLowerCase() ?? format
);
};

public parseISO = (isoString: string) => {
Expand Down Expand Up @@ -174,25 +178,30 @@ export default class DateFnsUtils implements IUtils<Date> {
};

public getDiff = (value: Date, comparing: Date | string, unit?: Unit) => {
// we output 0 if the compare date is string and parsing is not valid
const dateToCompare = this.date(comparing) ?? value;
if (!this.isValid(dateToCompare)) {
return 0;
}
switch (unit) {
case "years":
return differenceInYears(value, this.date(comparing));
return differenceInYears(value, dateToCompare);
case "quarters":
return differenceInQuarters(value, this.date(comparing));
return differenceInQuarters(value, dateToCompare);
case "months":
return differenceInMonths(value, this.date(comparing));
return differenceInMonths(value, dateToCompare);
case "weeks":
return differenceInWeeks(value, this.date(comparing));
return differenceInWeeks(value, dateToCompare);
case "days":
return differenceInDays(value, this.date(comparing));
return differenceInDays(value, dateToCompare);
case "hours":
return differenceInHours(value, this.date(comparing));
return differenceInHours(value, dateToCompare);
case "minutes":
return differenceInMinutes(value, this.date(comparing));
return differenceInMinutes(value, dateToCompare);
case "seconds":
return differenceInSeconds(value, this.date(comparing));
return differenceInSeconds(value, dateToCompare);
default: {
return differenceInMilliseconds(value, this.date(comparing));
return differenceInMilliseconds(value, dateToCompare);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/dayjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"dayjs": "^1.8.17",
"rollup": "^2.0.2",
"typescript": "^3.7.2"
"typescript": "^5.0.0"
},
"scripts": {
"build": "rollup -c && tsc -p tsconfig.declaration.json"
Expand Down
Loading

0 comments on commit 6ce349e

Please sign in to comment.