Skip to content

Commit

Permalink
Merge pull request #7006 from seralichtenhahn/fix/clear-storage-function
Browse files Browse the repository at this point in the history
fix clearStorage function to match expected behaviour
  • Loading branch information
limzykenneth authored May 4, 2024
2 parents 7cb4f9e + c658000 commit 63316d7
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 63316d7

Please sign in to comment.