├── README.md ├── astorage.js ├── astorage.min.js ├── bower.json └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # ASTORAGE 2 | A tiny API wrapper for `localStorage` that lets you safely save numbers, arrays, objects and other data types. 3 | 4 | ## Installation 5 | Astorage is available via npm or bower. 6 | 7 | ```bash 8 | npm install astorage 9 | ``` 10 | 11 | or 12 | 13 | ```bash 14 | bower install astorage 15 | ``` 16 | 17 | Otherwise you can directly [download zip](https://github.com/andrew--r/astorage/archive/master.zip). 18 | 19 | ## Usage and API 20 | ```javascript 21 | // define some data 22 | var users = [ 23 | { 24 | name: 'John Doe', 25 | age: 21 26 | }, 27 | { 28 | name: 'Alex Smith', 29 | age: 35 30 | } 31 | ]; 32 | 33 | // save data into localStorage. 34 | // API: astorage.set(key, value) 35 | // key — string 36 | // value — string, number, array, object or anything else 37 | astorage.set('users', users); 38 | 39 | // get value from localStorage by key. 40 | // API: astorage.get(key) 41 | // key — string 42 | astorage.get('users'); 43 | > [{name: 'John Doe', age: 21}, {name: 'Alex Smith', age: 35}] 44 | 45 | // remove item from localStorage 46 | astorage.remove('users'); 47 | 48 | // or empty localStorage 49 | astorage.clear(); 50 | 51 | // get localStorage items count 52 | astorage.length 53 | > 0 54 | ``` 55 | 56 | ## LICENSE 57 | Released under the MIT license. 58 | 59 | ``` 60 | The MIT License (MIT) 61 | 62 | Copyright © 2015 Andrew Romanov, Kiril Osiyuk 63 | 64 | Permission is hereby granted, free of charge, to any person obtaining a copy 65 | of this software and associated documentation files (the "Software"), to deal 66 | in the Software without restriction, including without limitation the rights 67 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 68 | copies of the Software, and to permit persons to whom the Software is 69 | furnished to do so, subject to the following conditions: 70 | 71 | The above copyright notice and this permission notice shall be included in 72 | all copies or substantial portions of the Software. 73 | 74 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 75 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 76 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 77 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 78 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 79 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 80 | THE SOFTWARE. 81 | ``` -------------------------------------------------------------------------------- /astorage.js: -------------------------------------------------------------------------------- 1 | (function(w, l, j) { 2 | 3 | var isLocalStorageAvailable, 4 | isJsonParserAvailable, 5 | messages; 6 | 7 | messages = { 8 | ls: 'localStorage is not available', 9 | json: 'JSON is not available', 10 | full: 'Data can\'t be saved because localStorage is full' 11 | }; 12 | 13 | isLocalStorageAvailable = (l in w && w[l] != undefined); 14 | isJsonParserAvailable = (j in w && w[j] != undefined); 15 | 16 | if (!isLocalStorageAvailable) { 17 | throw new Error(messages.ls); 18 | } 19 | if (!isJsonParserAvailable) { 20 | throw new Error(messages.json); 21 | } 22 | 23 | w.astorage = { 24 | 25 | set: function(key, value) { 26 | try { 27 | w[l].setItem(key, w[j].stringify(value)); 28 | } catch (e) { 29 | console.warn(messages.full); 30 | } 31 | }, 32 | 33 | get: function(key) { 34 | return w[j].parse(w[l].getItem(key)); 35 | }, 36 | 37 | remove: function(key) { 38 | w[l].removeItem(key); 39 | }, 40 | 41 | clear: function() { 42 | w[l].clear(); 43 | }, 44 | 45 | get length() { 46 | return w[l].length; 47 | } 48 | }; 49 | 50 | })(window, 'localStorage', 'JSON'); -------------------------------------------------------------------------------- /astorage.min.js: -------------------------------------------------------------------------------- 1 | (function(a,b,d){var e,f;e=b in a&&void 0!=a[b];f=d in a&&void 0!=a[d];if(!e)throw Error("localStorage is not available");if(!f)throw Error("JSON is not available");a.astorage={set:function(c,e){try{a[b].setItem(c,a[d].stringify(e))}catch(f){console.warn("Data can't be saved because localStorage is full")}},get:function(c){return a[d].parse(a[b].getItem(c))},remove:function(c){a[b].removeItem(c)},clear:function(){a[b].clear()},get length(){return a[b].length}}})(window,"localStorage","JSON"); -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "astorage", 3 | "main": "astorage.min.js", 4 | "version": "1.1.0", 5 | "homepage": "https://github.com/andrew--r/astorage", 6 | "authors": [ 7 | { "name": "Andrew Romanov", "email": "scorpion21.97@gmail.com", "homepage": "http://andrew-r.ru" }, 8 | { "name": "Kiril Osiyuk" } 9 | ], 10 | "description": "A tiny API wrapper for localStorage that lets you safely save numbers, arrays, objects and other data types.", 11 | "keywords": [ 12 | "javascript", 13 | "localStorage", 14 | "html5", 15 | "api", 16 | "wrapper" 17 | ], 18 | "license": "MIT", 19 | "ignore": [ 20 | "**/.*", 21 | "node_modules", 22 | "bower_components", 23 | "test", 24 | "tests" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "astorage", 3 | "version": "1.1.0", 4 | "description": "A tiny API wrapper for localStorage that lets you safely save numbers, arrays, objects and other data types.", 5 | "main": "astorage.min.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/andrew--r/astorage.git" 12 | }, 13 | "keywords": [ 14 | "javascript", 15 | "localstorage", 16 | "html5", 17 | "api", 18 | "wrapper" 19 | ], 20 | "author": "Andrew Romanov", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/andrew--r/astorage/issues" 24 | }, 25 | "homepage": "https://github.com/andrew--r/astorage" 26 | } 27 | --------------------------------------------------------------------------------