DEV Community

Nikema
Nikema

Posted on

Collab Lab Week 4 - Tuesday

I'm tired so I'll keep it very short today. Tuesdays are park days for my kids and me so I don't tend to be very productive on this day. At least the afternoon and evening is dedicated to park day.

As far as progress on the user story goes, I did some thinking, some doc reading, some video watching, and a tiny bit of playing with code. We need to check the user input for duplicates in the database and part of that task means stripping punctuation from the input and normalizing capitalization. My mind instantly went to RegEx when I saw the acceptance criteria.

I kind of have a fear of regular expressions but I also knew this would be an easily googleable problem to solve. I played with some inputs and RegEx in my node REPL.

This is what I came up with:

input = input.replace(/[^a-zA-Z\s]/g, "").toLowerCase()
// will strip out all non-letters except whitespace, then lowercase the string

The only problem is if a user puts in extra spaces for some reason those spaces don't get trimmed.

node REPL

I'm meeting again with Mike tomorrow. At least I'll be able to show I've been thinking about how to approach the problem. I also posted the links I looked at today to the wiki.

Top comments (3)

Collapse
 
eggorybarnes profile image
eggorybarnes

You could try something like,

input.replace(/\s+\s/g, " ")

to replace all multiple spaces with a single space.

Collapse
 
nikema profile image
Nikema

Thank you. We talked in slack about the acceptance criteria for this story and I'm going to change my approach. Instead of limiting to letters only, I'll exclude the punctuation. The product owner let me know that we need to be able to accept accented letters and numbers.

Collapse
 
nikema profile image
Nikema

I ended up using this afterall!

const normalizeName = name => {
  return name
    .replace(/[,.;:!-?]/g, '')
    .toLowerCase()
    .replace(/\s+\s/g, ' ')
    .trim();
};