Skip to content

Commit

Permalink
add parseKeywordValue helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 8, 2022
1 parent 1dd2f29 commit ac5957c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './match-request-props';
export * from './local-storage-utils';
export * from './parse-keyword-value';
24 changes: 24 additions & 0 deletions src/helpers/parse-keyword-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Modify value according to a keyword.
* Pass initial value if a keyword is not found.
* @param {string} rawValue
* @returns {string}
*/
export const parseKeywordValue = (rawValue) => {
const NOW_VALUE_KEYWORD = '$now$';
const CURRENT_DATE_KEYWORD = '$currentDate$';

let parsedValue;

if (rawValue === NOW_VALUE_KEYWORD) {
// Set to current time in ms, e.g 1667915146503
parsedValue = Date.now().toString();
} else if (rawValue === CURRENT_DATE_KEYWORD) {
// Set to current date e.g 'Tue Nov 08 2022 13:53:19 GMT+0300'
parsedValue = Date();
} else {
parsedValue = rawValue;
}

return parsedValue;
};
15 changes: 3 additions & 12 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
nativeIsNaN,
isCookieSetWithValue,
concatCookieNameValuePath,
parseKeywordValue,
// following helpers should be imported and injected
// because they are used by helpers above
isValidCookieRawPath,
Expand Down Expand Up @@ -90,21 +91,10 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel
return;
}

const NOW_VALUE_KEYWORD = '$now$';
const ONE_YEAR_EXPIRATION_KEYWORD = '1year';
const ONE_DAY_EXPIRATION_KEYWORD = '1day';

let parsedValue;

if (value === NOW_VALUE_KEYWORD) {
// Set cookie value to current time if corresponding keyword was passed
const date = new Date();
const currentTime = date.getTime();

parsedValue = currentTime.toString();
} else {
parsedValue = value;
}
const parsedValue = parseKeywordValue(value);

let cookieToSet = concatCookieNameValuePath(name, parsedValue, path);
if (!cookieToSet) {
Expand Down Expand Up @@ -161,5 +151,6 @@ trustedSetCookie.injections = [
isCookieSetWithValue,
concatCookieNameValuePath,
isValidCookieRawPath,
parseKeywordValue,
getCookiePath,
];
16 changes: 3 additions & 13 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
hit,
nativeIsNaN,
setItem,
parseKeywordValue,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -58,19 +59,7 @@ export function trustedSetLocalStorageItem(source, key, value) {
return;
}

const NOW_VALUE_KEYWORD = '$now$';
const CURRENT_DATE_KEYWORD = '$currentDate$';

let parsedValue;

// Set item value to current time if corresponding keyword was passed
if (value === NOW_VALUE_KEYWORD) {
parsedValue = Date.now().toString();
} else if (value === CURRENT_DATE_KEYWORD) {
parsedValue = Date();
} else {
parsedValue = value;
}
const parsedValue = parseKeywordValue(value);

setItem(key, parsedValue);
hit(source);
Expand All @@ -85,4 +74,5 @@ trustedSetLocalStorageItem.injections = [
hit,
nativeIsNaN,
setItem,
parseKeywordValue,
];

0 comments on commit ac5957c

Please sign in to comment.