URLSearchParams overview

Various functions to aid in working with JavaScript’s URLSearchParams interface. It can be thought of similarly to Map<string, Array<string>>.

Added in v0.2.0


Table of contents


1 Typeclass Instances

Eq

An Eq instance for URLSearchParams in which equivalence is determined without respect to order.

Signature

export declare const Eq: Eq<URLSearchParams>
Eq :: Eq URLSearchParams

Example

import { Eq, fromString as f } from 'fp-ts-std/URLSearchParams'

assert.strictEqual(Eq.equals(f('a=1&b=2&a=3'), f('b=2&a=3&a=1')), true)
assert.strictEqual(Eq.equals(f('a=1&b=2&a=3'), f('a=1&b=2')), false)

Added in v0.18.0

Monoid

A Monoid instance for URLSearchParams in which all key/value pairs are preserved.

Signature

export declare const Monoid: Monoid<URLSearchParams>
Monoid :: Monoid URLSearchParams

Example

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

assert.deepStrictEqual(Monoid.empty, empty)

Added in v0.18.0

Semigroup

A Semigroup instance for URLSearchParams in which all key/value pairs are preserved.

Signature

export declare const Semigroup: SemigroupT<URLSearchParams>
Semigroup :: SemigroupT URLSearchParams

Example

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

const xs = fromString('a=1&b=2&a=3')
const ys = fromString('b=4&c=5')

const f = Semigroup.concat

assert.deepStrictEqual(f(xs, ys), fromString('a=1&a=3&b=4&b=2&c=5'))
assert.deepStrictEqual(f(ys, xs), fromString('b=2&b=4&c=5&a=1&a=3'))

Added in v0.18.0

3 Functions

appendAt

Append a URL parameter in a URLSearchParams.

Signature

export declare const appendAt: (k: string) => (v: string) => Endomorphism<URLSearchParams>
appendAt :: string -> string -> Endomorphism URLSearchParams

Example

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

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

const f = lookup('c')

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

Added in v0.18.0

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

concatBy

Concat two URLSearchParams according to f in case of conflicts. The Array in the return type of f encodes the possibility to set the key once, multiple times, or not at all. Output order is unspecified.

Signature

export declare const concatBy: (
  f: (k: string) => (vs: [NonEmptyArray<string>, NonEmptyArray<string>]) => Array<string>
) => (xs: URLSearchParams) => Endomorphism<URLSearchParams>
concatBy :: (string -> [NonEmptyArray string, NonEmptyArray string] -> Array string) -> URLSearchParams -> Endomorphism URLSearchParams

Example

import { concatBy, fromString, toString } from 'fp-ts-std/URLSearchParams'
import { fst } from 'fp-ts/Tuple'
import { constant } from 'fp-ts/function'

const concatFirst = concatBy(constant(fst))

const xs = fromString('a=1&b=2&a=3')
const ys = fromString('b=1&c=2')

assert.deepStrictEqual(concatFirst(xs)(ys), fromString('a=1&a=3&b=2&c=2'))
assert.deepStrictEqual(concatFirst(ys)(xs), fromString('b=1&c=2&a=1&a=3'))

Added in v0.18.0

deleteAt

Delete all URL parameters with the specified key.

Signature

export declare const deleteAt: (k: string) => Endomorphism<URLSearchParams>
deleteAt :: string -> Endomorphism URLSearchParams

Example

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

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

const f = lookup('a')

assert.deepStrictEqual(f(x), O.some(['b', 'e']))
assert.deepStrictEqual(f(y), O.none)

Added in v0.18.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

fromMap

Convert a Map to a URLSearchParams.

Signature

export declare const fromMap: (x: Map<string, Array<string>>) => URLSearchParams
fromMap :: Map string (Array string) -> URLSearchParams

Example

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

const m: Map<string, Array<string>> = new Map([
  ['a', ['b', 'c']],
  ['d', ['e', 'f']],
])
const s = 'a=b&a=c&d=e&d=f'

assert.deepStrictEqual(fromMap(m), fromString(s))

Added in v0.18.0

fromRecord

Parse a URLSearchParams from a record.

Signature

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

Example

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

const r = { a: ['b', 'c'], d: ['e'] }
const s = 'a=b&a=c&d=e'

assert.deepStrictEqual(fromRecord(r), new URLSearchParams(s))

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

isEmpty

Test if there are any search params.

Signature

export declare const isEmpty: Predicate<URLSearchParams>
isEmpty :: Predicate URLSearchParams

Example

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

assert.strictEqual(isEmpty(new URLSearchParams()), true)
assert.strictEqual(isEmpty(new URLSearchParams({ k: 'v' })), false)

Added in v0.16.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

keys

Get an unsorted, potentially duplicative array of the keys in a URLSearchParams.

Signature

export declare const keys: (x: URLSearchParams) => Array<string>
keys :: URLSearchParams -> Array string

Example

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

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

assert.deepStrictEqual(keys(x), ['a', 'c', 'a'])

Added in v0.18.0

lookup

Attempt to get all matches for a URL parameter from a URLSearchParams.

Signature

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

Example

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

const x = fromString('a=b&c=d1&c=d2')

assert.deepStrictEqual(lookup('a')(x), O.some(['b']))
assert.deepStrictEqual(lookup('c')(x), O.some(['d1', 'd2']))
assert.deepStrictEqual(lookup('e')(x), O.none)

Added in v0.18.0

lookupFirst

Attempt to get the first match for a URL parameter from a URLSearchParams.

Signature

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

Example

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

const x = fromString('a=b&c=d1&c=d2')

assert.deepStrictEqual(lookupFirst('c')(x), O.some('d1'))
assert.deepStrictEqual(lookupFirst('e')(x), O.none)

Added in v0.18.0

singleton

Construct a URLSearchParams from a single key/value pair.

Signature

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

Example

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

assert.deepStrictEqual(singleton('k')('v'), new URLSearchParams({ k: 'v' }))

Added in v0.18.0

size

Get the number of potentially duplicative key/value pairs in a URLSearchParams.

Signature

export declare const size: (x: URLSearchParams) => number
size :: URLSearchParams -> number

Example

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

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

assert.strictEqual(size(x), 3)

Added in v0.18.0

toLeadingString

Like toString, but includes a leading question mark if the params are non-empty.

Signature

export declare const toLeadingString: (xs: URLSearchParams) => string
toLeadingString :: URLSearchParams -> string

Example

import { toString, toLeadingString } from 'fp-ts-std/URLSearchParams'

assert.strictEqual(toString(new URLSearchParams('')), '')
assert.strictEqual(toString(new URLSearchParams('a=b')), 'a=b')

assert.strictEqual(toLeadingString(new URLSearchParams('')), '')
assert.strictEqual(toLeadingString(new URLSearchParams('a=b')), '?a=b')

Added in v0.18.0

toMap

Convert a URLSearchParams to a Map, grouping values by keys.

Signature

export declare const toMap: (x: URLSearchParams) => Map<string, NonEmptyArray<string>>
toMap :: URLSearchParams -> Map string (NonEmptyArray string)

Example

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

const u = fromString('a=1&b=3&a=2&b=4')
const m = new Map([
  ['a', ['1', '2']],
  ['b', ['3', '4']],
])

assert.deepStrictEqual(toMap(u), m)

Added in v0.18.0

toRecord

Convert a URLSearchParams to a record, grouping values by keys.

Signature

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

Example

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

const x = new URLSearchParams('a=b&c=d&a=e')

assert.deepStrictEqual(toRecord(x), { a: ['b', 'e'], c: ['d'] })

Added in v0.17.0

toString

Returns a query string suitable for use in a URL, absent a question mark.

Signature

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

Example

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

const x = new URLSearchParams('a=b&c=d')

assert.strictEqual(toString(x), 'a=b&c=d')

Added in v0.17.0

toTuples

Losslessly convert a URLSearchParams to an array of tuples.

Signature

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

Example

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

const x = new URLSearchParams('a=b&c=d&a=e')

assert.deepStrictEqual(toTuples(x), [
  ['a', 'b'],
  ['c', 'd'],
  ['a', 'e'],
])

Added in v0.17.0

upsertAt

Insert or replace a URL parameter in a URLSearchParams.

Signature

export declare const upsertAt: (k: string) => (v: string) => Endomorphism<URLSearchParams>
upsertAt :: string -> string -> Endomorphism URLSearchParams

Example

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

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

const f = lookupFirst('c')

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

Added in v0.18.0

values

Get a flattened array of all the values in a URLSearchParams.

Signature

export declare const values: (x: URLSearchParams) => Array<string>
values :: URLSearchParams -> Array string

Example

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

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

assert.deepStrictEqual(values(x), ['b', 'd', 'e'])

Added in v0.18.0

5 Zone of Death

getAllForParam

Attempt to get all matches for a URL parameter from a URLSearchParams.

Signature

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

Example

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

const x = fromString('a=b&c=d1&c=d2')

assert.deepStrictEqual(getAllForParam('a')(x), O.some(['b']))
assert.deepStrictEqual(getAllForParam('c')(x), O.some(['d1', 'd2']))
assert.deepStrictEqual(getAllForParam('e')(x), O.none)

Added in v0.16.0

getParam

Attempt to get the first match for 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=d1&c=d2')

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

Added in v0.1.0

setParam

Set a URL parameter in a URLSearchParams.

Signature

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

Example

import { setParam, lookupFirst, 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 = lookupFirst('c')

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

Added in v0.1.0