index overview

fp-ts bindings for LocalStorage

Example

import * as assert from 'assert'
import { setItem, getItem } from 'fp-ts-local-storage'
import { some } from 'fp-ts/Option'
import { pipe } from 'fp-ts/pipeable'
import { chain } from 'fp-ts/IO'

// const program: IO<Option<string>>
const program = pipe(
  setItem('foo', JSON.stringify({ bar: 'baz' })),
  chain(() => getItem('foo'))
)

assert.deepStrictEqual(program(), some('{"bar":"baz"}'))

Note that localStorage may throw, for example if disabled by the user or with a QuotaExceededError, so you may want to wrap the API call with a tryCatch

import { setItem, getItem } from 'fp-ts-local-storage'
import { chain, tryCatch } from 'fp-ts/IOEither'
import { pipe } from 'fp-ts/pipeable'
import { toError } from 'fp-ts/Either'

// const program: IOEither<Error, Option<string>>
const program = pipe(
  tryCatch(setItem('foo', JSON.stringify({ bar: 'baz' })), toError),
  chain(() => tryCatch(getItem('foo'), toError))
)

Added in v0.2.0


Table of contents


clear

Signature

export declare const clear: IO<void>

Added in v0.2.0

getItem

Signature

export declare function getItem(key: string): IO<Option<string>>

Added in v0.2.0

key

Signature

export declare function key(index: number): IO<Option<string>>

Added in v0.2.0

length

Signature

export declare const length: IO<number>

Added in v0.2.0

removeItem

Signature

export declare function removeItem(key: string): IO<void>

Added in v0.2.0

setItem

Signature

export declare function setItem(key: string, value: string): IO<void>

Added in v0.2.0