Skip to content

Commit

Permalink
add value keywords & testcases stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 8, 2022
1 parent 6781691 commit 1f7e95c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
27 changes: 22 additions & 5 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
* - `value` - required, key value; possible values:
* - arbitrary value
* - `$now$` keyword for setting current time in ms, corresponds to `Date.now()` and `(new Date).getTime()` calls
* - `$thisDate$` keyword for setting string representation of the current date and time, corresponds to `Date()` and `(new Date).toString()` calls
* - `$currentDate$` keyword for setting string representation of the current date and time, corresponds to `Date()` and `(new Date).toString()` calls
*
* **Examples**
* 1. Set local storage item
Expand All @@ -33,14 +33,17 @@ import {
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'providers', '{"providers":[16364,88364],"consent":"all"}')
* ```
*
* 2. Set item with a special value
* 2. Set item with current time in ms
* ```
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.play', '$now$')
* ```
*
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'WPPwlbcapp9yj24', '$thisDate$')
* 3. Set item with current date, e.g 'Tue Nov 08 2022 13:53:19 GMT+0300'
* ```
* example.org#%#//scriptlet('trusted-set-local-storage-item', 'player.live.current.play', '$currentDate$')
* ```
*
* 3. Set item without key or value
* 4. Set item without key or value
* ```
* example.org#%#//scriptlet('trusted-set-local-storage-item', '', '2644')
*
Expand All @@ -54,6 +57,20 @@ 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 setItem = (key, value) => {
const { localStorage } = window;
// setItem() may throw an exception if the storage is full.
Expand All @@ -68,7 +85,7 @@ export function trustedSetLocalStorageItem(source, key, value) {
}
};

setItem(key, value);
setItem(key, parsedValue);
}

trustedSetLocalStorageItem.names = [
Expand Down
20 changes: 7 additions & 13 deletions tests/scriptlets/trusted-set-local-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,13 @@ if (isSafariBrowser()) {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(cName), 'no', 'localStorage item has been set');
clearStorageItem(cName);
});

cName = '__test-item_arrayItem';
cValue = '["item"]';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(cName), cValue, 'localStorage item has not been set');
clearStorageItem(cName);
// test('Set localStorage item with $now$ keyword value', (assert) => {

cName = '__test-item_object';
cValue = '{"key":value"}';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(cName), cValue, 'localStorage item has not been set');
clearStorageItem(cName);
});
// });

// test('Set localStorage item with $currentDate$ keyword value', (assert) => {

// });
}

0 comments on commit 1f7e95c

Please sign in to comment.