Isaac
Isaac

Reputation: 12874

How to compose in this situation

var articles = [
  {
    title: 'Everything Sucks',
    author: { name: 'Debbie Downer' }
  },
  {
    title: 'If You Please',
    author: { name: 'Caspar Milquetoast' }
  }
];

var names = _.map(_.compose(_.get('name'), _.get('author'))) 
// returning ['Debbie Downer', 'Caspar Milquetoast']

Now based on the above given articles and function names, make a boolean function that says whether a given person wrote any of the articles.

isAuthor('New Guy', articles) //false
isAuthor('Debbie Downer', articles)//true

My attempts on below

var isAuthor = (name, articles) => {
    return _.compose(_.contains(name), names(articles))
};

However it's failing on jsbin with error below. Perhaps someone can try to explain what goes wrong with my attempt so that I can learn from mistake

Uncaught expected false to equal function(n,t){return r.apply(this,arguments)}

Upvotes: 1

Views: 58

Answers (1)

Ori Drori
Ori Drori

Reputation: 191936

Compose returns a function, so you need to pass articles to that function. Compose will pass the articles to getNames, and will pass the result of getNames to contains(name) (which also returns a function) that will handle the author names, and return the boolean result:

const { map, path, compose, contains } = R

const getNames = map(path(['author', 'name']))

const isAuthor = (name) => compose(
  contains(name),
  getNames
)

const articles = [{"title":"Everything Sucks","author":{"name":"Debbie Downer"}},{"title":"If You Please","author":{"name":"Caspar Milquetoast"}}]

console.log(isAuthor('New Guy')(articles)) //false
console.log(isAuthor('Debbie Downer')(articles)) //true
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Upvotes: 1

Related Questions