Skip to content

Instantly share code, notes, and snippets.

@sdtsui
Created May 3, 2016 06:07
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 sdtsui/462008cca41999b97ad7e0fe3b52d21c to your computer and use it in GitHub Desktop.
Save sdtsui/462008cca41999b97ad7e0fe3b52d21c to your computer and use it in GitHub Desktop.
siteRow-fp.js
// @flow
import React from 'react';
import styles from './SiteRow.css';
import classnames from 'classnames';
import _ from 'lodash/fp';
import R from 'ramda';
/**
* Vanilla JS
*/
const _mapPropsToIds = (props: Object): string => {
let ids;
if (props.offerIDs) {
ids = props
.offerIDs
.map( id => id.toString())
.join(", ");
}
let idText = `Offer ID(s): ${ids}`;
return idText;
}
/**
* Lodash-FP
*/
const _mapPropsToIds = (props) => {
let ids;
if (props.offerIDs) {
let getStrings = _.compose(
_.map(_.toString),
_.property('offerIDs'),
);
ids = getStrings(props).join(", ");
}
let idText = `Offer ID(s): ${ids}`;
return idText;
}
/**
* Ramda
*/
const _applyJoinWithSpacer = (spacer, array) => {
return array.join(spacer);
};
const _mapPropsToIds = R.ifElse(
R.compose(R.is(Array), R.prop('offerIDs')),
R.curry(
R.compose(
R.concat('Offer ID(s): '),
R.curry(_applyJoinWithSpacer)(", "),
R.curry(R.map(R.toString)),
R.curry(R.prop('offerIDs')),
)
),
function() {return "...loading.";}
);
const SiteRow = (props) => {
let idText = _mapPropsToIds(props);
return (
<div>
{idText}
</div>
);
}
export default SiteRow;
@sdtsui
Copy link
Author

sdtsui commented May 3, 2016

const _mapPropsToIds = R.compose(
  R.concat("Offer ID(s): "),
  R.ifElse(
    R.compose(R.is(Array), R.prop('offerIDs')),
    R.compose(
      R.join(', '),
      R.prop('offerIDs')
    ),
    () => ''
  )
);

const joinByComma = R.compose(R.join(', '), R.prop('offerIDs'));
const offerIDsIsArray = R.compose(R.is(Array), R.prop('offerIDs'))
const prependOfferIDText = R.concat("Offer ID(s): ");
const passEmptyString = () => '';
const applyJoinIfArray = R.ifElse(offerIDsIsArray, joinByComma, emptyString);

const _mapPropsToIds = R.compose(prependOfferIDText, applyJoinIfArray);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment