Skip to content
This repository was archived by the owner on Jan 11, 2021. It is now read-only.

Commit d5462b0

Browse files
committed
Lint files; Do not throw error on init; Use list folder for bundles
1 parent 5a784b9 commit d5462b0

13 files changed

+102
-88
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/*
2+
src/util/**
3+
dist/**

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.0
2+
- Rewrite using ES2015 syntax.
3+
- Do not throw error on initialization if `localStorage` or `sessionStorage` is not supported or unavailable. User can still use the `supported` method to test for.
4+
15
# 1.0.0
26
### Features
37
- Add `quota()` public API method.

README.md

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
[![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)
22

3-
*Distributable files can be found under [releases](https://github.com/georapbox/webStorage/releases).*
4-
53
# webStorage
64

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+
```
821

922

1023
## Data API
@@ -17,7 +30,7 @@ The API methods below deal with getting and setting data in an offline store.
1730
getItem(key)
1831
```
1932

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.
2134

2235
### setItem
2336

@@ -48,9 +61,9 @@ clear([clearAll])
4861

4962
> Use this method with caution.
5063
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.
5265

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.
5467

5568
### keys
5669

@@ -76,13 +89,13 @@ iterate(iteratorCallback)
7689

7790
Iterate over all value/key pairs in datastore.
7891

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:
8093

8194
- key
8295
- value
8396
- iterationNumber (one-based number)
8497

85-
You can early exit from iterator by returning ```false``` inside ```iteratorCallback```.
98+
You can early exit from iterator by returning `false` inside `iteratorCallback`.
8699

87100
### quota
88101

@@ -98,7 +111,7 @@ Approximately display the size for each key in datastore and the total size of a
98111
supported()
99112
```
100113

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.
102115

103116

104117
## Settings API
@@ -123,101 +136,88 @@ Set and persist webStorage options. This must be called before any other calls t
123136
createInstance([options])
124137
```
125138

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)`.
127140

128141

129142
## Usage Example
130143

131144
```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]'}
136149
];
137150

138-
var companies = ['Google', 'Yahoo', 'Microsoft', 'Mozilla'];
151+
const companies = ['Google', 'Yahoo', 'Microsoft', 'Mozilla'];
139152

140153
/* Saving some items with the default configuration */
141154
webStorage.setItem('user', users[0]);
142155
webStorage.setItem('company', companies[0]);
143156

144157
/* Create a new instance and save some items */
145-
var ls = webStorage.createInstance({
146-
name: 'MyApp'
158+
const ls = webStorage.createInstance({
159+
name: 'MyApp'
147160
});
148161

149162
ls.setItem('user', users[1]);
150163
ls.setItem('company', companies[2]);
151164
ls.setItem('dummyKey', 100);
152165

153166
/* 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"
156169

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"
159172

160173
/* Get length of datastores */
161-
webStorage.length(); // => 2
162-
ls.length(); // => 3
174+
webStorage.length(); // -> 2
175+
ls.length(); // -> 3
163176

164177
/* 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" ]
167180

168181
/* Itereate over datastores */
169182
ls.iterate(function (key, value, iterNum) {
170-
console.log(iterNum, ':', key, ':', value);
183+
console.log(iterNum, ':', key, ':', value);
171184
});
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]" }
175188

176189
/* Quota */
177190
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 } }"
179192

180193
/* Removing items */
181194
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)
185198
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
189202
ls.clear(true); /* Flush away everything in localStorage */
190-
webStorage.length(); // => 0
203+
webStorage.length(); // -> 0
191204
```
192205

193206

194-
## Install
195-
196-
### npm
207+
## Build for development
197208

198-
```sh
199-
$ npm install webStorage
209+
```bash
210+
$ npm run dev
200211
```
201212

202-
### git
203-
204-
```sh
205-
$ git clone https://github.com/georapbox/webStorage.git
206-
```
213+
## Build for production
207214

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
215217
```
216218

217-
### 2. Build
219+
## Test
218220

219-
```sh
220-
$ npm run build
221+
```bash
222+
$ npm test
221223
```
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.

TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
1. Better way to select between localStorage and sessionStorage in configuration.
2-
2. Write more tests.
1+

lib/webStorage.js renamed to dist/webStorage.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/webStorage.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/webStorage.min.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/webStorage.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function (config) {
1414

1515
// list of files / patterns to load in the browser
1616
files: [
17-
'lib/webStorage.js',
17+
'dist/webStorage.js',
1818
'test/**/*.js'
1919
],
2020

lib/webStorage.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
{
22
"name": "webStorage",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A minimal Javascript wrapper to work with localStorage and sessionStorage",
5-
"main": "lib/webStorage.js",
5+
"main": "dist/webStorage.js",
66
"scripts": {
77
"build": "webpack --mode=build",
88
"dev": "webpack --progress --colors --watch --mode=dev",
99
"test": "karma start",
10-
"ci": "npm run build; npm test"
10+
"lint": "eslint src/**",
11+
"ci": "npm run lint; npm test; npm run build;"
1112
},
1213
"repository": {
1314
"type": "git",
1415
"url": "git+https://github.com/georapbox/webStorage.git"
1516
},
1617
"keywords": [
17-
"localStorage",
18-
"sessionStorage",
19-
"Storage",
20-
"Offline Storage",
21-
"Web Storage",
22-
"DOM Storage"
18+
"localStorage", "sessionStorage", "Storage",
19+
"Offline Storage", "Web Storage", "DOM Storage"
2320
],
2421
"author": "George Raptis <[email protected]> (georapbox.github.io)",
2522
"license": "MIT",

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class WebStorage {
2525
constructor(options) {
2626
options = extend({}, defaultConfig, options);
2727

28-
if (!isStorageSupported(options.driver)) {
29-
throw new Error('Web Storage is not supported by your browser.');
30-
}
31-
3228
if (options.name == null || trim(options.name) === '') {
3329
throw 'You must use a valid name for the database.';
3430
}
@@ -183,7 +179,10 @@ class WebStorage {
183179
const storeKeyPrefix = this.storeKeyPrefix;
184180

185181
iterateStorage(this, function (key, value, iterationNumber) {
186-
if (callback && callback(removePrefix(key, storeKeyPrefix), JSON.parse(value), iterationNumber) === false) {
182+
const _key = removePrefix(key, storeKeyPrefix);
183+
const _value = JSON.parse(value);
184+
185+
if (callback && callback(_key, _value, iterationNumber) === false) {
187186
return false;
188187
}
189188
});
@@ -212,10 +211,11 @@ class WebStorage {
212211
}
213212

214213
/**
215-
* Checks if the driver of choice (localStorage || sessionStorage) is supported.
214+
* Checks if the driver of choice (localStorage or sessionStorage) is supported.
215+
* It will return `false` if storage is full.
216216
*
217217
* @this {WebStorage}
218-
* @return {Boolean} Returns true if Web Storage is supported else returns false.
218+
* @return {Boolean} Returns true if Web Storage is supported; otherwise false.
219219
*/
220220
supported() {
221221
return isStorageSupported(this.options.driver);

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const config = {
3131
entry: `${__dirname}/src/index.js`,
3232
devtool: 'source-map',
3333
output: {
34-
path: `${__dirname}/lib`,
34+
path: `${__dirname}/dist`,
3535
filename: outputFile,
3636
library: libraryName,
3737
libraryTarget: 'umd',

0 commit comments

Comments
 (0)