How to do complex conditionals or regex in jade

How to do complex conditionals or regex in jade

Problem

I’m having an interesting challenge and can’t figure out how to go about solving it. I’m pulling a list of subscribers from my mailchimp and populating a members’ page on my website with the data. One of the field in my sign up for says, “Website or LinkedIn profile url.”

What I’d like to do is, check if they have provided a linkedin address, and if so drop their url in the linkedin member plugin, if not use the other data to create a simpler profile, name website, headline etc.

My problem is I can’t say if data.websiteField.match(/linkedin/) within jade, so I either need to pass the data to some client side javascript which I’m having trouble with or do something else.

Here’s a sample of data returned from mailchimp

[ // this first bit is just the fields
  [
    "email address",
    "first name",
    "last name",
    "role",
    "organization",
    "headline",
    "website",
    "areas of interest",
    "please email me about:",
    "member_rating",
    "optin_time",
    "optin_ip",
    "confirm_time",
    "confirm_ip",
    "latitude",
    "longitude",
    "gmtoff",
    "dstoff",
    "timezone",
    "cc",
    "region",
    "last_changed"
  ],
  [ // and here's the first user
    "woop@booomshakala.com", // haha, just hiding some data
    "Woop",
    "Shak",
    "Tester",
    "ShakaCo",
    "Whooooooper",
    "http://linkedin.com/in/costamichailidis", // hit me up sometime
    "Creativity, Innovation, Ideas and Science",
    "Job Placement, Internships, Recruitment & Retention, Technology & MOOCs, Measurement & Evaluation, Documentation & Dissemination",
    2,
    "2013-03-28 19:28:55",
    "173.52.122.111",
    "2013-03-28 19:29:12",
    "173.52.122.111",
    "40.7648000",
    "-73.7775000",
    "-5",
    "-4",
    "America/New_York", "US", "NY", "2013-03-28 19:29:12"
  ]
]

Any help would rock!

Also, I’m using express. Does express make locals available in client side javascript?

Problem courtesy of: Costa

Solution

Client Side Regex

Jade allows you execute arbitrary javascript using the - modifier at the start of the line

- if (profile.websiteField.match(/linkedin/)
    // render linkedin profile here
- else
    // render simple profile here

Server Side Regex

I think it would be simpler to format the profile information server-side and set a renderLinkedIn boolean field to true or false

function displaySignUpPage(req, res) {
  var profile = formatMailChimpData()
  // profile now looks like
  // {
  //   "email address": "woop@booomshakala.com",
  //   "first name": "Noah",
  //   ...
  // }

  var linkedInRegex = /linkedin/;
  profile.renderLinkedIn = linkedInRegex.test(profile.website) // set renderLinkedIn to true or false
  // say your jade view is called signUpPage.jade
  var pageData = {
    title: 'Register',
    profile: profile
  }
  res.render('signUpPage', pageData)
}


function formatMailChimpData() {
  var mailChimpData = [
    [
      "email address",
      "first name",
      "last name",
      "role",
      "organization",
      "headline",
      "website"
      // other fields truncated
    ],
    [
      "woop@booomshakala.com", // haha, just hiding some data
      "Noah",
      "Isaacson",
      "Tester",
      "ShakaCo",
      "Whooooooper",
      "http://www.linkedin.com/pub/noah-isaacson/59/6a2/553"
    ]
  ]


  // mailChimp puts the keys as the first entry
  var mailChimpFields = mailChimpData[0]
  var mailChimpProfile = mailChimpData[1]
  // make profile into key-value pairs
  var keyValuePairs = mailChimpFields.map(function (field, index) {
    var profileValue = mailChimpProfile[index]
    var keyValuePair = [field, profileValue]
    return keyValuePair
   })
  var profile = keyValuePairs.reduce(function(prev, pair) {
    var key = pair[0]
    var value = pair[1]
    prev[key] = value
    return prev
  }, {})
  return profile
}
Solution courtesy of: Noah

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *