Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added p5.Vector in localStorags(#3990) #4034

Merged
merged 4 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions src/data/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import p5 from '../core/main';
* @method storeItem
* @for p5
* @param {String} key
* @param {String|Number|Object|Boolean|p5.Color} value
* @param {String|Number|Object|Boolean|p5.Color|p5.Vector} value
*
* @example
* <div><code>
Expand Down Expand Up @@ -71,6 +71,10 @@ p5.prototype.storeItem = (key, value) => {
case 'object':
if (value instanceof p5.Color) {
type = 'p5.Color';
} else if (value instanceof p5.Vector) {
type = 'p5.Vector';
let coord = [value.x, value.y, value.z];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let coord = [value.x, value.y, value.z];
const coord = [value.x, value.y, value.z];

small request, to try to use const whenever something doesn't need to be reassigned. Otherwise looks great!

value = coord;
}
value = JSON.stringify(value);
break;
Expand All @@ -92,7 +96,7 @@ p5.prototype.storeItem = (key, value) => {
* @method getItem
* @for p5
* @param {String} key name that you wish to use to store in local storage
* @return {Number|Object|String|Boolean|p5.Color} Value of stored item
* @return {Number|Object|String|Boolean|p5.Color|p5.Vector} Value of stored item
*
* @example
* <div><code>
Expand Down Expand Up @@ -149,6 +153,10 @@ p5.prototype.getItem = function(key) {
value = JSON.parse(value);
value = this.color(...value.levels);
break;
case 'p5.Vector':
value = JSON.parse(value);
value = this.createVector(...value);
break;
case 'string':
default:
break;
Expand Down
10 changes: 10 additions & 0 deletions test/unit/data/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ suite('local storage', function() {
var myNumber = 46;
var myString = 'coolio';
var myColor;
var myVector;

var hardCodedTypeID = 'p5TypeID';

Expand All @@ -13,11 +14,13 @@ suite('local storage', function() {
p.setup = function() {
myp5 = p;
myColor = myp5.color(40, 100, 70);
myVector = myp5.createVector(10, 20, 30);
myp5.storeItem('myBoolean', myBoolean);
myp5.storeItem('myObject', myObject);
myp5.storeItem('myNumber', myNumber);
myp5.storeItem('myString', myString);
myp5.storeItem('myColor', myColor);
myp5.storeItem('myVector', myVector);
done();
};
});
Expand Down Expand Up @@ -66,6 +69,9 @@ suite('local storage', function() {
test('p5 Color should retrieve as p5 Color', function() {
assert.isTrue(myp5.getItem('myColor') instanceof p5.Color);
});
test('p5 Vector should retrieve as p5 Vector', function() {
assert.isTrue(myp5.getItem('myVector') instanceof p5.Vector);
});
});

var checkRemoval = function(key) {
Expand Down Expand Up @@ -94,5 +100,9 @@ suite('local storage', function() {
test('color should be removable', function() {
checkRemoval('myColor');
});

test('vector should be removable', function() {
checkRemoval('myVector');
});
});
});