Link Search Menu Expand Document

URLSearchParams overview

Various functions to aid in working with JavaScript’s URLSearchParams interface.

Added in v0.2.0


Table of contents


utils

clone

Clone a URLSearchParams.

Signature

export declare const clone: (x: URLSearchParams) => URLSearchParams
clone :: URLSearchParams -> URLSearchParams

Example

import { clone, fromString } from 'fp-ts-std/URLSearchParams'

const x = fromString('a=b&c=d')

assert.strictEqual(x === clone(x), false)
assert.deepStrictEqual(x, clone(x))

Added in v0.2.0

empty

An empty URLSearchParams.

Signature

export declare const empty: URLSearchParams
empty :: URLSearchParams

Example

import { empty } from 'fp-ts-std/URLSearchParams'

assert.deepStrictEqual(empty, new URLSearchParams())

Added in v0.2.0

fromRecord

Parse a URLSearchParams from a record.

Signature

export declare const fromRecord: (x: Record<string, string>) => URLSearchParams
fromRecord :: Record string string -> URLSearchParams

Example

import { fromRecord } from 'fp-ts-std/URLSearchParams'

const x = { a: 'b', c: 'd' }

assert.deepStrictEqual(fromRecord(x), new URLSearchParams(x))

Added in v0.2.0

fromString

Parse a URLSearchParams from a string.

Signature

export declare const fromString: (x: string) => URLSearchParams
fromString :: string -> URLSearchParams

Example

import { fromString } from 'fp-ts-std/URLSearchParams'

const x = 'a=b&c=d'

assert.deepStrictEqual(fromString(x), new URLSearchParams(x))

Added in v0.2.0

fromTuples

Parse a URLSearchParams from an array of tuples.

Signature

export declare const fromTuples: (x: Array<[string, string]>) => URLSearchParams
fromTuples :: Array [string, string] -> URLSearchParams

Example

import { fromTuples } from 'fp-ts-std/URLSearchParams'

const x: Array<[string, string]> = [
  ['a', 'b'],
  ['c', 'd'],
]

assert.deepStrictEqual(fromTuples(x), new URLSearchParams(x))

Added in v0.2.0

getParam

Attempt to get a URL parameter from a URLSearchParams.

Signature

export declare const getParam: (k: string) => (ps: URLSearchParams) => Option<string>
getParam :: string -> URLSearchParams -> Option string

Example

import { getParam, fromString } from 'fp-ts-std/URLSearchParams'
import * as O from 'fp-ts/Option'

const x = fromString('a=b&c=d')

assert.deepStrictEqual(getParam('c')(x), O.some('d'))
assert.deepStrictEqual(getParam('e')(x), O.none)

Added in v0.1.0

isURLSearchParams

Refine a foreign value to URLSearchParams.

Signature

export declare const isURLSearchParams: Refinement<unknown, URLSearchParams>
isURLSearchParams :: Refinement unknown URLSearchParams

Example

import { isURLSearchParams, fromString } from 'fp-ts-std/URLSearchParams'

const x = fromString('a=b&c=d')

assert.deepStrictEqual(isURLSearchParams(x), true)
assert.deepStrictEqual(isURLSearchParams({ not: { a: 'urlsearchparams' } }), false)

Added in v0.1.0

setParam

Set a URL parameter in a URLSearchParams. This does not mutate the input.

Signature

export declare const setParam: (k: string) => (v: string) => (x: URLSearchParams) => URLSearchParams
setParam :: string -> string -> URLSearchParams -> URLSearchParams

Example

import { setParam, getParam, fromString } from 'fp-ts-std/URLSearchParams'
import * as O from 'fp-ts/Option'

const x = fromString('a=b&c=d')
const y = setParam('c')('e')(x)

const f = getParam('c')

assert.deepStrictEqual(f(x), O.some('d'))
assert.deepStrictEqual(f(y), O.some('e'))

Added in v0.1.0