|
2 | 2 | * @function doAction
|
3 | 3 | * @description
|
4 | 4 | * A universal write function for localStorage and sessionStorage.
|
5 |
| - * @param {string} [target=local] - a string determines which storage to use |
6 |
| - * @param {string} [action=setItem] - a string determines the write action |
7 |
| - * @param {string} key - the key of a storage item |
8 |
| - * @param {string} value - the value of a storage item |
| 5 | + * @param {object} request - the storage request object |
| 6 | + * @param {string} request.target - a string determines which storage to use |
| 7 | + * @param {string} request.action - a string determines the write action |
| 8 | + * @param {string} request.key - the key of a storage item |
| 9 | + * @param {string} request.value - the value of a storage item |
9 | 10 | */
|
10 |
| -export default function doAction(target = 'local', action = 'setItem', key = undefined, value = undefined) { // eslint-disable-line |
| 11 | +function doAction(request) { |
| 12 | + const args = [request.target, request.action, request.key, request.value] |
11 | 13 | // Store the third and the fourth parameter in a new array, if defined.
|
12 |
| - const storageArgs = Array.prototype.slice.call(arguments, 2) |
| 14 | + const storageArgs = args.slice(2) |
13 | 15 | // Determine the storage target.
|
14 |
| - const storage = target === `local` ? localStorage : sessionStorage |
| 16 | + const storage = request.target === `local` ? localStorage : sessionStorage |
15 | 17 |
|
16 | 18 | // Execute the storage action and pass arguments if they were defined.
|
17 |
| - storage[action](...storageArgs) |
| 19 | + storage[request.action](...storageArgs) |
18 | 20 | }
|
| 21 | + |
| 22 | +export default doAction |
0 commit comments