Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/data/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,23 @@ import p5 from '../core/main';
* </code>
* </div>
*/
p5.prototype.storeItem = function(key, value) {
p5.prototype.storeItem = function (key, value) {
if (typeof key !== 'string') {
console.log(
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} is not a string.`
);
return;
}
if (key.endsWith('p5TypeID')) {
console.log(
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} must not end with 'p5TypeID'.`
);
return;
}

if (typeof value === 'undefined') {
console.log('You cannot store undefined variables using storeItem().');
p5._friendlyError('You cannot store undefined variables using storeItem().');
return;
}
let type = typeof value;
switch (type) {
Expand Down Expand Up @@ -276,13 +279,14 @@ p5.prototype.storeItem = function(key, value) {
* </code>
* </div>
*/
p5.prototype.getItem = function(key) {
p5.prototype.getItem = function (key) {
let value = localStorage.getItem(key);
const type = localStorage.getItem(`${key}p5TypeID`);
if (typeof type === 'undefined') {
console.log(
p5._friendlyError(
`Unable to determine type of item stored under ${key}in local storage. Did you save the item with something other than setItem()?`
);
return;
} else if (value !== null) {
switch (type) {
case 'number':
Expand Down Expand Up @@ -442,11 +446,12 @@ p5.prototype.clearStorage = function () {
* </code>
* </div>
*/
p5.prototype.removeItem = function(key) {
p5.prototype.removeItem = function (key) {
if (typeof key !== 'string') {
console.log(
p5._friendlyError(
`The argument that you passed to removeItem() - ${key} is not a string.`
);
return;
}
localStorage.removeItem(key);
localStorage.removeItem(`${key}p5TypeID`);
Expand Down