1
1
[ ![ Build Status] ( https://travis-ci.org/georapbox/webStorage.svg?branch=master )] ( https://travis-ci.org/georapbox/webStorage ) [ ![ Dependencies] ( https://david-dm.org/georapbox/webStorage.svg?theme=shields.io )] ( https://david-dm.org/georapbox/webStorage ) [ ![ devDependency Status] ( https://david-dm.org/georapbox/webStorage/dev-status.svg )] ( https://david-dm.org/georapbox/webStorage#info=devDependencies )
2
2
3
- * Distributable files can be found under [ releases] ( https://github.com/georapbox/webStorage/releases ) .*
4
-
5
3
# webStorage
6
4
7
- webStorage is a minimal Javascript library that improves the way you work with ``` localStorage ``` or ``` sessionStorage ``` .
5
+ webStorage is a minimal Javascript library that improves the way you work with ` localStorage ` or ` sessionStorage ` .
6
+
7
+
8
+ ## Install
9
+
10
+ ### npm
11
+
12
+ ``` bash
13
+ $ npm install webStorage
14
+ ```
15
+
16
+ ### Browser
17
+
18
+ ``` html
19
+ <script src =" node_modules/webStorage/dist/webStorage.min.js" ></script >
20
+ ```
8
21
9
22
10
23
## Data API
@@ -17,7 +30,7 @@ The API methods below deal with getting and setting data in an offline store.
17
30
getItem (key)
18
31
```
19
32
20
- Gets an item from an offline store. If the key does not exist, ``` getItem() `` ` will return null.
33
+ Gets an item from an offline store. If the key does not exist, ` getItem() ` will return null.
21
34
22
35
### setItem
23
36
@@ -48,9 +61,9 @@ clear([clearAll])
48
61
49
62
> Use this method with caution.
50
63
51
- If ``` clearAll ``` is set to ``` true `` ` , removes every key from the storage, returning it to a blank slate.
64
+ If ` clearAll ` is set to ` true ` , removes every key from the storage, returning it to a blank slate.
52
65
53
- If ``` clearAll ``` is set to ``` false `` ` or any other falsy value it will remove only the keys that belong to the specific databse.
66
+ If ` clearAll ` is set to ` false ` or any other falsy value it will remove only the keys that belong to the specific databse.
54
67
55
68
### keys
56
69
@@ -76,13 +89,13 @@ iterate(iteratorCallback)
76
89
77
90
Iterate over all value/key pairs in datastore.
78
91
79
- < code > iteratorCallback</ code > is called once for each pair, with the following arguments:
92
+ ` iteratorCallback ` is called once for each pair, with the following arguments:
80
93
81
94
- key
82
95
- value
83
96
- iterationNumber (one-based number)
84
97
85
- You can early exit from iterator by returning ``` false ``` inside ``` iteratorCallback `` ` .
98
+ You can early exit from iterator by returning ` false ` inside ` iteratorCallback ` .
86
99
87
100
### quota
88
101
@@ -98,7 +111,7 @@ Approximately display the size for each key in datastore and the total size of a
98
111
supported ()
99
112
```
100
113
101
- Checks if the driver of choice (< code > localStorage</ code > or < code > sessionStorage</ code > ) is supported by the browser.
114
+ Checks if the driver of choice (` localStorage ` or ` sessionStorage ` ) is supported by the browser. It will return ` false ` if storage is full .
102
115
103
116
104
117
## Settings API
@@ -123,101 +136,88 @@ Set and persist webStorage options. This must be called before any other calls t
123
136
createInstance ([options])
124
137
```
125
138
126
- Creates a new instance of the webStorage. The ``` options ``` can be the same as ``` config(options) `` ` .
139
+ Creates a new instance of the webStorage. The ` options ` can be the same as ` config(options) ` .
127
140
128
141
129
142
## Usage Example
130
143
131
144
``` js
132
- var users = [
133
- {id
: 1 , name
: ' John Doe' , email
: ' [email protected] ' },
134
- {id
: 2 , name
: ' George Cooper' , email
: ' [email protected] ' },
135
- {id
: 2 , name
: ' Tim Smith' , email
: ' [email protected] ' }
145
+ const users = [
146
+ {id
: 1 , name
: ' John Doe' , email
: ' [email protected] ' },
147
+ {id
: 2 , name
: ' George Cooper' , email
: ' [email protected] ' },
148
+ {id
: 2 , name
: ' Tim Smith' , email
: ' [email protected] ' }
136
149
];
137
150
138
- var companies = [' Google' , ' Yahoo' , ' Microsoft' , ' Mozilla' ];
151
+ const companies = [' Google' , ' Yahoo' , ' Microsoft' , ' Mozilla' ];
139
152
140
153
/* Saving some items with the default configuration */
141
154
webStorage .setItem (' user' , users[0 ]);
142
155
webStorage .setItem (' company' , companies[0 ]);
143
156
144
157
/* Create a new instance and save some items */
145
- var ls = webStorage .createInstance ({
146
- name: ' MyApp'
158
+ const ls = webStorage .createInstance ({
159
+ name: ' MyApp'
147
160
});
148
161
149
162
ls .setItem (' user' , users[1 ]);
150
163
ls .setItem (' company' , companies[2 ]);
151
164
ls .setItem (' dummyKey' , 100 );
152
165
153
166
/* Retrieving saved items */
154
- webStorage .
getItem (
' user' );
// = > Object { id: 1, name: "John Doe", email: "[email protected] " }
155
- webStorage .getItem (' company' ); // = > "Google"
167
+ webStorage .
getItem (
' user' );
// - > Object { id: 1, name: "John Doe", email: "[email protected] " }
168
+ webStorage .getItem (' company' ); // - > "Google"
156
169
157
- ls .
getItem (
' user' );
// = > Object { id: 2, name: "George Cooper", email: "[email protected] " }
158
- ls .getItem (' company' ); // = > "Microsoft"
170
+ ls .
getItem (
' user' );
// - > Object { id: 2, name: "George Cooper", email: "[email protected] " }
171
+ ls .getItem (' company' ); // - > "Microsoft"
159
172
160
173
/* Get length of datastores */
161
- webStorage .length (); // = > 2
162
- ls .length (); // = > 3
174
+ webStorage .length (); // - > 2
175
+ ls .length (); // - > 3
163
176
164
177
/* Get datastores' keys */
165
- webStorage .keys (); // = > Array [ "company", "user" ]
166
- ls .keys (); // = > Array [ "dummyKey", "company", "user" ]
178
+ webStorage .keys (); // - > Array [ "company", "user" ]
179
+ ls .keys (); // - > Array [ "dummyKey", "company", "user" ]
167
180
168
181
/* Itereate over datastores */
169
182
ls .iterate (function (key , value , iterNum ) {
170
- console .log (iterNum, ' :' , key, ' :' , value);
183
+ console .log (iterNum, ' :' , key, ' :' , value);
171
184
});
172
- // = > 1 : dummyKey : 100
173
- // = > 2 : company : Microsoft
174
- // = > 3 : user : Object { id: 2, name: "George Cooper", email: "[email protected] " }
185
+ // - > 1 : dummyKey : 100
186
+ // - > 2 : company : Microsoft
187
+ // - > 3 : user : Object { id: 2, name: "George Cooper", email: "[email protected] " }
175
188
176
189
/* Quota */
177
190
ls .quota ();
178
- // = > Object { "total": 0.0001430511474609375, "items": { "MyApp/dummyKey": 0.0000057220458984375, "MyApp/company": 0.0000209808349609375, "MyApp/user": 0.0001163482666015625 } }"
191
+ // - > Object { "total": 0.0001430511474609375, "items": { "MyApp/dummyKey": 0.0000057220458984375, "MyApp/company": 0.0000209808349609375, "MyApp/user": 0.0001163482666015625 } }"
179
192
180
193
/* Removing items */
181
194
webStorage .removeItem (' user' );
182
- webStorage .length (); // = > 1
183
- webStorage .keys (); // = > Array [ "company" ]
184
- ls .length (); // = > 3 (still same as before)
195
+ webStorage .length (); // - > 1
196
+ webStorage .keys (); // - > Array [ "company" ]
197
+ ls .length (); // - > 3 (still same as before)
185
198
ls .clear (); /* Clear only the "MyApp" datastore */
186
- ls .length (); // = > 0
187
- ls .keys (); // = > Array []
188
- webStorage .length (); // = > 1
199
+ ls .length (); // - > 0
200
+ ls .keys (); // - > Array []
201
+ webStorage .length (); // - > 1
189
202
ls .clear (true ); /* Flush away everything in localStorage */
190
- webStorage .length (); // = > 0
203
+ webStorage .length (); // - > 0
191
204
```
192
205
193
206
194
- ## Install
195
-
196
- ### npm
207
+ ## Build for development
197
208
198
- ``` sh
199
- $ npm install webStorage
209
+ ``` bash
210
+ $ npm run dev
200
211
```
201
212
202
- ### git
203
-
204
- ``` sh
205
- $ git clone https://github.com/georapbox/webStorage.git
206
- ```
213
+ ## Build for production
207
214
208
- ## Build the project for development
209
-
210
- ### 1. Install dependancies
211
-
212
- ``` sh
213
- $ cd webStorage
214
- $ npm install
215
+ ``` bash
216
+ $ npm run build
215
217
```
216
218
217
- ### 2. Build
219
+ ## Test
218
220
219
- ``` sh
220
- $ npm run build
221
+ ``` bash
222
+ $ npm test
221
223
```
222
-
223
- The command above will create a ``` dist/ ``` folder that contains the library to be used in production. It will also lint the code and run the tests.
0 commit comments