Skip to content

Commit

Permalink
add setItems to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 8, 2022
1 parent 600bf62 commit 872a9ea
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './random-response';
export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './match-request-props';
export * from './local-storage-utils';
11 changes: 11 additions & 0 deletions src/helpers/local-storage-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const setItem = (key, value) => {
// eslint-disable-next-line no-console
const log = console.log.bind(console);
const { localStorage } = window;
// setItem() may throw an exception if the storage is full.
try {
localStorage.setItem(key, value);
} catch (e) {
log(`Was unable to set localStorage item due to: ${e.message}`);
}
};
17 changes: 3 additions & 14 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
hit,
nativeIsNaN,
setItem,
} from '../helpers/index';

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

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

setItem(key, keyValue);
hit(source);
}

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

/* eslint-disable max-len */
Expand Down Expand Up @@ -71,21 +72,8 @@ export function trustedSetLocalStorageItem(source, key, value) {
parsedValue = value;
}

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

setItem(key, parsedValue);
hit(source);
}

trustedSetLocalStorageItem.names = [
Expand All @@ -96,4 +84,5 @@ trustedSetLocalStorageItem.names = [
trustedSetLocalStorageItem.injections = [
hit,
nativeIsNaN,
setItem,
];

0 comments on commit 872a9ea

Please sign in to comment.