Skip to content

Commit 5ad0c29

Browse files
committed
Initial commit
0 parents  commit 5ad0c29

File tree

10 files changed

+316
-0
lines changed

10 files changed

+316
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
.psc-ide-port

.jscsrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"preset": "grunt",
3+
"disallowSpacesInFunctionExpression": null,
4+
"requireSpacesInFunctionExpression": {
5+
"beforeOpeningRoundBrace": true,
6+
"beforeOpeningCurlyBrace": true
7+
},
8+
"disallowSpacesInAnonymousFunctionExpression": null,
9+
"requireSpacesInAnonymousFunctionExpression": {
10+
"beforeOpeningRoundBrace": true,
11+
"beforeOpeningCurlyBrace": true
12+
},
13+
"disallowSpacesInsideObjectBrackets": null,
14+
"requireSpacesInsideObjectBrackets": "all",
15+
"validateQuoteMarks": "\"",
16+
"requireCurlyBraces": null
17+
}

.jshintrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"bitwise": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"eqnull": true,
6+
"esversion": 3,
7+
"forin": true,
8+
"freeze": true,
9+
"funcscope": true,
10+
"futurehostile": true,
11+
"latedef": true,
12+
"maxcomplexity": 1,
13+
"maxdepth": 1,
14+
"maxparams": 4,
15+
"maxstatements": 1,
16+
"noarg": true,
17+
"nocomma": true,
18+
"nonbsp": true,
19+
"nonew": true,
20+
"predef": ["window", "exports"],
21+
"notypeof": true,
22+
"shadow": true,
23+
"singleGroups": true,
24+
"strict": "global",
25+
"undef": true,
26+
"unused": true
27+
}

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
node_js: node
5+
install:
6+
- npm install
7+
script:
8+
- npm run build

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2016 Ian D. Bollinger
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included
12+
in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# purescript-web-storage
2+
3+
[![Build Status](https://travis-ci.org/ianbollinger/purescript-web-storage.svg?branch=master)](https://travis-ci.org/ianbollinger/purescript-web-storage)
4+
5+
PureScript bindings for the
6+
[web storage API](https://html.spec.whatwg.org/multipage/webstorage.html).
7+
8+
## Installation
9+
10+
```
11+
bower install purescript-web-storage
12+
```
13+
14+
## License
15+
16+
`purescript-web-storage` is licensed under the [MIT license](LICENSE).

bower.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "purescript-web-storage",
3+
"description": "PureScript bindings for the web storage API",
4+
"license": "MIT",
5+
"ignore": [
6+
"**/.*",
7+
"bower_components",
8+
"node_modules",
9+
"output",
10+
"package.json"
11+
],
12+
"keywords": [
13+
"purescript"
14+
],
15+
"authors": [
16+
"Ian D. Bollinger <[email protected]> (https://github.com/ianbollinger)"
17+
],
18+
"homepage": "https://github.com/ianbollinger/purescript-web-storage",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/ianbollinger/purescript-web-storage"
22+
},
23+
"dependencies": {
24+
"purescript-eff": "^1.0.0",
25+
"purescript-nullable": "^1.0.1",
26+
"purescript-prelude": "^1.0.1",
27+
"purescript-exceptions": "^1.0.0",
28+
"purescript-maybe": "^1.0.0"
29+
},
30+
"devDependencies": {
31+
"purescript-aff": "^1.0.0",
32+
"purescript-psci-support": "^1.0.0",
33+
"purescript-test-unit": "^7.0.0"
34+
}
35+
}

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"postinstall": "bower install",
5+
"build": "jshint src && jscs src && pulp build --optimise --to dist/web-storage.js",
6+
"test": "pulp browserify --optimise --include test --main Test.Main --to dist/test.js"
7+
},
8+
"devDependencies": {
9+
"jscs": "^3.0.7",
10+
"jshint": "^2.9.2",
11+
"pulp": "^9.0.1",
12+
"purescript": "^0.9.2",
13+
"purescript-psa": "^0.3.9"
14+
}
15+
}

src/Web/Storage.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2016 Ian D. Bollinger
2+
//
3+
// Licensed under the MIT license <LICENSE or
4+
// http://opensource.org/licenses/MIT>. This file may not be copied, modified,
5+
// or distributed except according to those terms.
6+
7+
"use strict";
8+
9+
exports.local = function () {
10+
return window.localStorage;
11+
};
12+
13+
exports.session = function () {
14+
return window.sessionStorage;
15+
};
16+
17+
exports.length = function (storage) {
18+
return function () {
19+
return storage.length;
20+
};
21+
};
22+
23+
exports.keyForeign = function (index) {
24+
return function (storage) {
25+
return function () {
26+
return storage.key(index);
27+
};
28+
};
29+
};
30+
31+
exports.getItemForeign = function (key) {
32+
return function (storage) {
33+
return function () {
34+
return storage.getItem(key);
35+
};
36+
};
37+
};
38+
39+
exports.setItem = function (key) {
40+
return function (value) {
41+
return function (storage) {
42+
return function () {
43+
storage.setItem(key, value);
44+
};
45+
};
46+
};
47+
};
48+
49+
exports.removeItem = function (key) {
50+
return function (storage) {
51+
return function () {
52+
storage.removeItem(key);
53+
};
54+
};
55+
};
56+
57+
exports.clear = function (storage) {
58+
return function () {
59+
storage.clear();
60+
};
61+
};

src/Web/Storage.purs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
-- Copyright 2016 Ian D. Bollinger
2+
--
3+
-- Licensed under the MIT license <LICENSE or
4+
-- http://opensource.org/licenses/MIT>. This file may not be copied, modified,
5+
-- or distributed except according to those terms.
6+
7+
-- | Provides bindings for the
8+
-- | [web storage API](https://html.spec.whatwg.org/multipage/webstorage.html).
9+
module Web.Storage
10+
( STORAGE
11+
, Storage
12+
, local
13+
, session
14+
, length
15+
, key
16+
, getItem
17+
, setItem
18+
, removeItem
19+
, clear
20+
) where
21+
22+
import Prelude
23+
import Control.Monad.Eff (Eff)
24+
import Control.Monad.Eff.Exception (EXCEPTION)
25+
import Data.Nullable as Nullable
26+
import Data.Nullable (Nullable)
27+
import Data.Maybe (Maybe)
28+
29+
-- | The web storage effect.
30+
foreign import data STORAGE :: !
31+
32+
-- | Provides access to a list of key/value pairs.
33+
foreign import data Storage :: *
34+
35+
-- | Storage that persists between browsing sessions.
36+
foreign import local :: forall eff. Eff (storage :: STORAGE | eff) Storage
37+
38+
-- | Like `local` storage, but is cleared when the browser is closed.
39+
foreign import session :: forall eff. Eff (storage :: STORAGE | eff) Storage
40+
41+
-- | The number of key/value pairs currently in storage.
42+
foreign import length
43+
:: forall eff
44+
. Storage
45+
-> Eff (storage :: STORAGE | eff) Int
46+
47+
-- | `key n` is the name of the *n*th key in storage.
48+
-- |
49+
-- | The order of keys is user-agent defined, but is consistent so long as the
50+
-- | number of keys doesn't change. If *n* is negative or greater than or equal
51+
-- | to the number of key/value pairs in storage, this function returns
52+
-- | `Nothing`.
53+
key
54+
:: forall eff
55+
. Int
56+
-> Storage
57+
-> Eff (storage :: STORAGE | eff) (Maybe String)
58+
key index storage = Nullable.toMaybe <$> keyForeign index storage
59+
60+
foreign import keyForeign
61+
:: forall eff
62+
. Int
63+
-> Storage
64+
-> Eff (storage :: STORAGE | eff) (Nullable String)
65+
66+
-- | The value associated with the given key in storage.
67+
-- |
68+
-- | If the given key does not exist in storage, this function returns
69+
-- | `Nothing`.
70+
getItem
71+
:: forall eff
72+
. String
73+
-> Storage
74+
-> Eff (storage :: STORAGE | eff) (Maybe String)
75+
getItem key' storage = Nullable.toMaybe <$> getItemForeign key' storage
76+
77+
foreign import getItemForeign
78+
:: forall eff
79+
. String
80+
-> Storage
81+
-> Eff (storage :: STORAGE | eff) (Nullable String)
82+
83+
-- | Add a new key/value pair to storage if a pair with the given key doesn't
84+
-- | yet exist; otherwise, replace the existing value with the given key.
85+
-- |
86+
-- | If a previous value is equal to the new value, this function does nothing.
87+
-- | If this function can't set a new value, it throws an exception. Setting
88+
-- | can fail if the user has disabled web storage, or if the storage quota has
89+
-- | been exceeded, for example.
90+
foreign import setItem
91+
:: forall eff
92+
. String
93+
-> String
94+
-> Storage
95+
-> Eff (storage :: STORAGE, err :: EXCEPTION | eff) Unit
96+
97+
-- | Remove the key/value pair with the given key from storage.
98+
-- |
99+
-- | If no pair with that key exists, this function does nothing.
100+
foreign import removeItem
101+
:: forall eff
102+
. String
103+
-> Storage
104+
-> Eff (storage :: STORAGE | eff) Unit
105+
106+
-- | Atomically empty storage of all key/value pairs.
107+
-- |
108+
-- | If there are no key/value pairs, this function does nothing.
109+
foreign import clear
110+
:: forall eff
111+
. Storage
112+
-> Eff (storage :: STORAGE | eff) Unit

0 commit comments

Comments
 (0)