-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: asDate support custom format and timezone using tempo
- Loading branch information
Showing
4 changed files
with
104 additions
and
8 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
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 |
---|---|---|
@@ -1,20 +1,67 @@ | ||
import { | ||
applyOffset, | ||
format, | ||
offset, | ||
parse, | ||
type Format, | ||
} from '@formkit/tempo'; | ||
import { Date, type DateOptions } from '@sinclair/typebox'; | ||
import { createTransformer } from './create-transformer'; | ||
|
||
export type { DateOptions }; | ||
|
||
/** | ||
* Create date transformer. Only accept {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format | Date-time string format}. | ||
* @param options - Validation options (see {@link DateOptions}) | ||
* Options for Tempo to parse and format date string. | ||
*/ | ||
export interface TempoOptions { | ||
/** | ||
* The format that should be used to parse and format the date. | ||
* @defaultValue ISO 8601 format | ||
* @see {@link https://tempo.formkit.com/#format-tokens | Tempo Format Tokens} | ||
*/ | ||
format?: Format; | ||
/** | ||
* Timezone for the decode's input and encode's output date string. | ||
* @defaultValue UTC | ||
*/ | ||
timezone?: string; | ||
} | ||
|
||
/** | ||
* Create date transformer. | ||
* Using {@link https://tempo.formkit.com | Tempo} to parse and format date string. | ||
* @param options - {@link TempoOptions} for parsing/formatting and {@link DateOptions} for validation | ||
* @remarks Without format option, asDate expects {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format | ISO 8601 format} | ||
* @example | ||
* ```ts | ||
* Column('createdAt', asDate()); | ||
* Column('createdAt', asDate({ | ||
* format: 'DD/MM/YYYY', | ||
* timezone: 'Asia/Bangkok' | ||
* })); | ||
* ``` | ||
*/ | ||
export function asDate(options?: DateOptions) { | ||
export function asDate(options: TempoOptions & DateOptions = {}) { | ||
const { | ||
format: formatOption, | ||
timezone = 'UTC', | ||
...validateOptions | ||
} = options; | ||
|
||
return createTransformer( | ||
(str) => new global.Date(str), | ||
(date) => date.toISOString(), | ||
Date(options), | ||
(str) => { | ||
const localDate = parse(str, formatOption); | ||
return applyOffset(localDate, offset(localDate, timezone)); | ||
}, | ||
(date) => | ||
format({ | ||
date, | ||
format: formatOption ?? 'YYYY-MM-DDTHH:mm:ss', | ||
tz: timezone, | ||
}) + | ||
(formatOption === undefined | ||
? `.${date.getMilliseconds().toString().padStart(3, '0')}Z` | ||
: ''), | ||
Date(validateOptions), | ||
); | ||
} |
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