Skip to content

Instantly share code, notes, and snippets.

@karloscodes
Last active March 6, 2018 13:17
Show Gist options
  • Save karloscodes/22ddcbadb47e389154cbc6ff6d5c481b to your computer and use it in GitHub Desktop.
Save karloscodes/22ddcbadb47e389154cbc6ff6d5c481b to your computer and use it in GitHub Desktop.
First steps using the Ramda library. Ramda, Javascript, Functional Programming

Example to find an object inside an array given a name then, returns its value

const R = require('ramda');

const sesNotification = {
  "notificationType": "Delivery",
  "mail": {
    "headers": [
      {
        "name": "Content-Type",
        "value": "text/html"
      },
      {
        "name": "List-Unsubscribe",
        "value": "http://somedomain.com"
      },
      {
        "name": "X-Moonmail-User-ID",
        "value": "google-oauth2|12345"
      }
    ]
  }
};

const tapLog = (...tags) => R.tap(
  (...values) => console.log(...tags, ...values))


const filterByHeaderName = R.curry((notification, header) =>
  R.pipe(
    R.path(['mail', 'headers']),
    R.find(R.propEq('name', header)),
    R.prop('value')
  )(notification)
);


const results = filterByHeaderName(sesNotification)('Content-Type');
console.log(results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment