Skip to content

Commit

Permalink
Merge pull request #16 from zolplay-cn/cali
Browse files Browse the repository at this point in the history
fix(utils): add nullish value for parseDateTime
  • Loading branch information
CaliCastle committed Oct 24, 2022
2 parents fb546d3 + 2670013 commit 685a148
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @zolplay/react

## 0.2.4

### Patch Changes

- Updated dependencies
- @zolplay/utils@1.3.3

## 0.2.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zolplay/react",
"version": "0.2.3",
"version": "0.2.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/index.mjs",
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zolplay/utils

## 1.3.3

### Patch Changes

- fix(utils): add nullish value for parseDateTime

## 1.3.2

### Patch Changes
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/__test__/datetime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ describe('datetime utils', () => {

expect(parsed?.toISOString()).toBe('2023-01-01T00:00:00.000Z')
})

test('should handle nullish values', () => {
expect(parseDateTime(null)).toBe(null)
expect(parseDateTime({ date: null })).toBe(null)
expect(parseDateTime(undefined)).toBe(null)
expect(parseDateTime({ date: undefined })).toBe(null)
expect(parseDateTime()).toBe(null)
})
})
})
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zolplay/utils",
"version": "1.3.2",
"version": "1.3.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/index.mjs",
Expand Down
8 changes: 7 additions & 1 deletion packages/utils/src/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import relativeTime from 'dayjs/plugin/relativeTime'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'

import type { Nullish } from '.'

export type DateTime = Dayjs
export type DateTimeProps = {
date: DateTime | null | undefined
Expand Down Expand Up @@ -41,8 +43,12 @@ type ParseOptions = {
* @ref https://day.js.org/docs/en/parse/utc
*/
export const parseDateTime = (
options: string | ParseOptions
options?: Nullish<string> | Nullish<ParseOptions>
): DateTime | null => {
if (!options) {
return null
}

// take either a string or an object
const { date, timezone = null } =
typeof options === 'string' ? { date: options } : options
Expand Down

0 comments on commit 685a148

Please sign in to comment.