├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── _config.yml ├── jest.config.js ├── package.json ├── renovate.json ├── src ├── cookie.js ├── index.js ├── local.js ├── session.js └── storage.js ├── tests ├── cookie.test.js ├── local.test.js ├── session.test.js └── storage.test.js └── webpack.config.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | ko_fi: gadingnst 2 | custom: ['https://trakteer.id/gadingnst', 'https://karyakarsa.com/gadingnst'] 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /lib/* 2 | /node_modules/ 3 | /coverage/ 4 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | branches: 3 | only: 4 | - master 5 | node_js: 6 | - '11' 7 | script: npm run test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sutan Nst. 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Web Storage 2 | 3 | [![GitHub](https://img.shields.io/github/license/sutanlab/simple-webstorage.svg)](https://github.com/sutanlab/simple-webstorage) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/a3575d847c764f659810b1bd725679cf)](https://app.codacy.com/app/sutanlab/simple-webstorage?utm_source=github.com&utm_medium=referral&utm_content=sutanlab/simple-webstorage&utm_campaign=Badge_Grade_Dashboard) [![Build Status](https://travis-ci.org/sutanlab/simple-webstorage.svg?branch=master)](https://travis-ci.org/sutanlab/simple-webstorage) [![npm](https://img.shields.io/npm/v/simple-webstorage.svg)](https://www.npmjs.com/package/simple-webstorage) [![npm](https://img.shields.io/npm/dt/simple-webstorage.svg)](https://npm-stat.com/charts.html?package=simple-webstorage) [![npm](https://img.shields.io/bundlephobia/min/simple-webstorage.svg)](https://unpkg.com/simple-webstorage/lib/bundle/simple-webstorage.min.js) [![David](https://img.shields.io/david/dev/sutanlab/simple-webstorage.svg)](https://www.npmjs.com/package/simple-webstorage?activeTab=dependencies) [![GitHub issues](https://img.shields.io/github/issues/sutanlab/simple-webstorage.svg)](https://github.com/sutanlab/simple-webstorage/issues) [![Website](https://img.shields.io/website/https/sutanlab.js.org/simple-webstorage.svg)](https://sutanlab.js.org/simple-webstorage) 4 | 5 | > Lightweight utilities that can make easier to write and read application storage in client browser. 6 | 7 | ### Support : 8 | - Local Storage 9 | - Cookie Storage 10 | - Session Storage 11 | 12 | --- 13 | 14 | ## HOW TO USE 15 | 16 | ### 1. Use Package with NPM or YARN 17 | 18 | ```bash 19 | # with npm 20 | $ npm i simple-webstorage --save 21 | 22 | # or with yarn 23 | $ yarn add simple-webstorage 24 | ``` 25 | 26 | #### All API import 27 | 28 | ```js 29 | import SimpleWebStorage from 'simple-webstorage' 30 | 31 | const storage = SimpleWebStorage() 32 | 33 | storage.local.set('key', 'value') // empty the third parameter to store data permanently (only affected in local) 34 | storage.cookie.set('key', 'value', 5) 35 | storage.session.set('key', 'value', 5) 36 | ``` 37 | 38 | #### Partial API import 39 | 40 | ```js 41 | // # for local storage 42 | import { get as getLocalStorage, set as setLocalStorage } from 'simple-webstorage/lib/local' 43 | 44 | // # for session storage 45 | // import { get, set } from 'simple-webstorage/lib/session' 46 | 47 | // # for cookie storage 48 | // import { get, set } from 'simple-webstorage/lib/cookie' 49 | 50 | setLocalStorage('key', { 51 | name: 'you', 52 | skill: [ 53 | 'angry', 54 | 'crying' 55 | ] 56 | }) 57 | 58 | console.log(getLocalStorage('key')) // { name: 'you', skill: ['angry', 'crying'] } 59 | ``` 60 | 61 | ##### or you can import partial API like this : 62 | 63 | ```js 64 | // # for cookie storage 65 | import CookieStorage from 'simple-webstorage/lib/cookie' 66 | 67 | // # for local storage 68 | // import LocalStorage from 'simple-webstorage/lib/local' 69 | 70 | // # for session storage 71 | // import SessionStorage from 'simple-webstorage/lib/session' 72 | 73 | const cookie = CookieStorage() 74 | 75 | cookie.set('remembered', true) 76 | cookie.set('forgotten', true) 77 | 78 | console.log(cookie.get('remembered')) // true # get values from key. returns any 79 | console.log(cookie.keys()) // ['remembered', 'forgotten'] # list all keys. returns array 80 | ``` 81 | 82 | ### 2. All in minified js 83 | 84 | ```html 85 | 86 | 93 | ``` 94 | 95 | ## API Details 96 | 97 | | Storage | Method | Parameters | 98 | |-----------|-------------|---------------------------------------------------------------------------------------------| 99 | | `local` | set | `key` (type: String), `value` (type: any, default: 0), `expiryInMinutes` (type: Number, default: null) | 100 | | `cookie` or `session` | set | `key` (type: String), `value` (type: any, default: 0), `expiryInMinutes` (type: Number, default: 5) | 101 | | `local` or `cookie` or `session` | get | `key` (type: String) | 102 | | `local` or `cookie` or `session` | remove | `key` (type: String) | 103 | | `local` or `cookie` or `session` | keys | none | 104 | | `local` or `cookie` or `session` | clear | none | 105 | 106 | ## Support Me 107 | ### Global 108 | [![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/gadingnst) 109 | ### Indonesia 110 | - [Trakteer](https://trakteer.id/gadingnst) 111 | - [Karyakarsa](https://karyakarsa.com/gadingnst) 112 | 113 | --- 114 | Feel free to contribute [simple-webstorage](https://github.com/sutanlab/simple-webstorage) 🙂 115 | 116 | Copyright © 2019 by Sutan Gading Fadhillah Nasution 117 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | // For a detailed explanation regarding each configuration property, visit: 2 | // https://jestjs.io/docs/en/configuration.html 3 | 4 | module.exports = { 5 | clearMocks: true, 6 | testEnvironment: "node", 7 | coverageDirectory: "coverage", 8 | collectCoverage: true 9 | }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-webstorage", 3 | "version": "1.6.1", 4 | "description": "Lightweight utilities that can make easier to write and read application storage in client browser.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "babel src -d lib; webpack", 8 | "prepublish": "babel src -d lib; webpack", 9 | "pretest": "babel src -d lib; webpack", 10 | "test": "IS_TEST_MODE=true jest --coverage" 11 | }, 12 | "babel": { 13 | "presets": [ 14 | "@babel/preset-env" 15 | ] 16 | }, 17 | "files": [ 18 | "lib" 19 | ], 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/sutanlab/simple-webstorage.git" 23 | }, 24 | "keywords": [ 25 | "browser storage", 26 | "web storage", 27 | "local storage", 28 | "session storage", 29 | "cookie storage", 30 | "client storage", 31 | "storage", 32 | "cookie", 33 | "local", 34 | "session" 35 | ], 36 | "author": "Sutan Nst. ", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/sutanlab/simple-webstorage/issues" 40 | }, 41 | "homepage": "https://sutanlab.js.org/simple-webstorage", 42 | "devDependencies": { 43 | "@babel/cli": "7.6.4", 44 | "@babel/core": "7.6.4", 45 | "@babel/preset-env": "7.6.3", 46 | "jest": "24.9.0", 47 | "webpack": "4.41.2", 48 | "jsdom": "15.2.0", 49 | "webpack-cli": "3.3.9" 50 | }, 51 | "dependencies": {} 52 | } 53 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /src/cookie.js: -------------------------------------------------------------------------------- 1 | import { isNotNull, isNotEmpty } from './storage' 2 | 3 | export const set = (key, value = 0, expiryInMinutes) => { 4 | let expires = '' 5 | expiryInMinutes = isNotEmpty(expiryInMinutes) ? expiryInMinutes : 5 6 | if (expiryInMinutes) { 7 | const date = new Date(); 8 | date.setTime(date.getTime() + (expiryInMinutes * 60 * 1000)) 9 | expires = '; expires=' + date.toGMTString() 10 | } 11 | document.cookie = key + '=' + JSON.stringify(value) + expires + '; path=/' 12 | return value 13 | } 14 | 15 | export const get = key => { 16 | key = key + '=' 17 | const cookies = document.cookie.split(';') 18 | for(let i = 0; i < cookies.length; i++) { 19 | let cookie = cookies[i] 20 | while (cookie.charAt(0) === ' ') cookie = cookie.substring(1, cookie.length) 21 | if (cookie.indexOf(key) === 0) return JSON.parse(cookie.substring(key.length, cookie.length)) 22 | } 23 | return null 24 | } 25 | 26 | export const remove = key => { 27 | if (isNotNull(get(key))) { 28 | set(key, '', -1) 29 | return true 30 | } 31 | return false 32 | } 33 | 34 | export const clear = () => { 35 | const cookies = document.cookie.split(";") 36 | for(let i = 0; i < cookies.length; i++) { 37 | set(cookies[i].split("=")[0], '', -1) 38 | } 39 | } 40 | 41 | export const keys = () => { 42 | const keys = [] 43 | const cookies = document.cookie.split(';') 44 | for(let i = 0; i < cookies.length; i++) { 45 | const key = cookies[i].split("=")[0]; 46 | if(key !== '') keys.push(key) 47 | } 48 | return keys 49 | } 50 | 51 | export default () => ({ get, set, remove, clear, keys }) -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import LocalStorage from './local' 2 | import CookieStorage from './cookie' 3 | import SessionStorage from './session' 4 | 5 | global.SimpleWebStorage = () => ({ 6 | local: { 7 | get: LocalStorage().get, 8 | set: LocalStorage().set, 9 | remove: LocalStorage().remove, 10 | clear: LocalStorage().clear, 11 | keys: LocalStorage().keys 12 | }, 13 | cookie: { 14 | get: CookieStorage().get, 15 | set: CookieStorage().set, 16 | remove: CookieStorage().remove, 17 | clear: CookieStorage().clear, 18 | keys: CookieStorage().keys 19 | }, 20 | session: { 21 | get: SessionStorage().get, 22 | set: SessionStorage().set, 23 | remove: SessionStorage().remove, 24 | clear: SessionStorage().clear, 25 | keys: SessionStorage().keys 26 | } 27 | }) 28 | 29 | export default SimpleWebStorage -------------------------------------------------------------------------------- /src/local.js: -------------------------------------------------------------------------------- 1 | import { 2 | set as setStorage, 3 | get as getStorage, 4 | check as checkStorage, 5 | remove as removeStorage, 6 | clear as clearStorage, 7 | keys as keysStorage, 8 | isNotEmpty 9 | } from './storage' 10 | 11 | export const get = key => { 12 | try { 13 | return getStorage(checkStorage('localStorage'), key) 14 | } catch(err) { 15 | console.error(err.message) 16 | } 17 | return null 18 | } 19 | 20 | export const set = (key, value = 0, expiryInMinutes) => { 21 | try { 22 | expiryInMinutes = isNotEmpty(expiryInMinutes) ? expiryInMinutes : null 23 | return setStorage(checkStorage('localStorage'), key, value, expiryInMinutes) 24 | } catch(err) { 25 | console.error(err.message) 26 | } 27 | return false 28 | } 29 | 30 | export const remove = key => { 31 | try { 32 | return removeStorage(checkStorage('localStorage'), key) 33 | } catch(err) { 34 | console.error(err.message) 35 | } 36 | return false 37 | } 38 | 39 | export const clear = () => { 40 | try { 41 | return clearStorage(checkStorage('localStorage')) 42 | } catch(err) { 43 | console.error(err.message) 44 | } 45 | return false 46 | } 47 | 48 | export const keys = () => { 49 | try { 50 | return keysStorage(checkStorage('localStorage')) 51 | } catch(err) { 52 | console.error(err.message) 53 | } 54 | return false 55 | } 56 | 57 | export default () => ({ get, set, remove, clear, keys }) -------------------------------------------------------------------------------- /src/session.js: -------------------------------------------------------------------------------- 1 | import { 2 | set as setStorage, 3 | get as getStorage, 4 | check as checkStorage, 5 | remove as removeStorage, 6 | clear as clearStorage, 7 | keys as keysStorage, 8 | isNotEmpty 9 | } from './storage' 10 | 11 | export const get = key => { 12 | try { 13 | return getStorage(checkStorage('sessionStorage'), key) 14 | } catch(err) { 15 | console.error(err.message) 16 | } 17 | return null 18 | } 19 | 20 | export const set = (key, value = 0, expiryInMinutes) => { 21 | try { 22 | expiryInMinutes = isNotEmpty(expiryInMinutes) ? expiryInMinutes : 5 23 | return setStorage(checkStorage('sessionStorage'), key, value, expiryInMinutes) 24 | } catch(err) { 25 | console.error(err.message) 26 | } 27 | return false 28 | } 29 | 30 | export const remove = key => { 31 | try { 32 | return removeStorage(checkStorage('sessionStorage'), key) 33 | } catch(err) { 34 | console.error(err.message) 35 | } 36 | return false 37 | } 38 | 39 | export const clear = () => { 40 | try { 41 | return clearStorage(checkStorage('sessionStorage')) 42 | } catch(err) { 43 | console.error(err.message) 44 | } 45 | return false 46 | } 47 | 48 | export const keys = () => { 49 | try { 50 | return keysStorage(checkStorage('sessionStorage')) 51 | } catch(err) { 52 | console.error(err.message) 53 | } 54 | return false 55 | } 56 | 57 | export default () => ({ get, set, remove, clear, keys }) -------------------------------------------------------------------------------- /src/storage.js: -------------------------------------------------------------------------------- 1 | export const isNotNull = variable => (typeof variable !== 'undefined' && variable !== null) 2 | 3 | export const isNotEmpty = variable => (typeof variable !== 'undefined' && variable) 4 | 5 | export const check = storage => { 6 | if (storage in window && window[storage]) return window[storage] 7 | throw new Error(`Your Browser doesn't support ${storage}`) 8 | } 9 | 10 | export const get = (storage, key) => { 11 | const cache = storage.getItem(key) 12 | if (isNotNull(cache)) { 13 | const cacheParsed = JSON.parse(cache) 14 | if (isNotNull(cacheParsed)) { 15 | const timeNow = new Date().getTime() 16 | const dateCache = cacheParsed.created 17 | const expiryInMilis = parseInt(cacheParsed.expiry, 10) * 60 * 1000 18 | const expiryTime = parseInt(dateCache, 10) + expiryInMilis 19 | if (isNotNull(cacheParsed.expiry)) { 20 | if (expiryTime > timeNow) return cacheParsed.value 21 | else storage.removeItem(key) 22 | } else { 23 | return cacheParsed.value 24 | } 25 | } 26 | } 27 | return null 28 | } 29 | 30 | export const set = (storage, key, value, expiryInMinutes) => { 31 | const data = { 32 | created: new Date().getTime(), 33 | value, 34 | expiry: expiryInMinutes, 35 | } 36 | storage.setItem(key, JSON.stringify(data)) 37 | return data 38 | } 39 | 40 | export const remove = (storage, key) => { 41 | if(get(storage, key)) { 42 | storage.removeItem(key) 43 | return true 44 | } 45 | return false 46 | } 47 | 48 | export const keys = storage => { 49 | const keys = [] 50 | for (let i = 0; i < storage.length; i++) { 51 | const key = storage.key(i) 52 | if (get(storage, key)) keys.push(key) 53 | } 54 | return keys 55 | } 56 | 57 | export const clear = storage => storage.clear() 58 | -------------------------------------------------------------------------------- /tests/cookie.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | 5 | import CookieStorage, {get, set, keys, remove } from '../lib/cookie' 6 | 7 | const cookieStorage = CookieStorage() 8 | 9 | describe('Testing Cookie Storage API functionally', () => { 10 | test('[set] should not to be false', () => { 11 | expect(cookieStorage.set('person', { name: 'John', age: 21 })).toBeTruthy() 12 | }) 13 | test('[get] should to be equal', () => { 14 | expect(cookieStorage.get('person')).toEqual({ name: 'John', age: 21 }); 15 | }) 16 | test('[keys] should to be array', () => { 17 | expect(cookieStorage.keys()).toEqual(['person']); 18 | }) 19 | test('[remove] should to be true', () => { 20 | expect(cookieStorage.remove('person')).toBeTruthy(); 21 | }) 22 | }) 23 | 24 | describe('Testing Cookie Storage partial API functionally', () => { 25 | test('[set] should not to be false', () => { 26 | expect(set('person', { name: 'John', age: 21 })).toBeTruthy() 27 | }) 28 | test('[get] should to be equal', () => { 29 | expect(get('person')).toEqual({ name: 'John', age: 21 }); 30 | }) 31 | test('[keys] should to be array', () => { 32 | expect(keys()).toEqual(['person']); 33 | }) 34 | test('[remove] should to be true', () => { 35 | expect(remove('person')).toBeTruthy(); 36 | }) 37 | }) -------------------------------------------------------------------------------- /tests/local.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | 5 | import LocalStorage, {get, set, keys, remove } from '../lib/local' 6 | 7 | const localStorage = LocalStorage() 8 | 9 | describe('Testing Local Storage API functionally', () => { 10 | test('[set] should not to be false', () => { 11 | expect(localStorage.set('person', { name: 'John', age: 21 })).toBeTruthy() 12 | }) 13 | test('[get] should to be equal', () => { 14 | expect(localStorage.get('person')).toEqual({ name: 'John', age: 21 }); 15 | }) 16 | test('[keys] should to be array', () => { 17 | expect(localStorage.keys()).toEqual(['person']); 18 | }) 19 | test('[remove] should to be true', () => { 20 | expect(localStorage.remove('person')).toBeTruthy(); 21 | }) 22 | }) 23 | 24 | describe('Testing Local Storage partial API functionally', () => { 25 | test('[set] should not to be false', () => { 26 | expect(set('person', { name: 'John', age: 21 })).toBeTruthy() 27 | }) 28 | test('[get] should to be equal', () => { 29 | expect(get('person')).toEqual({ name: 'John', age: 21 }); 30 | }) 31 | test('[keys] should to be array', () => { 32 | expect(keys()).toEqual(['person']); 33 | }) 34 | test('[remove] should to be true', () => { 35 | expect(remove('person')).toBeTruthy(); 36 | }) 37 | }) -------------------------------------------------------------------------------- /tests/session.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | 5 | import SessionStorage, {get, set, keys, remove } from '../lib/session' 6 | 7 | const sessionStorage = SessionStorage() 8 | 9 | describe('Testing Session Storage API functionally', () => { 10 | test('[set] should not to be false', () => { 11 | expect(sessionStorage.set('person', { name: 'John', age: 21 })).toBeTruthy() 12 | }) 13 | test('[get] should to be equal', () => { 14 | expect(sessionStorage.get('person')).toEqual({ name: 'John', age: 21 }); 15 | }) 16 | test('[keys] should to be array', () => { 17 | expect(sessionStorage.keys()).toEqual(['person']); 18 | }) 19 | test('[remove] should to be true', () => { 20 | expect(sessionStorage.remove('person')).toBeTruthy(); 21 | }) 22 | }) 23 | 24 | describe('Testing Session Storage partial API functionally', () => { 25 | test('[set] should not to be false', () => { 26 | expect(set('person', { name: 'John', age: 21 })).toBeTruthy() 27 | }) 28 | test('[get] should to be equal', () => { 29 | expect(get('person')).toEqual({ name: 'John', age: 21 }); 30 | }) 31 | test('[keys] should to be array', () => { 32 | expect(keys()).toEqual(['person']); 33 | }) 34 | test('[remove] should to be true', () => { 35 | expect(remove('person')).toBeTruthy(); 36 | }) 37 | }) -------------------------------------------------------------------------------- /tests/storage.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | 5 | import SimpleWebStorage from '../lib/index' 6 | import { isNotNull, isNotEmpty } from '../lib/storage' 7 | 8 | const storage = SimpleWebStorage() 9 | 10 | describe('Testing Base API', () => { 11 | test('Testing all API import', () => { 12 | expect(storage).toBeTruthy() 13 | }) 14 | test('Test [isNotNull] functionally', () => { 15 | expect(isNotNull(undefined)).toBeFalsy() 16 | expect(isNotNull(null)).toBeFalsy() 17 | }) 18 | test('Test [isNotEmpty] functionally', () => { 19 | expect(isNotEmpty(undefined)).toBeFalsy() 20 | expect(isNotEmpty(null)).toBeFalsy() 21 | expect(isNotEmpty(false)).toBeFalsy() 22 | expect(isNotEmpty(0)).toBeFalsy() 23 | }) 24 | }) 25 | 26 | describe('Testing Local Storage API functionally', () => { 27 | test('[set] should not to be false', () => { 28 | expect(storage.local.set('person', { name: 'John', age: 21 })).toBeTruthy() 29 | }) 30 | test('[get] should to be equal', () => { 31 | expect(storage.local.get('person')).toEqual({ name: 'John', age: 21 }); 32 | }) 33 | test('[keys] should to be array', () => { 34 | expect(storage.local.keys()).toEqual(['person']); 35 | }) 36 | test('[remove] should to be true', () => { 37 | expect(storage.local.remove('person')).toBeTruthy(); 38 | }) 39 | }) 40 | 41 | describe('Testing Session Storage API functionally', () => { 42 | test('[set] should not to be false', () => { 43 | expect(storage.session.set('message', 'Hello')).toBeTruthy() 44 | }) 45 | test('[get] should to be equal', () => { 46 | expect(storage.session.get('message')).toEqual('Hello'); 47 | }) 48 | test('[keys] should to be array', () => { 49 | expect(storage.session.keys()).toEqual(['message']); 50 | }) 51 | test('[remove] should to be true', () => { 52 | expect(storage.session.remove('message')).toBeTruthy(); 53 | }) 54 | }) 55 | 56 | describe('Testing Cookie Storage API functionally', () => { 57 | test('[set] should not to be false', () => { 58 | expect(storage.cookie.set('logged', true)).toBeTruthy() 59 | }) 60 | test('[get] should to be false', () => { 61 | expect(storage.cookie.get('logged')).toBeTruthy(); 62 | }) 63 | test('[keys] should to be array', () => { 64 | expect(storage.cookie.keys()).toEqual(['logged']); 65 | }) 66 | test('[remove] should to be true', () => { 67 | expect(storage.cookie.remove('logged')).toBeTruthy(); 68 | }) 69 | }) -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | 3 | module.exports = { 4 | entry: ['./src/index.js'], 5 | mode: 'production', 6 | output: { 7 | path: path.join(__dirname, 'lib', 'bundle'), 8 | filename: 'simple-webstorage.min.js', 9 | }, 10 | optimization: { 11 | splitChunks: { 12 | chunks:'all' 13 | } 14 | } 15 | } --------------------------------------------------------------------------------