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

Improvement/named exports #380

Merged
merged 25 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"d3-shape": "^1.2.0",
"d3-timer": "^1.0.0",
"lodash": "^4.17.5",
"react-fast-compare": "^1.0.0"
"react-fast-compare": "^2.0.0"
},
"devDependencies": {
"builder-victory-component-dev": "^5.1.2",
Expand Down
5 changes: 3 additions & 2 deletions src/victory-container/victory-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CustomPropTypes from "../victory-util/prop-types";
import { assign, defaults, uniqueId, isObject } from "lodash";
import Portal from "../victory-portal/portal";
import Timer from "../victory-util/timer";
import { omit } from "../victory-util/helpers";
import Helpers from "../victory-util/helpers";

export default class VictoryContainer extends React.Component {
static displayName = "VictoryContainer";
Expand Down Expand Up @@ -130,7 +130,8 @@ export default class VictoryContainer extends React.Component {

render() {
const { width, height, responsive, events } = this.props;
const style = responsive ? this.props.style : omit(this.props.style, ["height", "width"]);
const style = responsive ?
this.props.style : Helpers.omit(this.props.style, ["height", "width"]);
const svgProps = assign(
{
width, height, role: "img",
Expand Down
2 changes: 1 addition & 1 deletion src/victory-legend/helper-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const getCalculatedValues = (props) => {
const style = getStyles(props, defaultStyles);
const colorScale = getColorScale(props);
const isHorizontal = orientation === "horizontal";
const borderPadding = Helpers.formatPadding(props.borderPadding);
const borderPadding = Helpers.getPadding({ padding: props.borderPadding });
return assign({}, props, { style, isHorizontal, colorScale, borderPadding });
};

Expand Down
4 changes: 2 additions & 2 deletions src/victory-portal/victory-portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import PropTypes from "prop-types";
import { defaults } from "lodash";
import Log from "../victory-util/log";
import { omit } from "../victory-util/helpers";
import Helpers from "../victory-util/helpers";

export default class VictoryPortal extends React.Component {
static displayName = "VictoryPortal";
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class VictoryPortal extends React.Component {
const childProps = children && children.props || {};
const standardProps = childProps.groupComponent ? { groupComponent, standalone: false } : {};
const newProps = defaults(
standardProps, childProps, omit(this.props, ["children", "groupComponent"])
standardProps, childProps, Helpers.omit(this.props, ["children", "groupComponent"])
);
const child = children && React.cloneElement(children, newProps);
return this.renderPortal(child);
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/circle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class Circle extends React.Component {
static propTypes = {
Expand All @@ -17,7 +17,7 @@ export default class Circle extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/line.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class Line extends React.Component {
static propTypes = {
Expand All @@ -18,7 +18,7 @@ export default class Line extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class Path extends React.Component {
static propTypes = {
Expand All @@ -15,7 +15,7 @@ export default class Path extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/rect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class Rect extends React.Component {
static propTypes = {
Expand All @@ -20,7 +20,7 @@ export default class Rect extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class Text extends React.Component {
static propTypes = {
Expand All @@ -24,7 +24,7 @@ export default class Text extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/victory-primitives/tspan.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import PropTypes from "prop-types";
import Collection from "../victory-util/collection";
import isEqual from "react-fast-compare";

export default class TSpan extends React.Component {
static propTypes = {
Expand All @@ -16,7 +16,7 @@ export default class TSpan extends React.Component {
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
return !isEqual(this.props, nextProps);
}

render() {
Expand Down
5 changes: 0 additions & 5 deletions src/victory-tooltip/victory-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PropTypes from "prop-types";
import CustomPropTypes from "../victory-util/prop-types";
import TextSize from "../victory-util/textsize";
import Helpers from "../victory-util/helpers";
import Collection from "../victory-util/collection";
import LabelHelpers from "../victory-util/label-helpers";
import VictoryLabel from "../victory-label/victory-label";
import VictoryTheme from "../victory-theme/victory-theme";
Expand Down Expand Up @@ -133,10 +132,6 @@ export default class VictoryTooltip extends React.Component {
}];
};

shouldComponentUpdate(nextProps) {
return !Collection.areVictoryPropsEqual(this.props, nextProps);
}

getDefaultOrientation(props) {
const { datum, horizontal, polar } = props;
if (!polar) {
Expand Down
29 changes: 4 additions & 25 deletions src/victory-util/add-events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { defaults, assign, keys, isFunction, pick, without, isEmpty } from "lodash";
import Events from "./events";
import Collection from "./collection";
import isEqual from "react-fast-compare";
import VictoryTransition from "../victory-transition/victory-transition";

// used for checking state changes. Expected components can be passed in via options
Expand All @@ -28,35 +28,14 @@ export default (WrappedComponent, options) => {

componentDidUpdate() {
const externalMutations = this.getExternalMutations(this.props);
if (!Collection.areVictoryPropsEqual(this.externalMutations, externalMutations)) {
if (!isEqual(this.externalMutations, externalMutations)) {
this.externalMutations = externalMutations;
this.applyExternalMutations(this.props, externalMutations);
}
}

shouldComponentUpdate(nextProps) {
const calculatedValues = this.getCalculatedValues(nextProps);
// re-render without additional checks when component is animated
if (this.props.animate || this.props.animating) {
this.cacheValues(calculatedValues);
return true;
}

// check for any state changes triggered by events or shared events
const calculatedState = this.getStateChanges(nextProps, calculatedValues);
if (!Collection.areVictoryPropsEqual(this.calculatedState, calculatedState)) {
this.cacheValues(calculatedValues);
this.calculatedState = calculatedState;
return true;
}

// check whether props have changed
if (!Collection.areVictoryPropsEqual(this.props, nextProps)) {
this.cacheValues(calculatedValues);
return true;
}

return false;
componentWillReceiveProps(nextProps) {
this.cacheValues(this.getCalculatedValues(nextProps));
}

applyExternalMutations(props, externalMutations) {
Expand Down
159 changes: 53 additions & 106 deletions src/victory-util/collection.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,56 @@
import isEqual from "react-fast-compare";
/* eslint-disable func-style */
/* eslint-disable no-use-before-define */

function isNonEmptyArray(collection) {
return Array.isArray(collection) && collection.length > 0;
}

function containsStrings(collection) {
return Array.isArray(collection) && collection.some((value) => typeof value === "string");
}

function containsDates(collection) {
return Array.isArray(collection) && collection.some((value) => value instanceof Date);
}

function containsNumbers(collection) {
return Array.isArray(collection) && collection.some((value) => typeof value === "number");
}

function containsOnlyStrings(collection) {
return isNonEmptyArray(collection) &&
collection.every((value) => typeof value === "string");
}

function isArrayOfArrays(collection) {
return isNonEmptyArray(collection) && collection.every(Array.isArray);
}

function removeUndefined(arr) {
return arr.filter((el) => el !== undefined);
}

function getMaxValue(arr, ...values) {
const array = arr.concat(values);
return containsDates(array) ?
new Date(Math.max(...array)) :
Math.max(...array);
}

function getMinValue(arr, ...values) {
const array = arr.concat(values);
return containsDates(array) ?
new Date(Math.min(...array)) :
Math.min(...array);
}

export default {
isNonEmptyArray(collection) {
return Array.isArray(collection) && collection.length > 0;
},

containsStrings(collection) {
return Array.isArray(collection) && collection.some((value) => typeof value === "string");
},

containsDates(collection) {
return Array.isArray(collection) && collection.some((value) => value instanceof Date);
},

containsNumbers(collection) {
return Array.isArray(collection) && collection.some((value) => typeof value === "number");
},

containsOnlyStrings(collection) {
return this.isNonEmptyArray(collection) &&
collection.every((value) => typeof value === "string");
},

isArrayOfArrays(collection) {
return this.isNonEmptyArray(collection) && collection.every(Array.isArray);
},

removeUndefined(arr) {
return arr.filter((el) => el !== undefined);
},

getMaxValue(arr, ...values) {
const array = arr.concat(values);
return this.containsDates(array) ?
new Date(Math.max(...array)) :
Math.max(...array);
},

getMinValue(arr, ...values) {
const array = arr.concat(values);
return this.containsDates(array) ?
new Date(Math.min(...array)) :
Math.min(...array);
},

/**
* Split array into subarrays using a delimiter function. Items qualifying as
* delimiters are excluded from the subarrays. Functions similarly to String.split
*
* Example:
* const array = [1, 2, 3, "omit", 4, 5, "omit", 6]
* splitArray(array, (item) => item === "omit");
* => [[1, 2, 3], [4, 5], [6]]
*
* @param {Array} array An array of items
* @param {Function} delimiterFn A function indicating values to be used as delimiters
* @returns {Object} Array of subarrays
*/
splitArray(array, delimiterFn) {
let segmentStartIndex = 0;
const segments = array.reduce((memo, item, index) => {
if (delimiterFn(item)) {
memo = memo.concat([array.slice(segmentStartIndex, index)]);
segmentStartIndex = index + 1;
} else if (index === array.length - 1) {
memo = memo.concat([array.slice(segmentStartIndex, array.length)]);
}
return memo;
}, []);

return segments.filter((segment) => {
return Array.isArray(segment) && segment.length > 0;
});
},

/**
* Takes an array of arrays. Returns whether each subarray has equivalent items.
* Each subarray should have two items. Used for componentShouldUpdate functions.
*
* Example:
* const propComparisons = [
* [x, nextProps.x],
* [y, nextProps.y],
* [style, this.style]
* ];
*
* allSetsEqual(propComparisons);
* => true
*
* @param {Array} itemSets An array of item sets
* @returns {Boolean} Whether all item comparisons are equal
*/
allSetsEqual(itemSets) {
return itemSets.every((comparisonSet) => {
return isEqual(comparisonSet[0], comparisonSet[1]);
});
},

/*
`areVictoryPropsEqual` does the following:
- marks any two Functions as equal
- returns false when checking the equality of things like `1` vs. `Object(1)`
(see the tests for more specifics)
*/
areVictoryPropsEqual(a, b) {
return isEqual(a, b);
}
containsDates,
containsNumbers,
containsOnlyStrings,
containsStrings,
getMaxValue,
getMinValue,
isArrayOfArrays,
removeUndefined
};
Loading