Skip to content

Commit

Permalink
fix: Serialization of default values in createSerializer (#642)
Browse files Browse the repository at this point in the history
* fix: Handle `clearOnDefault` in `createSerializer`

* chore: Fix types

---------

Co-authored-by: Francois Best <github@francoisbest.com>
  • Loading branch information
hugotiger and franky47 authored Sep 23, 2024
1 parent 9dc148f commit a1aa096
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
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

0 comments on commit a1aa096

Please sign in to comment.