Skip to content

Commit

Permalink
add getLimitedStorageItemValue helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 11, 2022
1 parent e411fe3 commit f18dc3a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 68 deletions.
43 changes: 43 additions & 0 deletions src/helpers/storage-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { nativeIsNaN } from './number-utils';

/**
* Sets item to a specified storage, if storage isn't full.
* @param {Storage} storage storage instance to set item into
Expand All @@ -17,3 +19,44 @@ export const setStorageItem = (storage, key, value, shouldLog) => {
}
}
};

export const getLimitedStorageItemValue = (value) => {
if (!value) {
return null;
}
const log = console.log.bind(console); // eslint-disable-line no-console
let validValue;
if (value === 'undefined') {
validValue = undefined;
} else if (value === 'false') {
validValue = false;
} else if (value === 'true') {
validValue = true;
} else if (value === 'null') {
validValue = null;
} else if (value === 'emptyArr') {
validValue = '[]';
} else if (value === 'emptyObj') {
validValue = '{}';
} else if (value === '') {
validValue = '';
} else if (/^\d+$/.test(value)) {
validValue = parseFloat(value);
if (nativeIsNaN(validValue)) {
log(`Invalid storage item value: '${value}'`);
return null;
}
if (Math.abs(validValue) > 0x7FFF) {
log(`Invalid storage item value: '${value}'`);
return null;
}
} else if (value === 'yes') {
validValue = 'yes';
} else if (value === 'no') {
validValue = 'no';
} else {
return null;
}

return validValue;
};
40 changes: 6 additions & 34 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
logVerbose,
nativeIsNaN,
setStorageItem,
getLimitedStorageItemValue,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -47,44 +48,14 @@ export function setLocalStorageItem(source, key, value) {
return;
}

if (typeof value === 'undefined') {
logVerbose('Item value should be specified.', source.verbose);
return;
}

let keyValue;
if (value === 'undefined') {
keyValue = undefined;
} else if (value === 'false') {
keyValue = false;
} else if (value === 'true') {
keyValue = true;
} else if (value === 'null') {
keyValue = null;
} else if (value === 'emptyArr') {
keyValue = '[]';
} else if (value === 'emptyObj') {
keyValue = '{}';
} else if (value === '') {
keyValue = '';
} else if (/^\d+$/.test(value)) {
keyValue = parseFloat(value);
if (nativeIsNaN(keyValue)) {
return;
}
if (Math.abs(keyValue) > 0x7FFF) {
return;
}
} else if (value === 'yes') {
keyValue = 'yes';
} else if (value === 'no') {
keyValue = 'no';
} else {
const validValue = getLimitedStorageItemValue(value);
if (validValue === null) {
logVerbose(`Invalid cookie value: '${validValue}'`, source.verbose);
return;
}

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

Expand All @@ -97,4 +68,5 @@ setLocalStorageItem.injections = [
logVerbose,
nativeIsNaN,
setStorageItem,
getLimitedStorageItemValue,
];
40 changes: 6 additions & 34 deletions src/scriptlets/set-session-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
logVerbose,
nativeIsNaN,
setStorageItem,
getLimitedStorageItemValue,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -47,44 +48,14 @@ export function setSessionStorageItem(source, key, value) {
return;
}

if (typeof value === 'undefined') {
logVerbose('Item value should be specified.', source.verbose);
return;
}

let keyValue;
if (value === 'undefined') {
keyValue = undefined;
} else if (value === 'false') {
keyValue = false;
} else if (value === 'true') {
keyValue = true;
} else if (value === 'null') {
keyValue = null;
} else if (value === 'emptyArr') {
keyValue = '[]';
} else if (value === 'emptyObj') {
keyValue = '{}';
} else if (value === '') {
keyValue = '';
} else if (/^\d+$/.test(value)) {
keyValue = parseFloat(value);
if (nativeIsNaN(keyValue)) {
return;
}
if (Math.abs(keyValue) > 0x7FFF) {
return;
}
} else if (value === 'yes') {
keyValue = 'yes';
} else if (value === 'no') {
keyValue = 'no';
} else {
const validValue = getLimitedStorageItemValue(value);
if (validValue === null) {
logVerbose(`Invalid cookie value: '${validValue}'`, source.verbose);
return;
}

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

setSessionStorageItem.names = [
Expand All @@ -96,4 +67,5 @@ setSessionStorageItem.injections = [
logVerbose,
nativeIsNaN,
setStorageItem,
getLimitedStorageItemValue,
];

0 comments on commit f18dc3a

Please sign in to comment.