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