Socket
Socket
Sign inDemoInstall

localstorage-fifo

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

localstorage-fifo - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

lib/fifo.js.map

390

lib/fifo.js

@@ -1,92 +0,36 @@

var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
class LocalStorageMock {
constructor() {
this.store = {};
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
removeItem(key) {
delete this.store[key];
}
setItem(key, value) {
if (value) {
this.store[key] = value.toString();
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var LocalStorage = function () {
function LocalStorage() {
classCallCheck(this, LocalStorage);
key(index) {
return Object.keys(this.store)[index];
}
get length() {
return Object.keys(this.store).length;
}
}
createClass(LocalStorage, [{
key: "getItem",
value: function getItem(key) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
return String(this[key]);
}
return null;
}
}, {
key: "setItem",
value: function setItem(key, val) {
this[key] = String(val);
}
}, {
key: "removeItem",
value: function removeItem(key) {
delete this[key];
}
}, {
key: "clear",
value: function clear() {
var self = this;
Object.keys(this).forEach(function (key) {
self[key] = undefined;
delete self[key];
});
}
}, {
key: "key",
value: function key() {
var i = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return Object.keys(this)[i];
}
}, {
key: "length",
get: function get$$1() {
return Object.keys(this).length;
}
}]);
return LocalStorage;
}();
var Fifo = function () {
/**
* Contructs a new Fifo object.
* @param {object} options - The Fifo configuration.
*/
function Fifo() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
classCallCheck(this, Fifo);
class Fifo {
constructor(options = {}) {
this.namespace = options.namespace || 'fifo';
this.noLS = false;
if (options.console) {
this.console = options.console;
} else {
this.console = function () {};
}
this.checkLocalStorage(options.shim);
this.console = options.console || function console() {};
this.data = {

@@ -96,4 +40,4 @@ keys: [],

};
var dataFromStorage = JSON.parse(this.LS.getItem(this.namespace));
this.checkLocalStorage(options.shim);
const dataFromStorage = JSON.parse(this.LS.getItem(this.namespace));
if (dataFromStorage && Object.prototype.hasOwnProperty.call(dataFromStorage, 'keys') && Object.prototype.hasOwnProperty.call(dataFromStorage, 'items')) {

@@ -105,205 +49,103 @@ this.data = dataFromStorage;

}
createClass(Fifo, [{
key: 'checkLocalStorage',
value: function checkLocalStorage(shim) {
// NOTE: This check has to be wrapped within a try/catch because of a SecurityError: DOM Exception 18 on iOS.
/* istanbul ignore next */
try {
if (typeof shim !== 'undefined' || typeof localStorage !== 'undefined' && localStorage !== null) {
this.LS = shim || localStorage;
this.noLS = false;
} else {
this.LS = new LocalStorage();
this.noLS = true;
this.console('warn', 'No localStorage, shimming.');
}
} catch (error) {
this.LS = new LocalStorage();
checkLocalStorage(shim) {
try {
if (typeof shim !== 'undefined' || typeof localStorage !== 'undefined' && localStorage !== null) {
this.LS = shim || localStorage;
this.noLS = false;
} else {
this.LS = new LocalStorageMock();
this.noLS = true;
this.console('warn', 'No localStorage, shimming.');
}
} catch (error) {
this.LS = new LocalStorageMock();
this.noLS = true;
this.console('warn', 'No localStorage, shimming.');
}
/**
* Attempts to save the key/value pair to localStorage.
* @return {boolean} - Whether or not the save was successful.
*/
}, {
key: 'trySave',
value: function trySave() {
if (this.noLS) {
}
trySave() {
if (this.noLS) {
return false;
}
try {
this.LS.setItem(this.namespace, JSON.stringify(this.data));
return true;
} catch (error) {
if (error.code === 18 || error.code === 21 || error.code === 22 || error.code === 1014 || error.number === -2147024882) {
return false;
}
try {
this.LS.setItem(this.namespace, JSON.stringify(this.data));
return true;
} catch (error) {
// 18 for Safari: SecurityError: DOM Exception 18
// 21 for some Safari
// 22 for Chrome and Safari, 1014 for Firefox: QUOTA_EXCEEDED_ERR
// -2147024882 for IE10 Out of Memory
/* istanbul ignore next */
if (error.code === 18 || error.code === 21 || error.code === 22 || error.code === 1014 || error.number === -2147024882) {
return false;
}
/* istanbul ignore next */
this.console('error', 'Error with localStorage:', error);
/* istanbul ignore next */
return true;
}
this.console('error', 'Error with localStorage:', error);
return true;
}
/**
* Attempts to remove the first item added to make room for the next.
* @return The item being removed.
*/
}, {
key: 'removeFirstIn',
value: function removeFirstIn() {
var firstIn = this.data.keys.pop();
var removedItem = {
key: firstIn,
value: this.data.items[firstIn]
};
delete this.data.items[firstIn];
return removedItem;
}
/**
* Save the key/value pair to localStorage.
* @return The item being removed.
*/
}, {
key: 'save',
value: function save() {
var removed = [];
if (this.noLS) {
return removed;
}
while (!this.trySave()) {
// NOTE: Difficult to test without a browser, and difficult in a browser.
/* istanbul ignore next */
if (this.data.keys.length) {
removed.push(this.removeFirstIn());
} else {
this.console('error', 'All items removed from ' + this.namespace + ', still can\'t save.');
}
}
}
removeFirstIn() {
const firstIn = this.data.keys.pop();
const removedItem = {
key: firstIn,
value: this.data.items[firstIn]
};
delete this.data.items[firstIn];
return removedItem;
}
save() {
const removed = [];
if (this.noLS) {
return removed;
}
/**
* Set a key/value pair.
* @param {string} key - The key to use in the key value pair.
* @param value - The value to use in the key value pair.
* @return The current instance of Fifo.
*/
}, {
key: 'set',
value: function set$$1(key, value) {
this.data.items[key] = value;
var index = this.data.keys.indexOf(key);
if (index > -1) {
this.data.keys.splice(index, 1);
while (!this.trySave()) {
if (this.data.keys.length) {
removed.push(this.removeFirstIn());
} else {
this.console('error', `All items removed from ${this.namespace}, still can't save.`);
}
this.data.keys.unshift(key);
this.save();
return this;
}
/**
* Get a value for a given key.
* @param {string} [key] - The key to use in the key value pair.
* @return The item, or, all items when no key is provided.
*/
}, {
key: 'get',
value: function get$$1(key) {
if (key) {
return this.data.items[key];
}
// Return all items.
return this.data.items;
return removed;
}
set(key, value) {
this.data.items[key] = value;
const index = this.data.keys.indexOf(key);
if (index > -1) {
this.data.keys.splice(index, 1);
}
/**
* All the keys currently being used.
* @return {array} The keys.
*/
}, {
key: 'keys',
value: function keys() {
return this.data.keys || [];
this.data.keys.unshift(key);
this.save();
return this;
}
get(key) {
if (key) {
return this.data.items[key];
}
/**
* Checks for the existence of a key.
* @param {string} key - The key to check for.
* @return {boolean} Wheter or not the key exist.
*/
}, {
key: 'has',
value: function has(key) {
return this.data.keys.indexOf(key) !== -1;
return this.data.items;
}
keys() {
return this.data.keys || [];
}
has(key) {
return this.data.keys.indexOf(key) !== -1;
}
remove(target) {
if (target == null || !this.data.keys) {
return;
}
/**
* Removes a key/value pair from the collection.
* @param victim - The key/value pair to find, can be one of a String, Regular Expression or a Function.
* @return The current instance of Fifo.
*/
}, {
key: 'remove',
value: function remove(victim) {
var _this = this;
if (victim == null || !this.data.keys) {
return this;
const {
keys
} = this.data;
keys.forEach((suspect, i) => {
if (suspect === target) {
this.data.keys.splice(i, 1);
delete this.data.items[suspect];
}
}, this);
this.save();
}
empty() {
this.data = {
keys: [],
items: {}
};
this.save();
}
}
var keys = this.data.keys;
keys.forEach(function (suspect, i) {
if (suspect === victim) {
_this.data.keys.splice(i, 1);
delete _this.data.items[suspect];
}
}, this);
this.save();
return this;
}
/**
* Resets the local data and saves empting out the localStorage to an empty state.
* @return The current instance of Fifo.
*/
}, {
key: 'empty',
value: function empty() {
this.data = {
keys: [],
items: {}
};
this.save();
return this;
}
}]);
return Fifo;
}();
export default Fifo;
exports["default"] = Fifo;
//# sourceMappingURL=fifo.js.map
{
"name": "localstorage-fifo",
"version": "2.0.1",
"version": "3.0.0",
"description": "JavaScript library for interacting with localStorage safely.",

@@ -12,3 +12,2 @@ "author": "Matthew Callis <matthew.callis@gmail.com>",

],
"main": "lib/fifo.js",
"repository": {

@@ -25,47 +24,54 @@ "type": "git",

],
"files": [
"lib/*",
"src/*",
"types/*"
],
"main": "lib/fifo.js",
"module": "./src/fifo.js",
"type": "module",
"types": "types/index.d.ts",
"typings": "types/index.d.ts",
"sideEffects": false,
"dependencies": {},
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.26.0",
"babel-plugin-espower": "^2.4.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-istanbul": "^4.1.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2015-rollup": "^3.0.0",
"babel-preset-es2016": "^6.24.1",
"babel-register": "^6.26.0",
"browser-env": "^3.2.5",
"codeclimate-test-reporter": "^0.5.0",
"coveralls": "^3.0.0",
"eslint": "^4.18.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-ava": "^4.5.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-no-inferred-method-name": "^1.0.2",
"eslint-plugin-react": "^7.6.1",
"eslint-plugin-xss": "^0.1.9",
"npm-bump": "^0.0.23",
"nyc": "^11.4.1",
"precommit-hook-eslint": "^3.0.0",
"rollup": "^0.56.2",
"rollup-plugin-babel": "^3.0.3",
"rollup-plugin-eslint": "^4.0.0",
"rollup-plugin-json": "^2.3.0",
"uglify-js": "^3.3.11"
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.1.3",
"@typescript-eslint/parser": "^5.12.1",
"ava": "^4.0.1",
"browser-env": "^3.3.0",
"c8": "^7.11.0",
"eslint": "^8.9.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-ava": "^13.2.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-jsdoc": "^37.9.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-optimize-regex": "^1.2.1",
"eslint-plugin-ramda": "^2.5.1",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-xss": "^0.1.11",
"jsdoc": "^3.6.10",
"jsdoc-tsimport-plugin": "^1.0.5",
"rollup": "^2.68.0",
"rollup-plugin-cleanup": "^3.2.1",
"typescript": "^4.5.5"
},
"engines": {
"node": "^16"
},
"preferGlobal": false,
"private": false,
"license": "MIT",
"jam": {
"dependencies": {}
},
"scripts": {
"lint": "eslint --quiet --color src/",
"local-web-server": "ruby -run -ehttpd . -p12345",
"lint": "eslint src/",
"make": "node rollup.config.js",
"report": "nyc report --reporter=html",
"test-watch": "npm test -- --watch",
"test": "NODE_ENV=test nyc ava",
"test": "NODE_ENV=test c8 ava --serial",
"validate": "npm ls"

@@ -87,3 +93,2 @@ },

"require": [
"babel-register",
"./test/helpers/setup-browser-env.js"

@@ -90,0 +95,0 @@ ],

@@ -66,3 +66,2 @@ # [Fifo](https://github.com/MatthewCallis/fifo)

npm run test
npm run report
```

@@ -69,0 +68,0 @@

@@ -1,7 +0,25 @@

import LocalStorage from './localStorage';
import LocalStorage from './localStorage.js';
/**
* The Fifo configuration object.
*
* @typedef {object} FifoOptions
* @property {string} [namespace] The name of the key in localStorage.
* @property {Function} [console] An optional console logging function.
* @property {object} [shim] A shim class to act as localStorage in case it is missing.
*/
/**
* A single rule where a subject is compared against a given value on an SDK.
*
* @typedef {object} FifoData
* @property {string[]} keys The collection of keys in the collection.
* @property {object} items The actual items in the collection.
*/
export default class Fifo {
/**
* Contructs a new Fifo object.
* @param {object} options - The Fifo configuration.
*
* @param {FifoOptions} options The Fifo configuration.
*/

@@ -11,10 +29,4 @@ constructor(options = {}) {

this.noLS = false;
if (options.console) {
this.console = options.console;
} else {
this.console = () => {};
}
this.checkLocalStorage(options.shim);
this.console = options.console || function console() {};
/** @type {FifoData} The localStoage data */
this.data = {

@@ -25,2 +37,4 @@ keys: [],

this.checkLocalStorage(options.shim);
const dataFromStorage = JSON.parse(this.LS.getItem(this.namespace));

@@ -34,5 +48,10 @@ if (dataFromStorage && Object.prototype.hasOwnProperty.call(dataFromStorage, 'keys') && Object.prototype.hasOwnProperty.call(dataFromStorage, 'items')) {

/**
* Checks for the existence of localStorage and shims it should it not exist.
* This check has to be wrapped within a try/catch because of a SecurityError: DOM Exception 18 on iOS.
*
* @param {object} shim A shim class to act as localStorage in case it is missing.
*/
checkLocalStorage(shim) {
// NOTE: This check has to be wrapped within a try/catch because of a SecurityError: DOM Exception 18 on iOS.
/* istanbul ignore next */
/* c8 ignore start */
try {

@@ -52,2 +71,3 @@ if (typeof shim !== 'undefined' || (typeof localStorage !== 'undefined' && localStorage !== null)) {

}
/* c8 ignore stop */
}

@@ -57,3 +77,9 @@

* Attempts to save the key/value pair to localStorage.
* @return {boolean} - Whether or not the save was successful.
* Possible Errors:
* - 18 for Safari: SecurityError: DOM Exception 18
* - 21 for some Safari
* - 22 for Chrome and Safari, 1014 for Firefox: QUOTA_EXCEEDED_ERR
* - -2147024882 for IE10 Out of Memory
*
* @returns {boolean} Whether or not the save was successful.
*/

@@ -68,14 +94,8 @@ trySave() {

return true;
/* c8 ignore next 7 */
} catch (error) {
// 18 for Safari: SecurityError: DOM Exception 18
// 21 for some Safari
// 22 for Chrome and Safari, 1014 for Firefox: QUOTA_EXCEEDED_ERR
// -2147024882 for IE10 Out of Memory
/* istanbul ignore next */
if (error.code === 18 || error.code === 21 || error.code === 22 || error.code === 1014 || error.number === -2147024882) {
return false;
}
/* istanbul ignore next */
this.console('error', 'Error with localStorage:', error);
/* istanbul ignore next */
return true;

@@ -87,3 +107,4 @@ }

* Attempts to remove the first item added to make room for the next.
* @return The item being removed.
*
* @returns {object} The item being removed.
*/

@@ -102,3 +123,5 @@ removeFirstIn() {

* Save the key/value pair to localStorage.
* @return The item being removed.
* This is difficult to test without a browser, and difficult in a browser.
*
* @returns {object[]} The item being removed.
*/

@@ -111,5 +134,4 @@ save() {

/* c8 ignore next 7 */
while (!this.trySave()) {
// NOTE: Difficult to test without a browser, and difficult in a browser.
/* istanbul ignore next */
if (this.data.keys.length) {

@@ -127,5 +149,6 @@ removed.push(this.removeFirstIn());

* Set a key/value pair.
* @param {string} key - The key to use in the key value pair.
* @param value - The value to use in the key value pair.
* @return The current instance of Fifo.
*
* @param {string} key The key to use in the key value pair.
* @param {string|number} value The value to use in the key value pair.
* @returns {Fifo} The current instance of Fifo.
*/

@@ -145,4 +168,5 @@ set(key, value) {

* Get a value for a given key.
* @param {string} [key] - The key to use in the key value pair.
* @return The item, or, all items when no key is provided.
*
* @param {string} [key] The key to use in the key value pair.
* @returns {*|[*]} The item, or, all items when no key is provided.
*/

@@ -160,3 +184,4 @@ get(key) {

* All the keys currently being used.
* @return {array} The keys.
*
* @returns {Array} The keys.
*/

@@ -169,4 +194,5 @@ keys() {

* Checks for the existence of a key.
*
* @param {string} key - The key to check for.
* @return {boolean} Wheter or not the key exist.
* @returns {boolean} Wheter or not the key exist.
*/

@@ -179,8 +205,8 @@ has(key) {

* Removes a key/value pair from the collection.
* @param victim - The key/value pair to find, can be one of a String, Regular Expression or a Function.
* @return The current instance of Fifo.
*
* @param {string} target - The key/value pair to find, can be one of a String, Regular Expression or a Function.
*/
remove(victim) {
if (victim == null || !this.data.keys) {
return this;
remove(target) {
if (target == null || !this.data.keys) {
return;
}

@@ -190,3 +216,3 @@

keys.forEach((suspect, i) => {
if (suspect === victim) {
if (suspect === target) {
this.data.keys.splice(i, 1);

@@ -198,3 +224,2 @@ delete this.data.items[suspect];

this.save();
return this;
}

@@ -204,3 +229,2 @@

* Resets the local data and saves empting out the localStorage to an empty state.
* @return The current instance of Fifo.
*/

@@ -213,4 +237,3 @@ empty() {

this.save();
return this;
}
}

@@ -1,32 +0,31 @@

export default class LocalStorage {
getItem(key) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
return String(this[key]);
}
return null;
export default class LocalStorageMock {
constructor() {
this.store = {};
}
setItem(key, val) {
this[key] = String(val);
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
removeItem(key) {
delete this[key];
delete this.store[key];
}
clear() {
const self = this;
Object.keys(this).forEach((key) => {
self[key] = undefined;
delete self[key];
});
setItem(key, value) {
if (value) {
this.store[key] = value.toString();
}
}
key(i = 0) {
return Object.keys(this)[i];
key(index) {
return Object.keys(this.store)[index];
}
get length() {
return Object.keys(this).length;
return Object.keys(this.store).length;
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc