diff --git a/src/data/local_storage.js b/src/data/local_storage.js index c316da5ad2..8b18dbff16 100644 --- a/src/data/local_storage.js +++ b/src/data/local_storage.js @@ -194,8 +194,13 @@ p5.prototype.getItem = function(key) { * } * */ -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', '')); + } + }); }; /** diff --git a/test/unit/data/local_storage.js b/test/unit/data/local_storage.js index 9ba6e42b02..f6523b1ef5 100644 --- a/test/unit/data/local_storage.js +++ b/test/unit/data/local_storage.js @@ -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'); + }); + }); });