Last active
November 24, 2017 21:33
-
-
Save squadette/af0a8a4ef0617774efad6db535a3a7fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
export default class OxfordComma extends React.Component { | |
render () { | |
const count = React.Children.count(this.props.children); | |
if (count === 0 || count === 1) { | |
return <span>{this.props.children}</span>; | |
} | |
if (count === 2) { | |
return <span>{React.Children.map(this.props.children, (child, ndx) => { | |
if (ndx === 0) { | |
return <span>{child} and </span>; | |
} else { | |
return child; | |
} | |
})}</span>; | |
} | |
return <span>{React.Children.map(this.props.children, (child, ndx) => { | |
if (ndx < count - 2 ) { | |
return <span>{child}, </span>; | |
} else if (ndx === count - 2) { | |
return <span>{child}, and </span>; | |
} else { | |
return child; | |
} | |
})}</span>; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment