4

I've created a node.js website that potential employees can apply for a job through. When they submit the application form I am using nodemailer and mailgun to send an email to the hiring manager with the applicants information. It sends the object of the new applicant with the key value pairs, but I would like to put a space between the keys containing more than one word and make them uppercase to be a little more eye pleasing. How can I do this?

here is a sample email with output key value pairs

enter image description here

Here is the code for sending an email

function sendAppliedEmail(applicant) {


let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key}: ${value}</li>`;
  });

  html += '</ul>';
klaurtar
  • 233
  • 3
  • 23

3 Answers3

7
replace(/([a-z])([A-Z])/g, `$1 $2`)


You can replace all lowerUpper matches.

Everytime a lowercase character followed by a uppercase character, the regex match and replace the both characters with themself and a space between.

function sendAppliedEmail(applicant) {


let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key.replace(/([a-z])([A-Z])/g, `$1 $2`)}: ${value}</li>`;
  });

  html += '</ul>';


if the key should be complete uppercase you can chain the toUpperCase() method to the replace method.

html += `<li>${key.replace(/([a-z])([A-Z])/g, `$1 $2`).toUpperCase()}: ${value}</li>`;
d-h-e
  • 2,478
  • 1
  • 10
  • 18
4

You can use the replace function to ascertain if a letter is upper case and return a prefixed space with the character if it is, while simultaneously making sure that the first character is capitalized.

key.replace(/[a-z]/gi, (m, o) => (m < {} && o) ? ` ${m}` : (o) ? m : m.toUpperCase())

Brief Explanation

You can determine whether or not a character is capitalized by comparing it to {}. If a character is less than it is capitalized, and if it is greater than it is lower case. This is because {} will be transformed to the string [object Object], and both the character codes will be compared. Upon comparison only the first character will be used("["). Thus "A" < "[" and "A" < {} is equivalent. This is easier explained through seeing their character codes:

"A".charCodeAt(0);
 //65
"[".charCodeAt(0);
 //91
"a".charCodeAt(0);
 //97

(In fact, it's more performant to use "[", and for optimization you would compare to that instead. It's simply easier to remember "{}" is greater than capital characters and vice versa)

Why do this?

In typical RegEx you could simply use [A-Z] to grab capitalized characters, but the caveat is that if you're looking to capitalize the first character AND space camel case, this comparison in addition to the offset parameter( think of it as an index of the string ) in the replace function allows you to do so in a singular pass through the string.


An Example:

let splitCase = key => key.replace(/[a-z]/gi, (m, o) => (m < "[" && o) ? ` ${m}` : (o) ? m : m.toUpperCase());

let key = "phoneNumber";
console.log(splitCase(key));

key = "firstName";
console.log(splitCase(key));

In Your Code:

The below snippet is non-functioning, but it should work within your code:

function sendAppliedEmail(applicant) {
let splitCase = (key) => key.replace(/[a-z]/gi, (m, o) => (m < "[" && o) ? ` ${m}` : (o) ? m : m.toUpperCase());

let html = '<img src="" alt="logo">';
  html += '<h2 style="color: black">New Applicant</h2>'
  html += '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${splitCase(key)}: ${value}</li>`;
  });

  html += '</ul>';
zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

You could slit them on the uppercase character with a regex. See this question: Javascript Split string on UpperCase Characters

n9iels
  • 867
  • 7
  • 21