Skip to content

Commit

Permalink
chore(core): removing find function
Browse files Browse the repository at this point in the history
Using the built in Array.find instead.
  • Loading branch information
markmcdowell committed Jul 9, 2020
1 parent ec4c43e commit 0b48e94
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 39 deletions.
42 changes: 20 additions & 22 deletions packages/core/src/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { scaleLinear } from "d3-scale";
import * as PropTypes from "prop-types";
import * as React from "react";

import { find, PureComponent } from "./utils";
import { PureComponent } from "./utils";

interface ChartProps {
readonly flipYScale?: boolean;
readonly height?: number;
readonly id: number | string;
readonly onContextMenu?: (props: any, event: React.MouseEvent) => void;
readonly onContextMenu?: (event: React.MouseEvent, props: unknown) => void;
readonly origin?: number[] | ((width: number, height: number) => number[]);
readonly padding?: number | { top: number; bottom: number };
readonly yExtents?: number[] | ((data: any) => number) | ((data: any) => number[]);
Expand Down Expand Up @@ -56,28 +56,10 @@ export class Chart extends PureComponent<ChartProps> {
unsubscribe(`chart_${id}`);
}

public readonly listener = (type, moreProps, state, e) => {
const { id, onContextMenu } = this.props;

if (type === "contextmenu") {
const { currentCharts } = moreProps;
if (currentCharts.indexOf(id) > -1) {
if (onContextMenu !== undefined) {
onContextMenu(moreProps, e);
}
}
}
};

public readonly yScale = () => {
const chartConfig = find(this.context.chartConfig, (each) => each.id === this.props.id);
return chartConfig.yScale.copy();
};

public getChildContext() {
const { id: chartId } = this.props;

const chartConfig = find(this.context.chartConfig, (each) => each.id === chartId);
const chartConfig = this.context.chartConfig.find((each) => each.id === chartId);

return {
chartId,
Expand All @@ -86,10 +68,26 @@ export class Chart extends PureComponent<ChartProps> {
}

public render() {
const { origin } = find(this.context.chartConfig, (each) => each.id === this.props.id);
const { origin } = this.context.chartConfig.find((each) => each.id === this.props.id);

const [x, y] = origin;

return <g transform={`translate(${x}, ${y})`}>{this.props.children}</g>;
}

private readonly listener = (type: string, moreProps, state, e) => {
const { id, onContextMenu } = this.props;

switch (type) {
case "contextmenu": {
const { currentCharts } = moreProps;
if (currentCharts.indexOf(id) > -1) {
if (onContextMenu !== undefined) {
onContextMenu(e, moreProps);
}
}
break;
}
}
};
}
4 changes: 2 additions & 2 deletions packages/core/src/GenericChartComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as PropTypes from "prop-types";

import { GenericComponent } from "./GenericComponent";
import { find, isDefined } from "./utils";
import { isDefined } from "./utils";

const ALWAYS_TRUE_TYPES = ["drag", "dragend"];

Expand Down Expand Up @@ -84,7 +84,7 @@ export class GenericChartComponent extends GenericComponent {

if (chartConfigList && Array.isArray(chartConfigList)) {
const { chartId } = this.context;
const chartConfig = find(chartConfigList, (each) => each.id === chartId);
const chartConfig = chartConfigList.find((each) => each.id === chartId);
this.moreProps.chartConfig = chartConfig;
}
if (isDefined(this.moreProps.chartConfig)) {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/utils/ChartDataUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as React from "react";
import { Chart } from "../Chart";

import {
find,
functor,
getClosestItem,
isDefined,
Expand Down Expand Up @@ -50,7 +49,7 @@ function isArraySize2AndNumber(yExtentsProp) {
return false;
}

export function getNewChartConfig(innerDimension, children, existingChartConfig = []) {
export function getNewChartConfig(innerDimension, children, existingChartConfig: any[] = []) {
return React.Children.map(children, (each) => {
if (each && each.type.toString() === Chart.toString()) {
const chartProps = {
Expand All @@ -76,7 +75,7 @@ export function getNewChartConfig(innerDimension, children, existingChartConfig
? (Array.isArray(yExtentsProp) ? yExtentsProp : [yExtentsProp]).map(functor)
: undefined;

const prevChartConfig = find(existingChartConfig, (d) => d.id === id);
const prevChartConfig = existingChartConfig.find((d) => d.id === id);

if (isArraySize2AndNumber(yExtentsProp)) {
if (
Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ export function getClosestValue(inputValue, currentValue) {
return currentValue + diff;
}

// @ts-ignore
export function find(list, predicate, context = this) {
for (let i = 0; i < list.length; ++i) {
if (predicate.call(context, list[i], i, list)) {
return list[i];
}
}
return undefined;
}

export function d3Window(node) {
const d3win =
node && ((node.ownerDocument && node.ownerDocument.defaultView) || (node.document && node) || node.defaultView);
Expand Down
4 changes: 2 additions & 2 deletions packages/interactive/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find, isDefined, isNotDefined, mapObject } from "@react-financial-charts/core";
import { isDefined, isNotDefined, mapObject } from "@react-financial-charts/core";

export function getValueFromOverride(override, index, key, defaultValue) {
if (isDefined(override) && override.index === index) {
Expand Down Expand Up @@ -69,7 +69,7 @@ function getMouseXY(moreProps, [ox, oy]) {

export function getMorePropsForChart(moreProps, chartId) {
const { chartConfig: chartConfigList } = moreProps;
const chartConfig = find(chartConfigList, (each) => each.id === chartId);
const chartConfig = chartConfigList.find((each) => each.id === chartId);

const { origin } = chartConfig;
const mouseXY = getMouseXY(moreProps, origin);
Expand Down

0 comments on commit 0b48e94

Please sign in to comment.