Skip to content

Commit

Permalink
fix clearStorage function
Browse files Browse the repository at this point in the history
  • Loading branch information
seralichtenhahn committed May 3, 2024
1 parent 7cb4f9e commit c658000
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/data/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,13 @@ p5.prototype.getItem = function(key) {
* }
* </code></div>
*/
p5.prototype.clearStorage = function() {
localStorage.clear();
p5.prototype.clearStorage = function () {
const keys = Object.keys(localStorage);
keys.forEach(key => {
if (key.endsWith('p5TypeID')) {
this.removeItem(key.replace('p5TypeID', ''));
}
});
};

/**
Expand Down
14 changes: 14 additions & 0 deletions test/unit/data/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ suite('local storage', function() {
checkRemoval('myVector');
});
});

suite('should be able to clear all items at once', function () {
test('should remove all items set by storeItem()', function () {
localStorage.setItem('extra', 'stuff');
myp5.clearStorage();
assert.deepEqual(myp5.getItem('myBoolean'), null);
assert.deepEqual(myp5.getItem('myNumber'), null);
assert.deepEqual(myp5.getItem('myObject'), null);
assert.deepEqual(myp5.getItem('myString'), null);
assert.deepEqual(myp5.getItem('myColor'), null);
assert.deepEqual(myp5.getItem('myVector'), null);
assert.deepEqual(myp5.getItem('extra'), 'stuff');
});
});
});

0 comments on commit c658000

Please sign in to comment.