├── .gitignore ├── src └── webStorage.js ├── tea.yaml ├── LICENSE ├── package.json ├── test └── index.test.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Directories 2 | node_modules/ 3 | dist/ 4 | coverage/ 5 | .cache/ 6 | 7 | # Files 8 | .env 9 | .DS_Store -------------------------------------------------------------------------------- /src/webStorage.js: -------------------------------------------------------------------------------- 1 | class WebStorage { 2 | constructor(storageType = 'localStorage') { 3 | this.storage = window[storageType]; 4 | } 5 | 6 | setItem(key, value) { 7 | this.storage.setItem(key, JSON.stringify(value)); 8 | } 9 | 10 | getItem(key) { 11 | const item = this.storage.getItem(key); 12 | return item ? JSON.parse(item) : null; 13 | } 14 | 15 | removeItem(key) { 16 | this.storage.removeItem(key); 17 | } 18 | 19 | clear() { 20 | this.storage.clear(); 21 | } 22 | } 23 | 24 | module.exports = WebStorage; 25 | -------------------------------------------------------------------------------- /tea.yaml: -------------------------------------------------------------------------------- 1 | # https://tea.xyz/what-is-this-file 2 | --- 3 | version: 1.0.0 4 | codeOwners: 5 | - '0x9BAac40A197bbb110ae5B2f31342D8d15D734832' 6 | - '0xC83681Fe4BE1f5c8F65657aA650eEB95F71971E5' 7 | - '0xB3ac3202f949f0bC08c88Bc9D794E8A4BC06d8FF' 8 | - '0x15853A28eC284F7d3A757963BD6e3fb803596a4B' 9 | - '0xD60ef4226B1e8fbafd3591B1Fddec3c3b15F6764' 10 | - '0x732F677454515f1b77D24E243a0C54E799D8D18d' 11 | - '0x612fc3022765D7913523c7dC84D98c3b080f4Afb' 12 | - '0x56Fff8D986453EBaD7e36b39Bfb0B4267c8dA5F8' 13 | - '0x10a05EA9e0202E2acF2fBB802746716007E4aC63' 14 | - '0x6dbf883341C909F291570e9DcaC8965356D79eC4' 15 | - '0x6A1acf6B249BFA3F0710d4983B48C36CE131AB8D' 16 | - '0x5D62317aF2415eF87938d6ffb34ead2a38ADb434' 17 | - '0x393E7948CaC1060C051DB1De5A0D5a42A33886db' 18 | - '0x34bdb45c4D4579F0A2778c6FdE3193ce19EC7201' 19 | - '0xb58bC386a34C8e01C2560f813924B11DA4852Ed1' 20 | - '0x12CC5C0904dDada585Ba231BF3500779ab5edde5' 21 | quorum: 1 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 ONE DIONYS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "onedionys-web-storage-abstraction", 3 | "version": "5.0.0", 4 | "description": "One Dionys (Web Storage Abstraction) - A set of functions to manage web storage including local storage, session storage, and cookies.", 5 | "main": "src/webStorage.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/onedionys/onedionys-web-storage-abstraction.git" 15 | }, 16 | "keywords": [ 17 | "onedionys", 18 | "tea", 19 | "package-manager", 20 | "web-storage-abstraction" 21 | ], 22 | "author": "One Dionys", 23 | "license": "ISC", 24 | "bugs": { 25 | "url": "https://github.com/onedionys/onedionys-web-storage-abstraction/issues" 26 | }, 27 | "homepage": "https://github.com/onedionys/onedionys-web-storage-abstraction#readme", 28 | "dependencies": { 29 | "@types/chai": "^4.3.12", 30 | "@types/mocha": "^10.0.6", 31 | "chai": "^5.1.0", 32 | "mocha": "^10.3.0" 33 | }, 34 | "devDependencies": { 35 | "chai": "^5.1.0", 36 | "mocha": "^10.3.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const WebStorage = require('../src/webStorage'); 3 | 4 | describe('WebStorage', function () { 5 | beforeEach(function () { 6 | // Set up a fake localStorage for testing 7 | const localStorage = {}; 8 | this.webStorage = new WebStorage('localStorage'); 9 | this.webStorage.storage = { 10 | setItem: (key, value) => { 11 | localStorage[key] = value; 12 | }, 13 | getItem: (key) => { 14 | return localStorage[key]; 15 | }, 16 | removeItem: (key) => { 17 | delete localStorage[key]; 18 | }, 19 | clear: () => { 20 | localStorage = {}; 21 | } 22 | }; 23 | }); 24 | 25 | describe('#setItem()', function () { 26 | it('should set an item in the localStorage', function () { 27 | this.webStorage.setItem('testKey', 'testValue'); 28 | assert.equal(localStorage['testKey'], JSON.stringify('testValue')); 29 | }); 30 | }); 31 | 32 | describe('#getItem()', function () { 33 | it('should get an item from the localStorage', function () { 34 | localStorage['testKey'] = JSON.stringify('testValue'); 35 | const value = this.webStorage.getItem('testKey'); 36 | assert.equal(value, 'testValue'); 37 | }); 38 | }); 39 | 40 | describe('#removeItem()', function () { 41 | it('should remove an item from the localStorage', function () { 42 | localStorage['testKey'] = JSON.stringify('testValue'); 43 | this.webStorage.removeItem('testKey'); 44 | assert.equal(localStorage['testKey'], undefined); 45 | }); 46 | }); 47 | 48 | describe('#clear()', function () { 49 | it('should clear the localStorage', function () { 50 | localStorage['testKey1'] = JSON.stringify('testValue1'); 51 | localStorage['testKey2'] = JSON.stringify('testValue2'); 52 | this.webStorage.clear(); 53 | assert.equal(Object.keys(localStorage).length, 0); 54 | }); 55 | }); 56 | }); 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
A set of functions to manage web storage including local storage, session storage, and cookies. 💖
4 | 5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |