Go's url.EncodeQuery in TypeScript

✓ Verified: Go 1.23
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { EncodeQuery } from 'locutus/golang/url/EncodeQuery'.

Or with CommonJS: const { EncodeQuery } = require('locutus/golang/url/EncodeQuery')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1EncodeQuery('b=2&b=3&a=1')'a=1&b=2&b=3'
2EncodeQuery('z=last&a=first')'a=first&z=last'
3EncodeQuery('flag&empty=')'empty=&flag='

Notes

  • Encodes query values with deterministic key ordering, mirroring Go’s url.Values.Encode.

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to Go's url.EncodeQuery looks like.

import { ParseQuery } from './ParseQuery.ts'
import { QueryEscape } from './QueryEscape.ts'

type QueryValue = string | string[]
type QueryValues = Record<string, QueryValue>

const normalizeValues = (input: QueryValues): Record<string, string[]> => {
const out: Record<string, string[]> = {}

for (const key of Object.keys(input)) {
const raw = input[key]
if (Array.isArray(raw)) {
out[key] = raw.map((value) => String(value))
continue
}
out[key] = [String(raw)]
}

return out
}

export function EncodeQuery(values: QueryValues | string): string {
// discuss at: https://locutus.io/golang/url/EncodeQuery
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Encodes query values with deterministic key ordering, mirroring Go's url.Values.Encode.
// example 1: EncodeQuery('b=2&b=3&a=1')
// returns 1: 'a=1&b=2&b=3'
// example 2: EncodeQuery('z=last&a=first')
// returns 2: 'a=first&z=last'
// example 3: EncodeQuery('flag&empty=')
// returns 3: 'empty=&flag='

const parsed: Record<string, string[]> =
typeof values === 'string' ? ParseQuery(values) : normalizeValues(values as QueryValues)

const pairs: string[] = []
const keys = Object.keys(parsed).sort()

for (const key of keys) {
const entries = parsed[key] ?? []
if (entries.length === 0) {
pairs.push(`${QueryEscape(key)}=`)
continue
}

for (const value of entries) {
pairs.push(`${QueryEscape(key)}=${QueryEscape(value)}`)
}
}

return pairs.join('&')
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


Help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Go url functions


Star