Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gcanti/fp-ts-local-storage
fp-ts bindings for LocalStorage
https://github.com/gcanti/fp-ts-local-storage
fp-ts functional-programming localstorage typescript
Last synced: 14 days ago
JSON representation
fp-ts bindings for LocalStorage
- Host: GitHub
- URL: https://github.com/gcanti/fp-ts-local-storage
- Owner: gcanti
- License: mit
- Created: 2018-07-25T08:50:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-24T17:08:43.000Z (about 1 year ago)
- Last Synced: 2024-10-22T12:19:25.977Z (19 days ago)
- Topics: fp-ts, functional-programming, localstorage, typescript
- Language: TypeScript
- Homepage: https://gcanti.github.io/fp-ts-local-storage/
- Size: 182 KB
- Stars: 49
- Watchers: 6
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fp-ts - gcanti/fp-ts-local-storage
README
[fp-ts](https://github.com/gcanti/fp-ts) bindings for LocalStorage
# Documentation
- [API Reference](https://gcanti.github.io/fp-ts-local-storage)
**Example**
```ts
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>
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`
```ts
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>
const program = pipe(
tryCatch(setItem('foo', JSON.stringify({ bar: 'baz' })), toError),
chain(() => tryCatch(getItem('foo'), toError))
)
```