-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Support reference following a date
- Loading branch information
Wanasit Tanakitrungruang
committed
Dec 30, 2023
1 parent
f00e9ef
commit fed9f9d
Showing
4 changed files
with
81 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/locales/en/refiners/ENMergeRelativeAfterDateRefiner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { MergingRefiner } from "../../../common/abstractRefiners"; | ||
import { ParsingComponents, ParsingResult, ReferenceWithTimezone } from "../../../results"; | ||
import { parseTimeUnits } from "../constants"; | ||
import { reverseTimeUnits } from "../../../utils/timeunits"; | ||
|
||
function IsPositiveFollowingReference(result: ParsingResult): boolean { | ||
return result.text.match(/^[+-]/i) != null; | ||
} | ||
|
||
function IsNegativeFollowingReference(result: ParsingResult): boolean { | ||
return result.text.match(/^-/i) != null; | ||
} | ||
|
||
/** | ||
* Merges a relative data/time that comes after an absolute date. | ||
* - [2020-02-13] [+2 weeks] | ||
* - [next tuesday] [+10 days] | ||
*/ | ||
export default class ENMergeRelativeAfterDateRefiner extends MergingRefiner { | ||
shouldMergeResults(textBetween: string, currentResult: ParsingResult, nextResult: ParsingResult): boolean { | ||
if (!textBetween.match(/^\s*$/i)) { | ||
return false; | ||
} | ||
|
||
return IsPositiveFollowingReference(nextResult) || IsNegativeFollowingReference(nextResult); | ||
} | ||
|
||
mergeResults(textBetween: string, currentResult: ParsingResult, nextResult: ParsingResult, context): ParsingResult { | ||
let timeUnits = parseTimeUnits(nextResult.text); | ||
if (IsNegativeFollowingReference(nextResult)) { | ||
timeUnits = reverseTimeUnits(timeUnits); | ||
} | ||
|
||
const components = ParsingComponents.createRelativeFromReference( | ||
new ReferenceWithTimezone(currentResult.start.date()), | ||
timeUnits | ||
); | ||
|
||
return new ParsingResult( | ||
currentResult.reference, | ||
currentResult.index, | ||
`${currentResult.text}${textBetween}${nextResult.text}`, | ||
components | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters