|
1 | 1 | -- | This module defines a data type and various functions for interacting
|
2 | 2 | -- | with the `Storage` interface of the Web Storage API.
|
| 3 | +-- | For example: |
| 4 | +-- | |
| 5 | +-- | ```purescript |
| 6 | +-- | import Prelude |
| 7 | +-- | import Effect (Effect) |
| 8 | +-- | import Effect.Console (log, logShow) |
| 9 | +-- | import Web.HTML (window) |
| 10 | +-- | import Web.HTML.Window (localStorage) |
| 11 | +-- | import Web.Storage.Storage (clear, getItem, removeItem, setItem) |
| 12 | +-- | |
| 13 | +-- | main :: Effect Unit |
| 14 | +-- | main = do |
| 15 | +-- | w <- window |
| 16 | +-- | s <- localStorage w |
| 17 | +-- | setItem "this-is-my-key" "Here is my value." s |
| 18 | +-- | v <- getItem "this-is-my-key" s |
| 19 | +-- | logShow v |
| 20 | +-- | |
| 21 | +-- | removeItem "this-is-my-key" s |
| 22 | +-- | v' <- getItem "this-is-my-key" s |
| 23 | +-- | log "It is gone!" |
| 24 | +-- | logShow v' |
| 25 | +-- | |
| 26 | +-- | clear s |
| 27 | +-- | ``` |
| 28 | + |
3 | 29 | module Web.Storage.Storage
|
4 | 30 | ( Storage
|
5 | 31 | , length
|
@@ -33,7 +59,8 @@ foreign import _getItem :: String -> Storage -> Effect (Nullable String)
|
33 | 59 | getItem :: String -> Storage -> Effect (Maybe String)
|
34 | 60 | getItem s = map toMaybe <<< _getItem s
|
35 | 61 |
|
36 |
| --- | Given a key name and a value (in that order), adds that key to the storage or updates its value if it already exists. |
| 62 | +-- | Given a key name and a value (in that order), adds that key to the |
| 63 | +-- | storage or updates its value if it already exists. |
37 | 64 | foreign import setItem :: String -> String -> Storage -> Effect Unit
|
38 | 65 |
|
39 | 66 | -- | Removes the given key from the storage.
|
|
0 commit comments