Skip to content

Commit

Permalink
New: ZH new default configuration combining zh_hans and zh_hant
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Jun 1, 2024
1 parent 57676b0 commit c5261b7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 21 deletions.
74 changes: 73 additions & 1 deletion src/locales/zh/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@
export * from "./hant";
import { includeCommonConfiguration } from "../../configurations";
import { Chrono, Configuration, Parser, Refiner } from "../../chrono";
import { ParsingResult, ParsingComponents, ReferenceWithTimezone } from "../../results";
import { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday } from "../../types";
import ExtractTimezoneOffsetRefiner from "../../common/refiners/ExtractTimezoneOffsetRefiner";

import ZHHansCasualDateParser from "./hans/parsers/ZHHansCasualDateParser";
import ZHHansDateParser from "./hans/parsers/ZHHansDateParser";
import ZHHansDeadlineFormatParser from "./hans/parsers/ZHHansDeadlineFormatParser";
import ZHHansRelationWeekdayParser from "./hans/parsers/ZHHansRelationWeekdayParser";
import ZHHansTimeExpressionParser from "./hans/parsers/ZHHansTimeExpressionParser";
import ZHHansWeekdayParser from "./hans/parsers/ZHHansWeekdayParser";

import ZHHantCasualDateParser from "./hant/parsers/ZHHantCasualDateParser";
import ZHHantDateParser from "./hant/parsers/ZHHantDateParser";
import ZHHantDeadlineFormatParser from "./hant/parsers/ZHHantDeadlineFormatParser";
import ZHHantRelationWeekdayParser from "./hant/parsers/ZHHantRelationWeekdayParser";
import ZHHantTimeExpressionParser from "./hant/parsers/ZHHantTimeExpressionParser";
import ZHHantWeekdayParser from "./hant/parsers/ZHHantWeekdayParser";
import ZHHantMergeDateRangeRefiner from "./hant/refiners/ZHHantMergeDateRangeRefiner";
import ZHHantMergeDateTimeRefiner from "./hant/refiners/ZHHantMergeDateTimeRefiner";

export * as hant from "./hant";
export * as hans from "./hans";
export { Chrono, Parser, Refiner, ParsingResult, ParsingComponents, ReferenceWithTimezone };
export { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday };

export const casual = new Chrono(createCasualConfiguration());
export const strict = new Chrono(createConfiguration());

export function parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[] {
return casual.parse(text, ref, option);
}

export function parseDate(text: string, ref?: ParsingReference | Date, option?: ParsingOption): Date {
return casual.parseDate(text, ref, option);
}

/**
* @ignore (to be documented later)
*/
export function createCasualConfiguration(): Configuration {
const option = createConfiguration();
option.parsers.unshift(new ZHHantCasualDateParser());
return option;
}

/**
* @ignore (to be documented later)
*/
export function createConfiguration(): Configuration {
const configuration = includeCommonConfiguration({
parsers: [
new ZHHantDateParser(),
new ZHHansDateParser(),
new ZHHantRelationWeekdayParser(),
new ZHHansRelationWeekdayParser(),
new ZHHantWeekdayParser(),
new ZHHansWeekdayParser(),
new ZHHantTimeExpressionParser(),
new ZHHansTimeExpressionParser(),
new ZHHantDeadlineFormatParser(),
new ZHHansDeadlineFormatParser(),
],
refiners: [new ZHHantMergeDateRangeRefiner(), new ZHHantMergeDateTimeRefiner()],
});

// REMOVE ExtractTimezoneOffsetRefiner
configuration.refiners = configuration.refiners.filter(
(refiner) => !(refiner instanceof ExtractTimezoneOffsetRefiner)
);

return configuration;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hans, "我今天要打游戏", new Date(2012, 7, 10, 12), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hans, "我2016年9月3号要打游戏", new Date(2012, 8 - 1, 10), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hans, "五日内我要通关游戏", new Date(2012, 7, 10), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hans, "我上午6点13分打游戏", new Date(2012, 7, 10), (result) => {
Expand Down Expand Up @@ -47,6 +47,20 @@ test("Test - Single Expression", function () {
const expectDate = new Date(2012, 7, 11, 8);
expect(expectDate.getTime()).toBeCloseTo(resultDate.getTime());
});

testSingleCase(chrono.zh.hans, "早上8点", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("早上8点");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(10);
expect(result.start.get("hour")).toBe(8);

const resultDate = result.start.date();
const expectDate = new Date(2012, 8 - 1, 10, 8);
expect(expectDate.getTime()).toBeCloseTo(resultDate.getTime());
});
});

test("Test - Range Expression", function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hans, "星期四", new Date(2016, 9 - 1, 2), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hant, "雞而家全部都係雞", new Date(2012, 7, 10, 8, 9, 10, 11), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hant, "雞2016年9月3號全部都係雞", new Date(2012, 8 - 1, 10), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hant, "五日內我地有d野做", new Date(2012, 7, 10), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hant, "雞上午6點13分全部都係雞", new Date(2012, 7, 10), (result) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chrono from "../../src";
import { testSingleCase } from "../test_util";
import * as chrono from "../../../src";
import { testSingleCase } from "../../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.zh.hant, "星期四", new Date(2016, 9 - 1, 2), (result) => {
Expand Down
18 changes: 18 additions & 0 deletions test/zh/zh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ test("Test - International compatible", () => {
expect(result.start).toBeDate(new Date(784043130000));
});
});

test("Test - Default Zh setting combine both hans/hant", () => {
testSingleCase(chrono.zh, "明天早上8点", new Date(2012, 8 - 1, 8, 12), (result) => {
expect(result.text).toBe("明天早上8点");
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(9);
expect(result.start.get("hour")).toBe(8);
});

testSingleCase(chrono.zh, "明天早上8點", new Date(2012, 8 - 1, 8, 12), (result) => {
expect(result.text).toBe("明天早上8點");
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(9);
expect(result.start.get("hour")).toBe(8);
});
});

0 comments on commit c5261b7

Please sign in to comment.