Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's make use of the ability to chain methods together.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Okay, so you just started a new
job at a startup called composure.
0:04
And the concept here is we keep our cool,
it doesn't go away no fear of missing out,
0:08
we keep our cool.
0:12
So the project is like this,
0:14
there's a bunch of fields that users can
fill things out on their profile, right?
0:16
And we started to allow HTML.
0:19
We said it was a bad idea, nobody really
listened, but you know it's a bad idea.
0:22
It started to look a lot
like My Space here.
0:26
The users love it, and
it's moving all over the place though like
0:28
almost every single field has this
concept of they can write their own HTML.
0:31
Not very cool.
0:36
So for an example, here's what happened.
0:37
So, we have some HTML here and
0:38
what we've noticed is that people
like to use the strong tag a lot.
0:41
So what we've done is we've
basically just removed it.
0:44
So I'm gonna go ahead and give this thing
a run so that you can see what happens.
0:46
So what happens, if we've gone
through this little utility here,
0:50
this a bold cleaner means some third party
libraries this is a grandle project.
0:54
So I've been using some third party
libraries adjacent for this one.
0:58
And basically, we get rid of the strong
tags, so see it's not down here.
1:02
See allows you to over promote.
1:05
It's not happening down here, so this will
help our method from this package here.
1:07
Now I'll strip strong tags.
1:11
So, strong was just the first problem that
we're having as we are building this do
1:13
your own HTML sort of thing.
1:17
The next thing that started happening is
people started putting their own links,
1:19
and people ended up
actually leaving the site.
1:22
So, what our design team asked is
if we could add a relic external
1:24
to any link that happens to be in there.
1:29
So let me just show you
what that looks like.
1:30
So, we'll get the result.
1:32
We're gonna take a thing
called externalize links and
1:34
I wanna parse in the results to that,
right.
1:37
So this, Strips the bold, and
1:40
this we'll, Add rel = external.
1:44
So, now you could see when I go and run
this, the link here has this rel external.
1:52
So one thing that we came up with as
a team was we wanted to have a utility
1:58
function that we could just kind
of use all over the place, right?
2:02
Because these fields are showing
up all over the place.
2:06
So in our utilities package,
we have a thing called Renderer.
2:09
And it has a method called render and
it takes input.
2:13
So we're going to take this htmlInput and
you can do things with it.
2:16
So we made it take a function, right?
2:21
Cuz that makes sense, cuz we wanna take
a function that filters those things.
2:23
So, what we did originally
was we just wanted to do,
2:25
both clearly like that, right.
2:28
And we'll go ahead,
let's store this in the result here.
2:30
So that takes care of this
this first one here but
2:36
it doesn't take care of this externalized
links, right, because there's a bunch.
2:38
So what we ended up having to do in
this is kind of gross, is we did this.
2:43
We took the text that was passed in and
we have a little land of body here.
2:47
So now, we have these two things going on.
2:51
So we'll grab the html and
will take the text in, and
2:54
then from here, we will grab
whatever came out of there and
2:59
externalize the links,right?
3:03
And say, let's just do this.
3:10
So return, html right?
3:12
Plus the comments when I went
back into that so here we go.
3:20
Still in the same thing basically right so
it's taken this function and
3:23
it runs a function on it.
3:26
Now this is definitely a step
in the right direction, right?
3:27
But I don't like the fact that
people are going to just copy and
3:30
paste this bit here,right, because we
need to do that every single time.
3:33
So, let's do this.
3:36
Let's look at these functions first.
3:38
So it's a string and it returns a string.
3:40
Each of the functions then here
take a string and return a string.
3:44
Let's play with this a little bit.
3:48
Let's see.
So
3:49
if I have a function and
we make it a string.
3:50
String.
3:55
Let's call this boldCleaner.
3:57
And we can just use
a method reference here.
4:01
So we can say Html:: boldCleaner; so
4:03
that's a function that we
have our handle on there.
4:05
So I'm gonna lose this, let's do this.
4:08
So then, what she want to do is after we
run this, we want to run externalize link.
4:12
So we want to run boldCleaner and
then we want to run externalizeLinks.
4:18
Look at that?
4:26
That is called composing a method, okay.
4:28
So we've composed the two.
4:31
So the first method runs, the results
from it is passed into the next one,
4:33
just like a chain.
4:36
So, as you can see I can
actually highlight this.
4:38
When I go ahead and extract a variable and
it called a filter.
4:42
Let's go ahead,
let's call this sanitizer, okay.
4:49
So there is now a sanitizer,
it does exactly what we wanted.
4:55
Now I can export this and
I can take that wherever I want it, right.
4:58
So that's pretty clean looking there,
isn't it?
5:01
And it's just doing those
little function pipeline there.
5:03
And again, it runs boldCleaner and
then it runs externalizeLinks, and
5:06
it pass a String all the way through.
5:11
So I don't know about you,
but I'm ready for lunch and
5:13
I'm about ready to head out when I see
a new feature request come through.
5:15
We now want to support Markdown.
5:19
Now, if Markdown is new to you,
it's a text format and
5:20
it's used all over the place.
5:24
I see it on GitHub, you'll see it on
a Wikis, different software bug trackers.
5:25
Now, it has a limited scope to HTML and
it's a lot easier to edit.
5:30
Check the teachers notes for more.
5:34
But we wanna do here is, we wanna make
sure that we can render Markdown.
5:36
Let's do that next video.
5:40
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up