This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Improve DateParam #82
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,39 +1,54 @@ | ||
| import { ParseError } from '~/fiscal-periods/error' | ||
|
|
||
| export class DateParam { | ||
| constructor(public date: Date | 'latest') {} | ||
| export class DateParamDate { | ||
| constructor(readonly date: Date) {} | ||
|
|
||
| public toString(): string { | ||
| if (this.date instanceof Date) { | ||
| return this.date.toISOString().substring(0, 10) | ||
| } else { | ||
| return this.date | ||
| } | ||
| return this.date.toISOString().substring(0, 10) | ||
| } | ||
|
|
||
| public isLatest(): this is DateParamLatest { | ||
| return false | ||
| } | ||
|
|
||
| public toDate(): Date { | ||
| if (this.date === 'latest') { | ||
| throw new Error('This cannot convert to Date') | ||
| } else { | ||
| return this.date | ||
| } | ||
| return this.date | ||
| } | ||
| } | ||
|
|
||
| export class DateParamLatest { | ||
| constructor(readonly date: 'latest') {} | ||
|
|
||
| public isLatest(): boolean { | ||
| return this.date === 'latest' | ||
| public toString(): string { | ||
| return this.date | ||
| } | ||
|
|
||
| public isLatest(): this is DateParamLatest { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| static parse(str: string): DateParam { | ||
| export type DateParam = DateParamDate | DateParamLatest | ||
|
|
||
| export const DateParam = { | ||
| from(date: Date | 'latest'): DateParam { | ||
| if (date === 'latest') { | ||
| return new DateParamLatest(date) | ||
| } else { | ||
| return new DateParamDate(date) | ||
| } | ||
| }, | ||
| parse(str: string): DateParam { | ||
| str = str.toLowerCase() | ||
| if (str == 'latest') { | ||
| return new DateParam(str) | ||
| return new DateParamLatest(str) | ||
| } | ||
|
|
||
| const matches = str.match(/^(\d{4})-(\d{2})-(\d{2})$/) | ||
| if (matches == undefined) { | ||
| throw new ParseError(`Invalid date format: ${str}`) | ||
| } | ||
|
|
||
| return new DateParam(new Date(str)) | ||
| return new DateParamDate(new Date(str)) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、もうすこしシンプルに書く書き方がある気がするな・・・