Skip to content

Commit c658000

Browse files
fix clearStorage function
1 parent 7cb4f9e commit c658000

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/data/local_storage.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,13 @@ p5.prototype.getItem = function(key) {
194194
* }
195195
* </code></div>
196196
*/
197-
p5.prototype.clearStorage = function() {
198-
localStorage.clear();
197+
p5.prototype.clearStorage = function () {
198+
const keys = Object.keys(localStorage);
199+
keys.forEach(key => {
200+
if (key.endsWith('p5TypeID')) {
201+
this.removeItem(key.replace('p5TypeID', ''));
202+
}
203+
});
199204
};
200205

201206
/**

test/unit/data/local_storage.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,18 @@ suite('local storage', function() {
105105
checkRemoval('myVector');
106106
});
107107
});
108+
109+
suite('should be able to clear all items at once', function () {
110+
test('should remove all items set by storeItem()', function () {
111+
localStorage.setItem('extra', 'stuff');
112+
myp5.clearStorage();
113+
assert.deepEqual(myp5.getItem('myBoolean'), null);
114+
assert.deepEqual(myp5.getItem('myNumber'), null);
115+
assert.deepEqual(myp5.getItem('myObject'), null);
116+
assert.deepEqual(myp5.getItem('myString'), null);
117+
assert.deepEqual(myp5.getItem('myColor'), null);
118+
assert.deepEqual(myp5.getItem('myVector'), null);
119+
assert.deepEqual(myp5.getItem('extra'), 'stuff');
120+
});
121+
});
108122
});

0 commit comments

Comments
 (0)