Skip to content

Commit

Permalink
improve setItem helper to setStorageItem
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 9, 2022
1 parent dbe58d1 commit 852d01d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export * from './random-response';
export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './match-request-props';
export * from './local-storage-utils';
export * from './storage-utils';
export * from './parse-keyword-value';
16 changes: 0 additions & 16 deletions src/helpers/local-storage-utils.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/helpers/storage-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Set item to a specified storage
* @param {Object} storage Storage instance to set item into
* @param {string} key
* @param {string} value
* @param {boolean} shouldLog
*/
export const setStorageItem = (storage, key, value, shouldLog) => {
// eslint-disable-next-line no-console
const log = console.log.bind(console);
// setItem() may throw an exception if the storage is full.
try {
storage.setItem(key, value);
} catch (e) {
if (shouldLog) {
log(`Unable to set sessionStorage item due to: ${e.message}`);
}
}
};
7 changes: 4 additions & 3 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
hit,
nativeIsNaN,
setItem,
setStorageItem,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -85,7 +85,8 @@ export function setLocalStorageItem(source, key, value) {
return;
}

setItem(key, keyValue);
const { localStorage } = window;
setStorageItem(localStorage, key, keyValue, source.verbose);
hit(source);
}

Expand All @@ -96,5 +97,5 @@ setLocalStorageItem.names = [
setLocalStorageItem.injections = [
hit,
nativeIsNaN,
setItem,
setStorageItem,
];
16 changes: 4 additions & 12 deletions src/scriptlets/set-session-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
hit,
nativeIsNaN,
setStorageItem,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -84,18 +85,8 @@ export function setSessionStorageItem(source, key, value) {
return;
}

const setItem = (key, value) => {
const { sessionStorage } = window;
// setItem() may throw an exception if the storage is full.
try {
sessionStorage.setItem(key, value);
hit(source);
} catch (e) {
log(`Was unable to set sessionStorage item due to: ${e.message}`);
}
};

setItem(key, keyValue);
const { sessionStorage } = window;
setStorageItem(sessionStorage, key, keyValue, source.verbose);
}

setSessionStorageItem.names = [
Expand All @@ -105,4 +96,5 @@ setSessionStorageItem.names = [
setSessionStorageItem.injections = [
hit,
nativeIsNaN,
setStorageItem,
];
7 changes: 4 additions & 3 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
hit,
nativeIsNaN,
setItem,
setStorageItem,
parseKeywordValue,
} from '../helpers/index';

Expand Down Expand Up @@ -69,7 +69,8 @@ export function trustedSetLocalStorageItem(source, key, value) {

const parsedValue = parseKeywordValue(value);

setItem(key, parsedValue);
const { localStorage } = window;
setStorageItem(localStorage, key, parsedValue, source.verbose);
hit(source);
}

Expand All @@ -81,6 +82,6 @@ trustedSetLocalStorageItem.names = [
trustedSetLocalStorageItem.injections = [
hit,
nativeIsNaN,
setItem,
setStorageItem,
parseKeywordValue,
];

0 comments on commit 852d01d

Please sign in to comment.