All right.
So here comes the next problem that we have to solve.
As I showed you before, if we head over to compose and we created a new post that just had a title of
Test an our URL if we type posts/Test spelt at exactly the same way as our title when we hit
enter then we get match found
right?
But here's a problem. Let's say if I go into my Atom and if the requestedTitle is not equal to any of
the store title I have, I'm going to use else
and then I'm going to log not a match.
So now if I again go and create my Test post
and now inside my home page I've got home and test. I can see that I've got that there
but in the URL
if I put /posts/test with a lowercase 't', in this case once I hit enter inside my
console you can see I get not a match.
And that is because in our code we're testing to see if storedTitle is equal to requestedTitle.
And if the requestedTitle had a lowercase t and our storedTitle because it's a title right?
It probably has a title case right?
So most of the words are capitalized. Then
this is not going to be true.
But in most cases, URLs should be lowercase. so it doesn't really make sense to put in a capital
T in there, it looks very very weird.
The other problem we have is that when you look at some of the blog posts say medium right?
You can see that they use word - word as their formatting for the URL.
And this is what we call kebab case in programming.
Now if I had a post that has two words in his title and I tried to use kebab case on it, say /
posts/
Another-post,
then it's also going to be not a match
right?
How do we solve this?
I want to introduce you to something that developers use a lot with Node which is something called Lodash.
And this is simply a utility library that makes it easier to work with Javascript inside your Node apps.
If you head over here to lodash.com you can see how to install it, how to use it inside Node.
Now here comes the challenge.
The goal of this challenge is for you to read through the documentation of Lodash
and I want you to have experience and practice in implementing the documentation.
We've already used a couple of libraries now such as Express, EJS, Requests and I want you to have practice
of implementing the documentation in a safe kind of environment
right? Because I'll show you how to do it very soon.
But I want you to give it a go.
And once you've installed it and required it, then I want you to head over to the documentation. And if
you search for lowercase then you can see that they have a method that allows you to turn all strings
into a lower case.
And if you look at the example it actually goes much further than that. It will in fact ignore all of
the hyphens or the underscores and it will simply just give you a pure string to work with.
So you should be able to use this library in order to make this URL actually give us a match for this
particular title.
When I type in this URL after you are done with implementing Lodash
and when I hit enter, in our console we should see match found. Pause the video and try to complete this challenge.
So here's hint number 1.
If you check out lodash.com you can see they tell you how to install it and it shows you how to do
it using NPM.
And this is pretty familiar to us by now. We just have to install Lodash right?
So let's go ahead and do that.
Let's head over to hyper and let's Control + C to quit our current nodemon session. And
we're going to use npm i or npm install whichever way you prefer.
And then we're going to spell out lodash and make sure that you have the right spelling.
Then we hit enter
and that gets installed to our current project.
The next thing you see here in the documentation is how to use it using Node.js which is perfect
right?
And you can see this familiar require thing that we've got going on.
And the thing about Lodash is that a Lodash actually refers to this which is known to some people
as an underscore.
And so their preferred way of working with Node is to create a new variable or constant
if you're using your versions of Javascript that is simply just a underscore and you set that to be
the Lodash library.
And then when you go to the documentation and you want to use one of its methods say the one we want
which is lower case, then you simply have underscore or lodash .lowercase and then in between
you pass in the string that you want to change into lowercase.
See if that is enough of a hint to be able to complete the challenge.
So here comes hint
number 2. In order for our requestedTitle to match with our storedTitle we need to reformat them so they're
both in the same format.
And it doesn't matter whatever it is that the user typed in here. It could be uppercase, lowercase, kebab
case, snake case, whatever it may be.
We want to convert it to the same format in order to work with it and compare it.
And that means that we have to format the storedTitle using that same lowercase method as well so that
they are equatable.
Pause the video now and see if you can now complete the challenge.