Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Immutable support #323

Merged
merged 19 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
cf6dfde
Add immutable helper to determine if type is immutable
stefvhuynh Dec 15, 2017
ffe6ec7
Modify formatData and createAccessor to accomodate immutable data
stefvhuynh Dec 15, 2017
cb5ad90
Use same array in reduce instead of generating new array every iteration
stefvhuynh Dec 15, 2017
62a801c
Account for array data inputs
stefvhuynh Dec 15, 2017
2556704
Check for iterable when making string map
stefvhuynh Dec 15, 2017
b9e2f03
Whitelist sortKey, eventKey and user-defined sortKey, eventKey and add
stefvhuynh Dec 15, 2017
f3bd9ff
Add tests for immutable inputs in data helper
stefvhuynh Dec 16, 2017
8736d5d
Fix lint errors
stefvhuynh Dec 18, 2017
c4c63d6
Merge branch 'master' into immutable-support
stefvhuynh Dec 18, 2017
006c6ca
Add immutable test for sort order
stefvhuynh Dec 18, 2017
975b412
Shallowly convert immutable objects to js instead of trimming datum
stefvhuynh Dec 18, 2017
bb10881
Add test for immutable createAccessor
stefvhuynh Dec 18, 2017
7d5b59a
Only run tests that could take immutable input twice
stefvhuynh Dec 18, 2017
e170cde
Add utility to get length of data and allow shallowToJS to convert
stefvhuynh Dec 19, 2017
13bfa35
Add helper method for parsing datum
stefvhuynh Dec 19, 2017
263bc41
Extend proptype for data to account for immutable
stefvhuynh Dec 19, 2017
2195154
Remove immutable from createAccessor; assume plain js object
stefvhuynh Dec 19, 2017
acc132f
Use reduce on props.data when getting string map from data
stefvhuynh Dec 20, 2017
c01a246
Clean up data spec
stefvhuynh Dec 20, 2017
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"chai": "^3.5.0",
"enzyme": "^3.1.0",
"enzyme-adapter-react-16": "^1.0.3",
"immutable": "^3.8.2",
"mocha": "^3.0.2",
"prop-types": "^15.5.8",
"react": "^16.0.0",
Expand Down
27 changes: 20 additions & 7 deletions src/victory-util/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assign, uniq, range, last, isFunction, property, sortBy } from "lodash"
import Helpers from "./helpers";
import Collection from "./collection";
import Scale from "./scale";
import Immutable from "./immutable";

export default {
/**
Expand All @@ -12,7 +13,7 @@ export default {
getData(props) {
let data;
if (props.data) {
if (props.data.length < 1) {
if (Immutable.isIterable(props.data) ? props.data.size < 1 : props.data.length < 1) {
return [];
} else {
data = this.formatData(props.data, props);
Expand Down Expand Up @@ -63,34 +64,45 @@ export default {
* @returns {Array} the formatted data
*/
formatData(dataset, props, stringMap) {
if (!Array.isArray(dataset)) {
const isArrayOrIterable = Array.isArray(dataset) || Immutable.isIterable(dataset);
if (!isArrayOrIterable) {
return [];
}

stringMap = stringMap || {
x: this.createStringMap(props, "x"),
y: this.createStringMap(props, "y")
};

const accessor = {
x: Helpers.createAccessor(props.x !== undefined ? props.x : "x"),
y: Helpers.createAccessor(props.y !== undefined ? props.y : "y"),
y0: Helpers.createAccessor(props.y0 !== undefined ? props.y0 : "y0")
};
const data = dataset.map((datum, index) => {

const data = dataset.reduce((dataArr, datum, index) => { // eslint-disable-line complexity
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using reduce here because js arrays and immutable Lists both have reduce and we can then return a regular array.

datum = Immutable.isImmutable(datum) ? Immutable.shallowToJS(datum) : datum;

const evaluatedX = datum._x !== undefined ? datum._x : accessor.x(datum);
const evaluatedY = datum._y !== undefined ? datum._y : accessor.y(datum);
const y0 = datum._y0 !== undefined ? datum._y0 : accessor.y0(datum);
const x = evaluatedX !== undefined ? evaluatedX : index;
const y = evaluatedY !== undefined ? evaluatedY : datum;
const originalValues = y0 === undefined ? { x, y } : { x, y, y0 };
const privateValues = y0 === undefined ? { _x: x, _y: y } : { _x: x, _y: y, _y0: y0 };
return assign(

dataArr.push(
assign(
originalValues, datum, privateValues,
// map string data to numeric values, and add names
typeof x === "string" ? { _x: stringMap.x[x], xName: x } : {},
typeof y === "string" ? { _y: stringMap.y[y], yName: y } : {},
typeof y0 === "string" ? { _y0: stringMap.y[y0], yName: y0 } : {}
);
});
)
);

return dataArr;
}, []);

const sortedData = this.sortData(data, props.sortKey, props.sortOrder);

Expand Down Expand Up @@ -196,7 +208,8 @@ export default {
* @returns {Array} an array of strings
*/
getStringsFromData(props, axis) {
if (!Array.isArray(props.data)) {
const isArrayOrIterable = Array.isArray(props.data) || Immutable.isIterable(props.data);
if (!isArrayOrIterable) {
return [];
}
const key = typeof props[axis] === "undefined" ? axis : props[axis];
Expand Down
16 changes: 14 additions & 2 deletions src/victory-util/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { defaults, isFunction, property, omit, reduce } from "lodash";
import Collection from "./collection";
import React from "react";
import Immutable from "./immutable";

export default {
getPoint(datum) {
Expand Down Expand Up @@ -144,7 +145,18 @@ export default {
return (x) => x;
}
// otherwise, assume it is an array index, property key or path (_.property handles all three)
return property(key);
return (x) => {
if (Immutable.isImmutable(x)) {
if (Array.isArray(key)) {
return x.getIn(key);
} else if (typeof key === "string") {
return x.getIn(key.split("."));
} else {
return x.get(key);
}
}
return property(key)(x);
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is more of a nice-to-have. People could just always specify an accessor function if they are passing in an immutable object.

},

modifyProps(props, fallbackProps, role) {
Expand Down
33 changes: 33 additions & 0 deletions src/victory-util/immutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
IMMUTABLE_ITERABLE: "@@__IMMUTABLE_ITERABLE__@@",
IMMUTABLE_RECORD: "@@__IMMUTABLE_RECORD__@@",
IMMUTABLE_LIST: "@@__IMMUTABLE_LIST__@@",
IMMUTABLE_MAP: "@@__IMMUTABLE_MAP__@@",

isImmutable(x) {
return this.isIterable(x) || this.isRecord(x);
},

isIterable(x) {
return !!(x && x[this.IMMUTABLE_ITERABLE]);
},

isRecord(x) {
return !!(x && x[this.IMMUTABLE_RECORD]);
},

isList(x) {
return !!(x && x[this.IMMUTABLE_LIST]);
},

isMap(x) {
return !!(x && x[this.IMMUTABLE_MAP]);
},

shallowToJS(x) {
return this.isIterable(x) ? x.reduce((prev, curr, key) => {
prev[key] = curr;
return prev;
}, this.isList(x) ? [] : {}) : x;
}
};
Copy link
Contributor Author

@stefvhuynh stefvhuynh Dec 18, 2017

Choose a reason for hiding this comment

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

these methods should work for immutable@v3.8.x (current latest version) as well as for v4.0.x (in release candidate phase) when it is released.

Loading