This module provides a unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies.
Add gdotdesign/elm-storage
to your dependencies:
"dependencies": {
"gdotdesign/elm-storage": "1.0.0 <= v < 2.0.0"
}
And install with elm-github-install using the elm-install
command.
The following functions are available for Storage.Local
, Storage.Session
:
-- 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
:
-- 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
}
The module contains steps and assertions to use with elm-spec. Check out the specs on how to use them, and here is a quick example:
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