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

fix: Serialization of default values in createSerializer #642

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 30 additions & 1 deletion packages/nuqs/src/serializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, test } from 'vitest'
import { parseAsBoolean, parseAsInteger, parseAsString } from './parsers'
import {
parseAsArrayOf,
parseAsBoolean,
parseAsInteger,
parseAsJson,
parseAsString
} from './parsers'
import { createSerializer } from './serializer'

const parsers = {
Expand Down Expand Up @@ -62,4 +68,27 @@ describe('serializer', () => {
const result = serialize('?str=bar&int=-1', { str: 'foo', int: null })
expect(result).toBe('?str=foo')
})
test('clears value when setting the default value when `clearOnDefault` is used', () => {
const serialize = createSerializer({
int: parseAsInteger.withOptions({ clearOnDefault: true }).withDefault(0),
str: parseAsString.withOptions({ clearOnDefault: true }).withDefault(''),
bool: parseAsBoolean
.withOptions({ clearOnDefault: true })
.withDefault(false),
arr: parseAsArrayOf(parseAsString)
.withOptions({ clearOnDefault: true })
.withDefault([]),
json: parseAsJson()
.withOptions({ clearOnDefault: true })
.withDefault({ foo: 'bar' })
})
const result = serialize({
int: 0,
str: '',
bool: false,
arr: [],
json: { foo: 'bar' }
})
expect(result).toBe('')
})
})
9 changes: 7 additions & 2 deletions packages/nuqs/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ type Base = string | URLSearchParams | URL
type Values<Parsers extends Record<string, ParserBuilder<any>>> = Partial<{
[K in keyof Parsers]?: ExtractParserType<Parsers[K]>
}>
type ParserWithOptionalDefault<T> = ParserBuilder<T> & { defaultValue?: T }

export function createSerializer<
Parsers extends Record<string, ParserBuilder<any>>
Parsers extends Record<string, ParserWithOptionalDefault<any>>
>(parsers: Parsers) {
/**
* Generate a query string for the given values.
Expand Down Expand Up @@ -41,7 +42,11 @@ export function createSerializer<
if (!parser || value === undefined) {
continue
}
if (value === null) {
const isMatchingDefault =
parser.defaultValue !== undefined &&
(parser.eq ?? ((a, b) => a === b))(value, parser.defaultValue)

if (value === null || (parser.clearOnDefault && isMatchingDefault)) {
search.delete(key)
} else {
search.set(key, parser.serialize(value))
Expand Down