Go's url.Parse 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 { Parse } from 'locutus/golang/url/Parse'.

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

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
1Parse('https://locutus.io:443/a/b?x=1#top'){scheme: 'https', host: 'locutus.io:443', path: '/a/b', rawQuery: 'x=1', fragment: 'top'}
2Parse('/a/b?x=1'){scheme: '', host: '', path: '/a/b', rawQuery: 'x=1', fragment: ''}
3Parse('//locutus.io/path'){scheme: '', host: 'locutus.io', path: '/path', rawQuery: '', fragment: ''}

Notes

  • Returns core URL parts from Go’s net/url.Parse model.

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

type ParsedURL = {
scheme: string
host: string
path: string
rawQuery: string
fragment: string
}

const parseAuthorityAndPath = (value: string): { host: string; path: string } => {
const slashIndex = value.indexOf('/')
if (slashIndex < 0) {
return { host: value, path: '' }
}

return {
host: value.slice(0, slashIndex),
path: value.slice(slashIndex),
}
}

export function Parse(rawurl: string): ParsedURL {
// discuss at: https://locutus.io/golang/url/Parse
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returns core URL parts from Go's net/url.Parse model.
// example 1: Parse('https://locutus.io:443/a/b?x=1#top')
// returns 1: {scheme: 'https', host: 'locutus.io:443', path: '/a/b', rawQuery: 'x=1', fragment: 'top'}
// example 2: Parse('/a/b?x=1')
// returns 2: {scheme: '', host: '', path: '/a/b', rawQuery: 'x=1', fragment: ''}
// example 3: Parse('//locutus.io/path')
// returns 3: {scheme: '', host: 'locutus.io', path: '/path', rawQuery: '', fragment: ''}

let rest = String(rawurl)
let fragment = ''
let rawQuery = ''

const hashIndex = rest.indexOf('#')
if (hashIndex >= 0) {
fragment = rest.slice(hashIndex + 1)
rest = rest.slice(0, hashIndex)
}

const queryIndex = rest.indexOf('?')
if (queryIndex >= 0) {
rawQuery = rest.slice(queryIndex + 1)
rest = rest.slice(0, queryIndex)
}

let scheme = ''
let host = ''
let path = ''

const schemeMatch = rest.match(/^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/)
if (schemeMatch?.[1] !== undefined) {
scheme = schemeMatch[1]
const afterScheme = schemeMatch[2] ?? ''
if (afterScheme.startsWith('//')) {
const authority = parseAuthorityAndPath(afterScheme.slice(2))
host = authority.host
path = authority.path
} else {
path = afterScheme
}
} else if (rest.startsWith('//')) {
const authority = parseAuthorityAndPath(rest.slice(2))
host = authority.host
path = authority.path
} else {
path = rest
}

return {
scheme,
host,
path,
rawQuery,
fragment,
}
}

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