chrono-node
Advanced tools
Comparing version 2.2.4 to 2.2.5
@@ -33,9 +33,9 @@ import { ParsingComponents, ParsingResult } from "./results"; | ||
[c in Component]?: number; | ||
}): ParsingComponents; | ||
} | ParsingComponents): ParsingComponents; | ||
createParsingResult(index: number, textOrEndIndex: number | string, startComponents?: { | ||
[c in Component]?: number; | ||
}, endComponents?: { | ||
} | ParsingComponents, endComponents?: { | ||
[c in Component]?: number; | ||
}): ParsingResult; | ||
} | ParsingComponents): ParsingResult; | ||
debug(block: AsyncDebugBlock): void; | ||
} |
@@ -79,2 +79,5 @@ "use strict"; | ||
createParsingComponents(components) { | ||
if (components instanceof results_1.ParsingComponents) { | ||
return components; | ||
} | ||
return new results_1.ParsingComponents(this.refDate, components); | ||
@@ -81,0 +84,0 @@ } |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AbstractTimeExpressionParser = void 0; | ||
const index_1 = require("../../index"); | ||
const dayjs_1 = __importDefault(require("dayjs")); | ||
function primaryTimePattern(primaryPrefix, primarySuffix) { | ||
@@ -64,12 +60,8 @@ return new RegExp("(^|\\s|T)" + | ||
extract(context, match) { | ||
const refDate = dayjs_1.default(context.refDate); | ||
const result = context.createParsingResult(match.index + match[1].length, match[0].substring(match[1].length)); | ||
result.start.imply("day", refDate.date()); | ||
result.start.imply("month", refDate.month() + 1); | ||
result.start.imply("year", refDate.year()); | ||
result.start = this.extractPrimaryTimeComponents(context, match); | ||
if (!result.start) { | ||
const startComponents = this.extractPrimaryTimeComponents(context, match); | ||
if (!startComponents) { | ||
match.index += match[0].length; | ||
return null; | ||
} | ||
const result = context.createParsingResult(match.index + match[1].length, match[0].substring(match[1].length), startComponents); | ||
const remainingText = context.text.substring(match.index + match[0].length); | ||
@@ -76,0 +68,0 @@ const followingPattern = this.getFollowingTimePatternThroughCache(); |
@@ -29,5 +29,6 @@ "use strict"; | ||
const references = __importStar(require("../../../common/casualReferences")); | ||
const PATTERN = /(now|today|tonight|tomorrow|tmr|yesterday|last\s*night)(?=\W|$)/i; | ||
class ENCasualDateParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking { | ||
innerPattern(context) { | ||
return /(now|today|tonight|tomorrow|tmr|yesterday|last\s*night)(?=\W|$)/i; | ||
return PATTERN; | ||
} | ||
@@ -34,0 +35,0 @@ innerExtract(context, match) { |
@@ -10,5 +10,6 @@ "use strict"; | ||
const dayjs_2 = require("../../../utils/dayjs"); | ||
const PATTERN = /(?:this)?\s*(morning|afternoon|evening|night|midnight|noon)(?=\W|$)/i; | ||
class ENCasualTimeParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking { | ||
innerPattern() { | ||
return /(?:this)?\s*(morning|afternoon|evening|night|midnight|noon)(?=\W|$)/i; | ||
return PATTERN; | ||
} | ||
@@ -15,0 +16,0 @@ innerExtract(context, match) { |
@@ -7,5 +7,6 @@ "use strict"; | ||
const timeunits_1 = require("../../../utils/timeunits"); | ||
const PATTERN = new RegExp(`(this|last|past|next|\\+|-)\\s*(${constants_1.TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); | ||
class ENTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking { | ||
innerPattern() { | ||
return new RegExp(`(this|last|past|next|\\+|-)\\s*(${constants_1.TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); | ||
return PATTERN; | ||
} | ||
@@ -12,0 +13,0 @@ innerExtract(context, match) { |
@@ -10,3 +10,2 @@ import { Component, ParsedComponents, ParsedResult } from "./index"; | ||
get(component: Component): number | null; | ||
date(): Date; | ||
isCertain(component: Component): boolean; | ||
@@ -23,4 +22,7 @@ getCertainComponents(): Array<Component>; | ||
isValidDate(): boolean; | ||
toString(): string; | ||
dayjs(): dayjs.Dayjs; | ||
toString(): string; | ||
date(): Date; | ||
private dateWithoutTimezoneAdjustment; | ||
private getTimezoneAdjustmentMinute; | ||
static createRelativeFromRefDate(refDate: Date, fragments: { | ||
@@ -27,0 +29,0 @@ [c in OpUnitType | QUnitType]?: number; |
@@ -38,5 +38,2 @@ "use strict"; | ||
} | ||
date() { | ||
return this.dayjs().toDate(); | ||
} | ||
isCertain(component) { | ||
@@ -89,37 +86,37 @@ return component in this.knownValues; | ||
isValidDate() { | ||
let dateMoment = this.dayjs(); | ||
if (this.isCertain("timezoneOffset")) { | ||
const adjustTimezoneOffset = this.get("timezoneOffset") - dateMoment.utcOffset(); | ||
dateMoment = dateMoment.add(adjustTimezoneOffset, "minute"); | ||
} | ||
if (dateMoment.get("year") != this.get("year")) | ||
const date = this.isCertain("timezoneOffset") ? this.dateWithoutTimezoneAdjustment() : this.date(); | ||
if (date.getFullYear() !== this.get("year")) | ||
return false; | ||
if (dateMoment.get("month") != this.get("month") - 1) | ||
if (date.getMonth() !== this.get("month") - 1) | ||
return false; | ||
if (dateMoment.get("date") != this.get("day")) | ||
if (date.getDate() !== this.get("day")) | ||
return false; | ||
if (this.get("hour") != null && dateMoment.get("hour") != this.get("hour")) | ||
if (this.get("hour") != null && date.getHours() != this.get("hour")) | ||
return false; | ||
if (this.get("minute") != null && dateMoment.get("minute") != this.get("minute")) | ||
if (this.get("minute") != null && date.getMinutes() != this.get("minute")) | ||
return false; | ||
return true; | ||
} | ||
dayjs() { | ||
let result = dayjs_1.default(); | ||
result = result.year(this.get("year")); | ||
result = result.month(this.get("month") - 1); | ||
result = result.date(this.get("day")); | ||
result = result.hour(this.get("hour")); | ||
result = result.minute(this.get("minute")); | ||
result = result.second(this.get("second")); | ||
result = result.millisecond(this.get("millisecond")); | ||
const currentTimezoneOffset = result.utcOffset(); | ||
const targetTimezoneOffset = this.get("timezoneOffset") !== null ? this.get("timezoneOffset") : currentTimezoneOffset; | ||
const adjustTimezoneOffset = targetTimezoneOffset - currentTimezoneOffset; | ||
result = result.add(-adjustTimezoneOffset, "minute"); | ||
return result; | ||
} | ||
toString() { | ||
return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify(this.impliedValues)}}]`; | ||
} | ||
dayjs() { | ||
return dayjs_1.default(this.date()); | ||
} | ||
date() { | ||
const date = this.dateWithoutTimezoneAdjustment(); | ||
return new Date(date.getTime() + this.getTimezoneAdjustmentMinute(date) * 60000); | ||
} | ||
dateWithoutTimezoneAdjustment() { | ||
const date = new Date(this.get("year"), this.get("month") - 1, this.get("day"), this.get("hour"), this.get("minute"), this.get("second"), this.get("millisecond")); | ||
date.setFullYear(this.get("year")); | ||
return date; | ||
} | ||
getTimezoneAdjustmentMinute(date) { | ||
var _a; | ||
date = date !== null && date !== void 0 ? date : new Date(); | ||
const currentTimezoneOffset = -date.getTimezoneOffset(); | ||
const targetTimezoneOffset = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : currentTimezoneOffset; | ||
return currentTimezoneOffset - targetTimezoneOffset; | ||
} | ||
static createRelativeFromRefDate(refDate, fragments) { | ||
@@ -126,0 +123,0 @@ let date = dayjs_1.default(refDate); |
@@ -18,3 +18,3 @@ { | ||
"license": "MIT", | ||
"version": "2.2.4", | ||
"version": "2.2.5", | ||
"directories": { | ||
@@ -30,2 +30,3 @@ "source": "./src", | ||
"@typescript-eslint/parser": "^4.7.0", | ||
"benny": "^3.6.15", | ||
"eslint": "^7.13.0", | ||
@@ -47,2 +48,3 @@ "eslint-config-prettier": "^6.15.0", | ||
"build": "tsc -p tsconfig.build.json", | ||
"benchmark": "npm run build && node ./benchmark/benchmark.js", | ||
"document": "typedoc", | ||
@@ -68,2 +70,5 @@ "prepare": "npm run build", | ||
}, | ||
"ignorePatterns": [ | ||
"benchmark/*.js" | ||
], | ||
"parserOptions": { | ||
@@ -70,0 +75,0 @@ "sourceType": "module", |
@@ -141,3 +141,7 @@ import { ParsingComponents, ParsingResult } from "./results"; | ||
createParsingComponents(components?: { [c in Component]?: number }): ParsingComponents { | ||
createParsingComponents(components?: { [c in Component]?: number } | ParsingComponents): ParsingComponents { | ||
if (components instanceof ParsingComponents) { | ||
return components; | ||
} | ||
return new ParsingComponents(this.refDate, components); | ||
@@ -149,4 +153,4 @@ } | ||
textOrEndIndex: number | string, | ||
startComponents?: { [c in Component]?: number }, | ||
endComponents?: { [c in Component]?: number } | ||
startComponents?: { [c in Component]?: number } | ParsingComponents, | ||
endComponents?: { [c in Component]?: number } | ParsingComponents | ||
): ParsingResult { | ||
@@ -153,0 +157,0 @@ const text = typeof textOrEndIndex === "string" ? textOrEndIndex : this.text.substring(index, textOrEndIndex); |
import { Parser, ParsingContext } from "../../chrono"; | ||
import { ParsingComponents, ParsingResult } from "../../results"; | ||
import { Meridiem } from "../../index"; | ||
import dayjs from "dayjs"; | ||
@@ -74,11 +73,4 @@ // prettier-ignore | ||
extract(context: ParsingContext, match: RegExpMatchArray): ParsingResult { | ||
const refDate = dayjs(context.refDate); | ||
const result = context.createParsingResult(match.index + match[1].length, match[0].substring(match[1].length)); | ||
result.start.imply("day", refDate.date()); | ||
result.start.imply("month", refDate.month() + 1); | ||
result.start.imply("year", refDate.year()); | ||
result.start = this.extractPrimaryTimeComponents(context, match); | ||
if (!result.start) { | ||
const startComponents = this.extractPrimaryTimeComponents(context, match); | ||
if (!startComponents) { | ||
match.index += match[0].length; | ||
@@ -88,2 +80,8 @@ return null; | ||
const result = context.createParsingResult( | ||
match.index + match[1].length, | ||
match[0].substring(match[1].length), | ||
startComponents | ||
); | ||
const remainingText = context.text.substring(match.index + match[0].length); | ||
@@ -90,0 +88,0 @@ const followingPattern = this.getFollowingTimePatternThroughCache(); |
@@ -8,5 +8,7 @@ import { ParsingContext } from "../../../chrono"; | ||
const PATTERN = /(now|today|tonight|tomorrow|tmr|yesterday|last\s*night)(?=\W|$)/i; | ||
export default class ENCasualDateParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(context: ParsingContext): RegExp { | ||
return /(now|today|tonight|tomorrow|tmr|yesterday|last\s*night)(?=\W|$)/i; | ||
return PATTERN; | ||
} | ||
@@ -13,0 +15,0 @@ |
@@ -7,5 +7,7 @@ import { ParsingContext } from "../../../chrono"; | ||
const PATTERN = /(?:this)?\s*(morning|afternoon|evening|night|midnight|noon)(?=\W|$)/i; | ||
export default class ENCasualTimeParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern() { | ||
return /(?:this)?\s*(morning|afternoon|evening|night|midnight|noon)(?=\W|$)/i; | ||
return PATTERN; | ||
} | ||
@@ -12,0 +14,0 @@ |
@@ -7,5 +7,7 @@ import { TIME_UNITS_PATTERN, parseTimeUnits } from "../constants"; | ||
const PATTERN = new RegExp(`(this|last|past|next|\\+|-)\\s*(${TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); | ||
export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(): RegExp { | ||
return new RegExp(`(this|last|past|next|\\+|-)\\s*(${TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); | ||
return PATTERN; | ||
} | ||
@@ -12,0 +14,0 @@ |
@@ -43,6 +43,2 @@ import { Component, ParsedComponents, ParsedResult } from "./index"; | ||
date(): Date { | ||
return this.dayjs().toDate(); | ||
} | ||
isCertain(component: Component): boolean { | ||
@@ -108,13 +104,9 @@ return component in this.knownValues; | ||
isValidDate(): boolean { | ||
let dateMoment = this.dayjs(); | ||
if (this.isCertain("timezoneOffset")) { | ||
const adjustTimezoneOffset = this.get("timezoneOffset") - dateMoment.utcOffset(); | ||
dateMoment = dateMoment.add(adjustTimezoneOffset, "minute"); | ||
} | ||
const date = this.isCertain("timezoneOffset") ? this.dateWithoutTimezoneAdjustment() : this.date(); | ||
if (dateMoment.get("year") != this.get("year")) return false; | ||
if (dateMoment.get("month") != this.get("month") - 1) return false; | ||
if (dateMoment.get("date") != this.get("day")) return false; | ||
if (this.get("hour") != null && dateMoment.get("hour") != this.get("hour")) return false; | ||
if (this.get("minute") != null && dateMoment.get("minute") != this.get("minute")) return false; | ||
if (date.getFullYear() !== this.get("year")) return false; | ||
if (date.getMonth() !== this.get("month") - 1) return false; | ||
if (date.getDate() !== this.get("day")) return false; | ||
if (this.get("hour") != null && date.getHours() != this.get("hour")) return false; | ||
if (this.get("minute") != null && date.getMinutes() != this.get("minute")) return false; | ||
@@ -124,28 +116,37 @@ return true; | ||
toString() { | ||
return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify( | ||
this.impliedValues | ||
)}}]`; | ||
} | ||
dayjs() { | ||
let result = dayjs(); | ||
return dayjs(this.date()); | ||
} | ||
result = result.year(this.get("year")); | ||
result = result.month(this.get("month") - 1); | ||
result = result.date(this.get("day")); | ||
result = result.hour(this.get("hour")); | ||
result = result.minute(this.get("minute")); | ||
result = result.second(this.get("second")); | ||
result = result.millisecond(this.get("millisecond")); | ||
date(): Date { | ||
const date = this.dateWithoutTimezoneAdjustment(); | ||
return new Date(date.getTime() + this.getTimezoneAdjustmentMinute(date) * 60000); | ||
} | ||
// Javascript Date Object return minus timezone offset | ||
const currentTimezoneOffset = result.utcOffset(); | ||
const targetTimezoneOffset = | ||
this.get("timezoneOffset") !== null ? this.get("timezoneOffset") : currentTimezoneOffset; | ||
private dateWithoutTimezoneAdjustment() { | ||
const date = new Date( | ||
this.get("year"), | ||
this.get("month") - 1, | ||
this.get("day"), | ||
this.get("hour"), | ||
this.get("minute"), | ||
this.get("second"), | ||
this.get("millisecond") | ||
); | ||
const adjustTimezoneOffset = targetTimezoneOffset - currentTimezoneOffset; | ||
result = result.add(-adjustTimezoneOffset, "minute"); | ||
return result; | ||
date.setFullYear(this.get("year")); | ||
return date; | ||
} | ||
toString() { | ||
return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify( | ||
this.impliedValues | ||
)}}]`; | ||
private getTimezoneAdjustmentMinute(date?: Date) { | ||
date = date ?? new Date(); | ||
const currentTimezoneOffset = -date.getTimezoneOffset(); | ||
const targetTimezoneOffset = this.get("timezoneOffset") ?? currentTimezoneOffset; | ||
return currentTimezoneOffset - targetTimezoneOffset; | ||
} | ||
@@ -152,0 +153,0 @@ |
@@ -25,4 +25,3 @@ import * as chrono from "../../src"; | ||
console.log(time); | ||
expect(time).toBeLessThan(1000); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
799257
339
18097
12