generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstorage.js
46 lines (40 loc) · 1.41 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// This is a scaffolding file we have provided for you which allows you to manage stored data for your application.
// It can be loaded into index.html.
// You should not need to modify it to complete the project.
/**
* Get a list of user ids
*
* @returns {string[]} List of user id strings
*/
export function getUserIds() {
return ["1", "2", "3", "4", "5"];
}
/**
* Get data associated with a specific user.
*
* @param {string} userId The user id to get data for
* @returns {any[] | null} The data associated with the user
*/
export function getData(userId) {
return JSON.parse(localStorage.getItem(`stored-data-user-${userId}`));
}
/**
* Store data for a specific user. If there was already data for this user, this function preserves it and adds the new data at the end.
*
* @param {string} userId The user id to store data for
* @param {any[]} data The data to store
*/
export function addData(userId, data) {
const key = `stored-data-user-${userId}`;
const existingData = getData(userId) || [];
const newData = existingData.concat(data);
localStorage.setItem(key, JSON.stringify(newData));
}
/**
* Clears all data associated with a specific user. NOTE: This is provided to help with development, and is not required in the final code
*
* @param {string} userId The user id to clear associated data for
*/
export function clearData(userId) {
localStorage.removeItem(`stored-data-user-${userId}`);
}