Skip to content

Commit

Permalink
Adds Russian locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhal Mikhailov committed Jun 26, 2022
1 parent 73c7da8 commit bdcecb6
Show file tree
Hide file tree
Showing 28 changed files with 1,841 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const chrono = require('chrono-node');
### What's changed in the v2
For Users
* Chrono’s default now handles only international English. While in the previous version, it tried to parse with all known languages.
* The current fully supported languages are `en`, `ja`, `fr`, and `nl` (`de`, `pt`, and `zh.hant` are partially supported).
* The current fully supported languages are `en`, `ja`, `fr`, `nl` and `ru` (`de`, `pt`, and `zh.hant` are partially supported).

For contributors and advanced users
* The project is rewritten in TypeScript
Expand Down
68 changes: 68 additions & 0 deletions src/common/casualReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ export function yesterday(reference: ReferenceWithTimezone): ParsingComponents {
return component;
}

export function theDayBeforeYesterday(reference: ReferenceWithTimezone): ParsingComponents {
let targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
targetDate = targetDate.add(-2, "day");
assignSimilarDate(component, targetDate);
implySimilarTime(component, targetDate);
return component;
}

/**
* The following day with dayjs.assignTheNextDay()
*/
Expand All @@ -44,6 +53,15 @@ export function tomorrow(reference: ReferenceWithTimezone): ParsingComponents {
return component;
}

export function theDayAfterTomorrow(reference: ReferenceWithTimezone): ParsingComponents {
let targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
targetDate = targetDate.add(2, "day");
assignSimilarDate(component, targetDate);
implySimilarTime(component, targetDate);
return component;
}

export function tonight(reference: ReferenceWithTimezone, implyHour = 22): ParsingComponents {
const targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
Expand All @@ -52,3 +70,53 @@ export function tonight(reference: ReferenceWithTimezone, implyHour = 22): Parsi
assignSimilarDate(component, targetDate);
return component;
}

export function lastNight(reference: ReferenceWithTimezone, implyHour = 0): ParsingComponents {
let targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
if (targetDate.hour() < 6) {
targetDate = targetDate.add(-1, "day");
}
assignSimilarDate(component, targetDate);
component.imply("hour", implyHour);
return component;
}

export function evening(reference: ReferenceWithTimezone, implyHour = 20): ParsingComponents {
const component = new ParsingComponents(reference, {});
component.imply("meridiem", Meridiem.PM);
component.imply("hour", implyHour);
return component;
}

export function yesterdayEvening(reference: ReferenceWithTimezone, implyHour = 20): ParsingComponents {
let targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
targetDate = targetDate.add(-1, "day");
assignSimilarDate(component, targetDate);
component.imply("hour", implyHour);
component.imply("meridiem", Meridiem.PM);
return component;
}

export function midnight(reference: ReferenceWithTimezone): ParsingComponents {
const component = new ParsingComponents(reference, {});
component.imply("hour", 0);
component.imply("minute", 0);
component.imply("second", 0);
return component;
}

export function morning(reference: ReferenceWithTimezone, implyHour = 6): ParsingComponents {
const component = new ParsingComponents(reference, {});
component.imply("meridiem", Meridiem.AM);
component.imply("hour", implyHour);
return component;
}

export function noon(reference: ReferenceWithTimezone): ParsingComponents {
const component = new ParsingComponents(reference, {});
component.imply("meridiem", Meridiem.AM);
component.imply("hour", 12);
return component;
}
6 changes: 5 additions & 1 deletion src/common/parsers/AbstractParserWithWordBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export abstract class AbstractParserWithWordBoundaryChecking implements Parser {
private cachedInnerPattern?: RegExp = null;
private cachedPattern?: RegExp = null;

patternLeftBoundary(): string {
return "(\\W|^)";
}

pattern(context: ParsingContext): RegExp {
const innerPattern = this.innerPattern(context);
if (innerPattern == this.cachedInnerPattern) {
return this.cachedPattern;
}

this.cachedPattern = new RegExp(`(\\W|^)${innerPattern.source}`, innerPattern.flags);
this.cachedPattern = new RegExp(`${this.patternLeftBoundary()}${innerPattern.source}`, innerPattern.flags);
this.cachedInnerPattern = innerPattern;
return this.cachedPattern;
}
Expand Down
25 changes: 19 additions & 6 deletions src/common/parsers/AbstractTimeExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { ParsingComponents, ParsingResult } from "../../results";
import { Meridiem } from "../../index";

// prettier-ignore
function primaryTimePattern(primaryPrefix: string, primarySuffix: string) {
function primaryTimePattern(leftBoundary: string, primaryPrefix: string, primarySuffix: string, flags: string) {
return new RegExp(
"(^|\\s|T|\\b)" +
leftBoundary +
`${primaryPrefix}` +
"(\\d{1,4})" +
"(?:" +
"(?:\\.|\\:|\\:)" +
"(?:\\.|:|:)" +
"(\\d{1,2})" +
"(?:" +
"(?:\\:|\\:)" +
"(?::|:)" +
"(\\d{2})" +
"(?:\\.(\\d{1,6}))?" +
")?" +
")?" +
"(?:\\s*(a\\.m\\.|p\\.m\\.|am?|pm?))?" +
`${primarySuffix}`,
"i"
flags
);
}

Expand Down Expand Up @@ -57,6 +57,14 @@ export abstract class AbstractTimeExpressionParser implements Parser {
this.strictMode = strictMode;
}

patternFlags(): string {
return "i";
}

primaryPatternLeftBoundary(): string {
return "(^|\\s|T|\\b)";
}

primarySuffix(): string {
return "(?=\\W|$)";
}
Expand Down Expand Up @@ -394,7 +402,12 @@ export abstract class AbstractTimeExpressionParser implements Parser {
return this.cachedPrimaryTimePattern;
}

this.cachedPrimaryTimePattern = primaryTimePattern(primaryPrefix, primarySuffix);
this.cachedPrimaryTimePattern = primaryTimePattern(
this.primaryPatternLeftBoundary(),
primaryPrefix,
primarySuffix,
this.patternFlags()
);
this.cachedPrimaryPrefix = primaryPrefix;
this.cachedPrimarySuffix = primarySuffix;
return this.cachedPrimaryTimePattern;
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ import * as ja from "./locales/ja";
import * as pt from "./locales/pt";
import * as nl from "./locales/nl";
import * as zh from "./locales/zh";
export { de, fr, ja, pt, nl, zh };
import * as ru from "./locales/ru";
export { de, fr, ja, pt, nl, zh, ru };

/**
* A shortcut for {@link en | chrono.en.strict}
Expand Down
Loading

0 comments on commit bdcecb6

Please sign in to comment.