All right. 1
So here comes the answer to the challenge. 2
First things first because Lodash is available to us through NPM, then we're going to install it in 3
the same way that we've installed Express EJS or Request to any of the previous libraries that we've 4
worked with. 5
Firstly we have to Control + C to quit our server and then we're going to run npm i or npm install and then 6
we're going to spell out lodash and then we'll let that install in the background. 7
Now the next thing we need to read in the documentation is how to use it using Node.js. And the way 8
that they prefer you to use the library is to create a new variable using a underscore or what is also 9
known as a low dash. 10
That is how some people might refer to that symbol. 11
Now in modern versions of Javascript we favor constant over var, so we're going to change that. 12
And then we're going to require lodash so that we tap into all of its capabilities. 13
Let's go into Atom and do that right at the top. 14
So we're going to create a new const and it's going to be a underscore or a lodash. And then we'll 15
get to set that to equal require the package that is lodash. 16
And now we have access to all of the utilities in this library. 17
Now as I mentioned, the particular one that we're interested in is the one that turns everything into 18
a lower case and ignores all of the dashes or the underscores so that we get a clear text that we can 19
work with and we can compare. In order to use this method they've shown you appear that you simply write 20
underscore, which refers to the lodash utility library, .and then we specify the name of the method 21
which is lowercase and then inside some parentheses we specify a string. 22
Let's go ahead and do that over here. 23
Now we want to convert our requested title from whatever format it is that the user put in here. 24
They could have an uppercase, lowercase, kebab case, snake case, whatever it is they decided to use. 25
We're going to convert it to the same format by using that lowercase method. 26
So now we're going to say const requestedTitle is equal to underscore or lodash .lowercase and then 27
we're going to open a set of parentheses and inside here is where we're going to put our string. 28
Now if you only want to convert a single string then you can simply specify it like this. 29
But if you have an array of strings then you can also put it into an array like so. 30
Both methods will work. 31
So now we have our requestedTitle converted to lowercase, but our storedTitle is still capitalized right? 32
And so we have to apply the same method to the storedTitle. 33
So let's do the same down here. _.lowercase open a set of parentheses and include the 34
thing that we want to convert which is our post.title. 35
So now if we hit save and we head back into our console and we restart our Node server and then we go 36
ahead and create a brand new post, just go over to compose let's just call it Another post and then I'm going 37
to paste in some lorem ipsum, hit publish 38
and now I have access to that. 39
And then over here in the URL I'm going to tap into the route that is /posts/ 40
another-post and the URL is all in lowercase whereas our title is-- has a capitalized another and then 41
it's got a space and then it's got post. 42
But I'm going to use kebab case over here 43
so I have a hyphen between another and post. 44
And now when I hit enter and I check my console, I still get a match found because we've converted this 45
title and this part of the parameter both into lowercase, we've thrown away all the hyphens or underscores 46
and we just have a clear text that both now match. As long as those strings match and they're in the 47
right order then we will get a match. 48
And this is going to set us up for the next challenge.