Skip to content

Commit

Permalink
Clean code (#8)
Browse files Browse the repository at this point in the history
* Refine flow types

* Clean code
  • Loading branch information
arniu authored Mar 30, 2018
1 parent cde9b9e commit b34d354
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 49 deletions.
22 changes: 8 additions & 14 deletions src/Sparkline.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as typed from './typed'

type Props = typed.GraphProps

const makeContext = ({
const createHelpers = ({
data,
width,
height,
Expand Down Expand Up @@ -38,18 +38,12 @@ const makeContext = ({
return {
scaleX,
scaleY,
points,
width,
height,
padding,
data,
max,
min
points
}
}

const Sparkline = (props: Props) => {
const context = makeContext(props)
const Sparkline = ({ children, ...props }: Props) => {
const drawHelpers = createHelpers(props)

return (
// prettier-ignore
Expand All @@ -58,11 +52,11 @@ const Sparkline = (props: Props) => {
height={props.height}
style={props.style}
>
{React.Children.map(props.children, child =>
{React.Children.map(children, child =>
React.cloneElement(child, {
...helper.filterProps(props),
...helper.filterProps(child.props),
...context
...props,
...child.props,
...drawHelpers
})
)}
</ART.Surface>
Expand Down
30 changes: 0 additions & 30 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,3 @@ export function sample<T> (xs: T[], sampling: number): T[] {
return i === cursor
})
}

const acceptedProps = [
'sampling', // sampling rate
'height', // maybe we should drop it
'width', // maybe we should drop it
'color', // fallback color
'stroke',
'strokeWidth',
'strokeDash',
'strokeCap',
'strokeJoin',
'opacity',
'fill'
]

/**
* Pick shape style
*
* @param {{[string]:any}} props
* @return {{[string]:any}}
*/
export function filterProps (props: Object): Object {
return acceptedProps.reduce((obj, key) => {
if (key in props) {
obj[key] = props[key]
}

return obj
}, {})
}
10 changes: 5 additions & 5 deletions src/typed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ export type GraphProps = {
sampling?: number,
limit?: number,
max?: number,
min?: number
}
min?: number,

export type GraphStyle = {
color?: string,
stroke?: string,
strokeWidth?: number,
opacity?: number,
fill?: string
}

export type ShareProps = {
export type DrawHelpers = {
scaleX: number => number,
scaleY: number => number,
points: Point[]
} & GraphProps
}

export type ShareProps = GraphProps & DrawHelpers

0 comments on commit b34d354

Please sign in to comment.