Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,460 --> 00:00:05,350
Our conversion pipe definitely works but I think you'll agree with me that it's not very full featured
2
00:00:05,350 --> 00:00:06,120
right now.
3
00:00:06,130 --> 00:00:11,950
We called this thing convert pipe but it can only do a conversion from miles over 2 kilometers.
4
00:00:11,950 --> 00:00:17,080
Let's figure out how we can add in some custom argument to this thing to customize exactly exactly what
5
00:00:17,080 --> 00:00:23,230
units we convert those miles into do so we can go back over to our app component template and on our
6
00:00:23,230 --> 00:00:29,320
convert pipe we can add on some syntax very similar to some of the syntax we saw earlier where we were
7
00:00:29,320 --> 00:00:36,220
using some other built in pipes and we were customizing how those pipes were behaving right after convert.
8
00:00:36,220 --> 00:00:42,070
Right here I'll put down a colon and then a string and inside there I'm going to add in about in any
9
00:00:42,070 --> 00:00:43,380
four meters.
10
00:00:43,390 --> 00:00:49,230
This means that I want to take that Miles value right there and I want to convert it into meters instead
11
00:00:49,230 --> 00:00:50,740
of kilometers.
12
00:00:50,760 --> 00:00:55,520
Let's save this and then go back over to our pipe and figure out how we can somehow receive that argument.
13
00:00:57,260 --> 00:01:01,440
The makeover inside my pipe you'll notice that there's the second argument right here of dot dot dot
14
00:01:01,590 --> 00:01:02,420
args.
15
00:01:02,490 --> 00:01:07,320
If you've never seen the syntax before it essentially means take whatever other arguments were provided
16
00:01:07,320 --> 00:01:13,320
to the transform function and stuffed them all into a single array called args if we console log args
17
00:01:13,350 --> 00:01:20,990
outright now and then save this and flip back over I can then type inside the miles and put right here
18
00:01:20,990 --> 00:01:26,750
and I can see what args is though it is a array with one element inside of it just that in that we had
19
00:01:26,750 --> 00:01:32,450
added in as we start to add in more arguments to our pipe they're all gonna show up inside of that args
20
00:01:32,600 --> 00:01:33,820
array.
21
00:01:33,930 --> 00:01:38,400
For example if I decided that in addition to that meter's input right here if for some reason I also
22
00:01:38,400 --> 00:01:43,550
wanted to have some like other value inside if you're like some number for who knows what reason I save
23
00:01:43,560 --> 00:01:45,280
that and flip back over.
24
00:01:45,280 --> 00:01:49,980
Now when I type something in I'm going to see first the M and then whatever other value I threw inside
25
00:01:49,980 --> 00:01:50,790
there.
26
00:01:51,090 --> 00:01:55,970
If we need multiple different arguments to pass in we just separate them all with the colon.
27
00:01:56,190 --> 00:01:59,230
In our case we really just need a designation of the output units.
28
00:01:59,250 --> 00:02:06,200
So again in this case meters we only really expect to receive one argument and usually best practice
29
00:02:06,230 --> 00:02:08,450
is to say that rather than passing in like this.
30
00:02:08,450 --> 00:02:10,840
Dot dot dot args are receiving that argument.
31
00:02:11,030 --> 00:02:15,920
We might instead specifically say hey you got to give us some kind of conversion element or some kind
32
00:02:15,920 --> 00:02:23,250
of conversion unit here I might call that or get units and say it needs to be a string.
33
00:02:23,290 --> 00:02:32,380
Now I can print out simply or get units by itself and if I say this enter number no longer do I have
34
00:02:32,380 --> 00:02:32,900
the array.
35
00:02:32,950 --> 00:02:34,010
I just have the m..
36
00:02:34,030 --> 00:02:36,140
I had entered okay.
37
00:02:36,180 --> 00:02:41,700
Let's figure out how we can adjust the logic inside of here to convert the input miles into some different
38
00:02:41,700 --> 00:02:47,670
target units based upon that target units arguments to do so I think maybe a good way to handle this
39
00:02:47,670 --> 00:02:49,550
would be a switch statement.
40
00:02:49,620 --> 00:02:54,180
We can delete that console log I'll put a switch statement set off here and we're gonna switch over
41
00:02:54,180 --> 00:03:00,160
the value of target units and we'll write out a couple of cases here.
42
00:03:00,300 --> 00:03:07,470
If Target units is equal to about limiters we'll start with that first and let's return value times
43
00:03:07,610 --> 00:03:14,360
one point 6 0 9 3 4 we can also add in a case to handle say meters.
44
00:03:14,600 --> 00:03:16,320
And that would be the same thing.
45
00:03:17,180 --> 00:03:24,050
We would multiply that by 1000 and it may be one more case that's for demonstration.
46
00:03:24,170 --> 00:03:31,270
We'll do a conversion over two centimeters I'd take the same thing and multiply it once again by a thousand.
47
00:03:31,300 --> 00:03:33,710
I might also want to put in a default case as well.
48
00:03:33,760 --> 00:03:38,230
In case someone tries to use my pipe and throws in some kind of target unit that I don't know how to
49
00:03:38,230 --> 00:03:44,040
handle I could put in a default right here and it's really up to us how to best handle this.
50
00:03:44,110 --> 00:03:48,700
We could just return our value and say well if you give us some kind of conversion metric that we don't
51
00:03:48,700 --> 00:03:53,410
understand let's just throw the value back at whoever is using this pipe but maybe a better way would
52
00:03:53,410 --> 00:03:54,670
be to throw an error.
53
00:03:54,700 --> 00:04:00,850
So a signal to other engineers that hey you're trying to convert into a unit that is not supported we
54
00:04:00,850 --> 00:04:10,880
could instead throw a new error and say ah get unit not supported except all right.
55
00:04:10,910 --> 00:04:18,950
Let's say this look back over now when I type inside of here if I say 1 mile I get 16 0 9 meters which
56
00:04:19,070 --> 00:04:20,880
I don't know sounds right to me.
57
00:04:21,160 --> 00:04:25,230
And if I go back to my template I can now change that to say kilometers.
58
00:04:25,250 --> 00:04:31,150
So now convert miles in two kilometers yep that looks good.
59
00:04:31,150 --> 00:04:38,260
And I can change convert over to the M instead give me miles in centimeters though one mile is many
60
00:04:38,260 --> 00:04:40,210
many many centimeters.
61
00:04:40,210 --> 00:04:40,480
Okay.
62
00:04:40,520 --> 00:04:43,190
So that's how we add in custom arguments to a pipe.
63
00:04:43,200 --> 00:04:47,740
We've got to do is use the same syntax we already saw around the built in pipes and we received that
64
00:04:47,770 --> 00:04:53,300
argument as additional arguments inside this transform function over here.
65
00:04:53,410 --> 00:04:54,950
So it looks good.
66
00:04:54,950 --> 00:04:59,640
Now in the next video there's a couple of other kind of gotchas around pipes I want you to be aware
67
00:04:59,640 --> 00:05:00,100
of.
68
00:05:00,150 --> 00:05:02,730
We're gonna take a look at a couple of corner cases in just a moment.
7488
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.