https://github.com/gdotdesign/elm-storage
Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
https://github.com/gdotdesign/elm-storage
cookie elm localstorage sessionstorage
Last synced: about 1 year ago
JSON representation
Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
- Host: GitHub
- URL: https://github.com/gdotdesign/elm-storage
- Owner: gdotdesign
- Created: 2017-01-09T16:07:18.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-07T15:55:20.000Z (over 9 years ago)
- Last Synced: 2025-03-24T06:51:52.149Z (over 1 year ago)
- Topics: cookie, elm, localstorage, sessionstorage
- Language: Elm
- Homepage:
- Size: 168 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# elm-storage
[](https://travis-ci.org/gdotdesign/elm-storage)

This module provides a unified interface for accessing and modifying
**LocalStorage**, **SessionStorage** and **Cookies**.
## Installation
Add `gdotdesign/elm-storage` to your dependencies:
```json
"dependencies": {
"gdotdesign/elm-storage": "1.0.0 <= v < 2.0.0"
}
```
And install with [elm-github-install](https://github.com/gdotdesign/elm-github-install) using the `elm-install` command.
## Usage
The following functions are available for `Storage.Local`, `Storage.Session`:
```elm
-- Asynchronous
get : String -> Task Error (Maybe String)
set : String -> String -> Task Error ()
remove : String -> Task Error ()
keys : Task Error (List String)
length : Task Error Int
clear : Task Error ()
-- Synchronous
getSync : String -> Result Error (Maybe String)
setSync : String -> String -> Result Error ()
keysSync : () -> Result Error (List String)
removeSync : String -> Result Error ()
length : () -> Result Error Int
clear : () -> Result Error ()
```
and a slightly different version for `Storage.Cookie`:
```elm
-- Asynchronous
set : String -> String -> SetOptions -> Task Error ()
remove : String -> RemoveOptions -> Task Error ()
get : String -> Task Error (Maybe String)
clear : RemoveOptions -> Task Error ()
keys : Task Error (List String)
length : Task Error Int
-- Synchronous
setSync : String -> String -> SetOptions -> Result Error ()
removeSync : String -> RemoveOptions -> Result Error ()
getSync : String -> Result Error (Maybe String)
keysSync : () -> Result Error (List String)
clear : RemoveOptions -> Result Error ()
length : () -> Result Error Int
-- Options
type alias SetOptions =
{ domain : String
, expires : Float
, secure : Bool
, path : String
}
type alias RemoveOptions =
{ domain : String
, path : String
}
```
## Testing
The module contains **steps and assertions** to use with [elm-spec](https://github.com/gdotdesign/elm-spec). Check out the [specs](spec) on how to use them, and here is a quick example:
```elm
import Spec exposing (..)
import Storage.Spec.Local exposing (localStorage)
tests : Node
tests =
describe "Local Storage"
[ it "should be testable"
[ localStorage.clear
, localStorage.set "user" "yoda"
, localStorage.hasItem "user"
, localStorage.valueEquals "user" "yoda"
, localStorage.haveNumberOfItems 1
, localStorage.haveItems ["user"]
]
]
main =
run tests
```