@availity/localstorage-core
Advanced tools
Comparing version 2.6.1 to 3.0.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [3.0.0](https://github.com/Availity/sdk-js/compare/@availity/localstorage-core@2.6.1...@availity/localstorage-core@3.0.0) (2019-10-21) | ||
### Code Refactoring | ||
* **localstorage-core:** converted class to function ([9f1fdf0](https://github.com/Availity/sdk-js/commit/9f1fdf0)) | ||
### BREAKING CHANGES | ||
* **localstorage-core:** Class is now a Function and doesn't require to be instantiated. | ||
## [2.6.1](https://github.com/Availity/sdk-js/compare/@availity/localstorage-core@2.6.0...@availity/localstorage-core@2.6.1) (2019-02-12) | ||
@@ -8,0 +24,0 @@ |
{ | ||
"name": "@availity/localstorage-core", | ||
"version": "2.6.1", | ||
"version": "3.0.0", | ||
"description": "Wraps localStorage with utility functions", | ||
@@ -12,2 +12,3 @@ "keywords": [ | ||
"main": "src/index.js", | ||
"types": "types/index.d.ts", | ||
"author": "Kasey Powers <kasey.powers@availity.com>", | ||
@@ -18,3 +19,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "9741ebcc564548d3f0e6def050b68529483b9350" | ||
"gitHead": "d376afc731f9cfc8287accd22e20aacb5c035f2d" | ||
} |
# localstorage-core | ||
> Wraps localStorage with utility functions | ||
[![Version](https://img.shields.io/npm/v/@availity/localstorage-core.svg?style=for-the-badge)](https://www.npmjs.com/package/@availity/localstorage-core) | ||
## Install | ||
@@ -11,8 +14,6 @@ | ||
```js | ||
import AvLocalStorarge from '@availity/localstorage-core`; | ||
import * as avLocalStorage from '@availity/localstorage-core'; | ||
const avLocalStorage = new AvLocalStorage(); | ||
avLocalStorage.set('myKey', 'myValue') | ||
console.log(avLocalStorage('myKey')) // logs: "myValue" | ||
avLocalStorage.set('myKey', 'myValue'); | ||
console.log(avLocalStorage.get('myKey')); // logs: "myValue" | ||
``` | ||
@@ -23,17 +24,23 @@ | ||
### `get(key)` | ||
Returns value stored at `key`, will attempt to parse JSON stored there. | ||
### `set(key, value)` | ||
Sets `key` to `value` in localStorage. Will stringify objects if needed. | ||
### `remove(key)` | ||
Removes `key` from localStorage. | ||
### `getKeys(searchKey)` | ||
Returns array of all keys that match the `searchKey` string or `RegExp`. | ||
### `removeKeys(searchKey)` | ||
Remove all keys that match the `searchKey` string or RegExp. | ||
### `getSessionBust()` | ||
Returns session key set by Availity on login. |
@@ -1,57 +0,53 @@ | ||
class AvLocalStorage { | ||
get(key) { | ||
// checks if localStorage is supported, then attempt to parse JSON keys | ||
const value = window.localStorage.getItem(key); | ||
let output; | ||
try { | ||
output = JSON.parse(value); | ||
} catch (error) { | ||
output = value; | ||
} | ||
return output; | ||
export const get = key => { | ||
// checks if localStorage is supported, then attempt to parse JSON keys | ||
const value = window.localStorage.getItem(key); | ||
let output; | ||
try { | ||
output = JSON.parse(value); | ||
} catch (error) { | ||
output = value; | ||
} | ||
return output; | ||
}; | ||
set(key, value) { | ||
// checks if localStorage is supported, stringifies non-strings before setting value | ||
const setValue = typeof value === 'string' ? value : JSON.stringify(value); | ||
window.localStorage.setItem(key, setValue); | ||
} | ||
export const set = (key, value) => { | ||
// checks if localStorage is supported, stringifies non-strings before setting value | ||
const setValue = typeof value === 'string' ? value : JSON.stringify(value); | ||
window.localStorage.setItem(key, setValue); | ||
}; | ||
remove(key) { | ||
// wrapper to remove Item | ||
window.localStorage.removeItem(key); | ||
} | ||
export const remove = key => { | ||
// wrapper to remove Item | ||
window.localStorage.removeItem(key); | ||
}; | ||
getKeys(searchKey) { | ||
// get all keys that match this string or regex | ||
const regexString = | ||
searchKey instanceof RegExp ? searchKey : new RegExp(searchKey); | ||
if (regexString) { | ||
const output = []; | ||
const { length } = window.localStorage; | ||
for (let i = 0; i < length; i++) { | ||
const thisKey = window.localStorage.key(i); | ||
if (regexString.test(thisKey)) { | ||
output.push(thisKey); | ||
} | ||
export const getKeys = searchKey => { | ||
// get all keys that match this string or regex | ||
const regexString = | ||
searchKey instanceof RegExp ? searchKey : new RegExp(searchKey); | ||
if (regexString) { | ||
const output = []; | ||
const { length } = window.localStorage; | ||
for (let i = 0; i < length; i++) { | ||
const thisKey = window.localStorage.key(i); | ||
if (regexString.test(thisKey)) { | ||
output.push(thisKey); | ||
} | ||
return output; | ||
} | ||
return undefined; | ||
return output; | ||
} | ||
removeKeys(searchKey) { | ||
// remove all keys that match this string or regex | ||
const removeKeys = this.getKeys(searchKey); | ||
removeKeys.forEach(key => { | ||
this.remove(key); | ||
}); | ||
} | ||
return undefined; | ||
}; | ||
getSessionBust() { | ||
return this.get('avCacheBust'); | ||
} | ||
} | ||
export const removeKeys = searchKey => { | ||
// remove all keys that match this string or regex | ||
const removeKeys = getKeys(searchKey); | ||
removeKeys.forEach(key => { | ||
remove(key); | ||
}); | ||
}; | ||
export default AvLocalStorage; | ||
export const getSessionBust = () => { | ||
return get('avCacheBust'); | ||
}; |
@@ -1,5 +0,3 @@ | ||
import AvLocalStorage from '..'; | ||
import * as avLocalStorage from '..'; | ||
let avLocalStorage; | ||
const getAllItems = () => { | ||
@@ -19,6 +17,2 @@ const items = {}; | ||
describe('avLocalStorage', () => { | ||
beforeEach(() => { | ||
avLocalStorage = new AvLocalStorage(); | ||
}); | ||
afterEach(() => { | ||
@@ -25,0 +19,0 @@ window.localStorage.clear(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11467
7
220
45
1