Skip to content

Instantly share code, notes, and snippets.

@cab
Created March 15, 2016 15:48
Show Gist options
  • Save cab/884c8787bb82c6afa81b to your computer and use it in GitHub Desktop.
Save cab/884c8787bb82c6afa81b to your computer and use it in GitHub Desktop.

lodash (no safety)

_(comments)
  .head()
  .pluck('examples')
  .head()
  .pluck('code')
  .value()

ramda (no safety)

compose(
  prop('code'),
  head,
  prop('examples')
  head
)(comments)

imperative (runtime safety)

let _exampleCode = null;

if(comments.length > 0) {
  const headComment = comments[0];
  if(headComment.examples !== null && headComment.examples !== undefined) {
    if(typeof head.examples === 'array') {
      if(head.examples.length > 0) {
        const headExample = head.examples[0];
        if(headExample.code !== null && headExample.code !== undefined) {
          if(typeof headExample.code === 'string') {
            _exampleCode = headExample.code;
          }
        }
    }
  }  
}

sanctuary (runtime safety)

head(comments).chain(get(Array, 'examples')).chain(head).chain(get(String, 'code'))
@ryedin
Copy link

ryedin commented Mar 15, 2016

Maybe just personal preference, but I would find this easier to parse in the multi-line form:

head(comments)
  .chain(get(Array, 'examples'))
  .chain(head)
  .chain(get(String, 'code'))

Other than that I totally agree S (or something like it) is the way to go for this.

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