Skip to content

Commit

Permalink
Merge pull request #15 from zolplay-cn/cali
Browse files Browse the repository at this point in the history
fix(utils): add support for parseDateTime
  • Loading branch information
CaliCastle authored Oct 24, 2022
2 parents a3a02c6 + 59b3deb commit fb546d3
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 11 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.3

### Patch Changes

- Updated dependencies
- @zolplay/utils@1.3.2

## 0.2.2

### 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.2",
"version": "0.2.3",
"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.2

### Patch Changes

- Add support for string as arg for `parseDateTime`

## 1.3.1

### Patch Changes
Expand Down
32 changes: 32 additions & 0 deletions packages/utils/__test__/datetime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, test } from 'vitest'

import { parseDateTime } from '@/datetime'

describe('datetime utils', () => {
describe('parseDateTime', () => {
test('should parse a string', () => {
const parsed = parseDateTime('2021-01-01T10:00:00.000Z')

expect(parsed?.year()).toBe(2021)
expect(parsed?.toISOString()).toBe('2021-01-01T10:00:00.000Z')
expect(parsed?.format('YYYY-MM-DD')).toBe('2021-01-01')
})

test('should parse an object', () => {
const parsed = parseDateTime({ date: '2022-01-01T09:00:00.000Z' })

expect(parsed?.year()).toBe(2022)
expect(parsed?.toISOString()).toBe('2022-01-01T09:00:00.000Z')
expect(parsed?.format('YYYY-MM-DD')).toBe('2022-01-01')
})

test('should parse an object with timezone', () => {
const parsed = parseDateTime({
date: '2023-01-01',
timezone: 'Asia/Tokyo',
})

expect(parsed?.toISOString()).toBe('2023-01-01T00:00:00.000Z')
})
})
})
8 changes: 6 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zolplay/utils",
"version": "1.3.1",
"version": "1.3.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"module": "dist/index.mjs",
Expand All @@ -11,12 +11,16 @@
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"dev": "pnpm run build --watch",
"test": "vitest run",
"test:watch": "vitest",
"lint": "eslint *.ts*"
},
"devDependencies": {
"@zolplay/config": "workspace:*",
"@zolplay/tsconfig": "workspace:*",
"typescript": "^4.5.2"
"typescript": "^4.5.2",
"vite": "^3.1.8",
"vite-tsconfig-paths": "^3.5.2"
},
"dependencies": {
"@types/lodash": "^4.14.186",
Expand Down
10 changes: 6 additions & 4 deletions packages/utils/src/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ type ParseOptions = {
* @ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
* @ref https://day.js.org/docs/en/parse/utc
*/
export const parseDateTime = ({
date,
timezone,
}: ParseOptions): DateTime | null => {
export const parseDateTime = (
options: string | ParseOptions
): DateTime | null => {
// take either a string or an object
const { date, timezone = null } =
typeof options === 'string' ? { date: options } : options
if (!date) {
return null
}
Expand Down
6 changes: 5 additions & 1 deletion packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"extends": "@zolplay/tsconfig/public-base.json",
"compilerOptions": {
"target": "ESNext"
"target": "ESNext",
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
14 changes: 14 additions & 0 deletions packages/utils/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="vitest" />
/// <reference types="vite/client" />

import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [tsconfigPaths()],
test: {
globals: true,
// setupFiles: './tests/setup.ts',
},
})
52 changes: 49 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb546d3

Please sign in to comment.