Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@squadette
Last active November 24, 2017 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squadette/af0a8a4ef0617774efad6db535a3a7fd to your computer and use it in GitHub Desktop.
Save squadette/af0a8a4ef0617774efad6db535a3a7fd to your computer and use it in GitHub Desktop.
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