Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add parseAsISODate #707

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/docs/content/docs/parsers/built-in.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BooleanParserDemo,
StringLiteralParserDemo,
DateISOParserDemo,
DatetimeISOParserDemo,
DateTimestampParserDemo,
JsonParserDemo
} from '@/content/docs/parsers/demos'
Expand Down Expand Up @@ -183,12 +184,22 @@ The query string value will be the **value** of the enum, not its name
There are two parsers that give you a `Date` object, their difference is
on how they encode the value into the query string.

### ISO 8601
### ISO 8601 Datetime

```ts
import { parseAsIsoDateTime } from 'nuqs'
```

<Suspense>
<DatetimeISOParserDemo/>
</Suspense>

### ISO 8601 Date

```ts
import { parseAsIsoDate } from 'nuqs'
```

tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
<Suspense>
<DateISOParserDemo/>
</Suspense>
Expand Down
7 changes: 6 additions & 1 deletion packages/docs/content/docs/parsers/demos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
parseAsFloat,
parseAsHex,
parseAsInteger,
parseAsIsoDate,
parseAsIsoDateTime,
parseAsJson,
parseAsStringLiteral,
Expand Down Expand Up @@ -290,10 +291,14 @@ export function DateParserDemo({
)
}

export function DateISOParserDemo() {
export function DatetimeISOParserDemo() {
return <DateParserDemo queryKey="iso" parser={parseAsIsoDateTime} />
}

export function DateISOParserDemo() {
return <DateParserDemo queryKey="iso" parser={parseAsIsoDate} />
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
}

export function DateTimestampParserDemo() {
return <DateParserDemo queryKey="ts" parser={parseAsTimestamp} />
}
Expand Down
11 changes: 11 additions & 0 deletions packages/nuqs/src/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
parseAsHex,
parseAsInteger,
parseAsIsoDateTime,
parseAsIsoDate,
parseAsString,
parseAsTimestamp
} from './parsers'
Expand Down Expand Up @@ -48,6 +49,16 @@ describe('parsers', () => {
ref
)
})
test('parseAsIsoDate', () => {
expect(parseAsIsoDate.parse('')).toBeNull()
expect(parseAsIsoDate.parse('not-a-date')).toBeNull()
const moment = '2020-01-01'
const ref = new Date(moment)
expect(parseAsIsoDate.parse(moment)).toStrictEqual(ref)
expect(parseAsIsoDate.parse(moment.slice(0, 10))).toStrictEqual(ref)
expect(parseAsIsoDate.serialize(ref).length).toBe(10)
expect(parseAsIsoDate.serialize(ref).length).not.toContain('T')
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
})
test('parseAsArrayOf', () => {
const parser = parseAsArrayOf(parseAsString)
expect(parser.serialize([])).toBe('')
Expand Down
16 changes: 16 additions & 0 deletions packages/nuqs/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ export const parseAsIsoDateTime = createParser({
serialize: (v: Date) => v.toISOString()
})

/**
* Querystring encoded as an ISO-8601 string (UTC)
* without the time zone offset, and returned as
* a Date object.
*/
export const parseAsIsoDate = createParser({
parse: v => {
const date = new Date(v + 'T00:00:00.000Z')
tylersayshi marked this conversation as resolved.
Show resolved Hide resolved
if (Number.isNaN(date.valueOf())) {
return null
}
return date
},
serialize: (v: Date) => v.toISOString().slice(0, 10)
})

/**
* String-based enums provide better type-safety for known sets of values.
* You will need to pass the parseAsStringEnum function a list of your enum values
Expand Down