Skip to content

Commit 8d66fb5

Browse files
committed
add recipe for lowercasing a string
1 parent 03a46f3 commit 8d66fb5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: recipe
3+
title: Lowercasing a String
4+
chapter: Strings
5+
---
6+
## Problem
7+
8+
You want to lowercase a string.
9+
10+
## Solution
11+
12+
Use JavaScript's String toLowerCase() method:
13+
14+
{% highlight coffeescript %}
15+
"ONE TWO THREE".toLowerCase()
16+
# => 'one two three'
17+
{% endhighlight %}
18+
19+
## Discussion
20+
21+
`toLowerCase()` is a standard JavaScript method. Don't forget the parentheses.
22+
23+
### Syntax Sugar
24+
25+
You can add some Ruby-like syntax sugar with the following shortcut:
26+
27+
{% highlight coffeescript %}
28+
String::downcase = -> @toLowerCase()
29+
"ONE TWO THREE".downcase()
30+
# => 'one two three'
31+
{% endhighlight %}
32+
33+
The snippet above demonstrates a few features of CoffeeScript:
34+
35+
* The double-colon `::` is shorthand for saying `.prototype.`
36+
* The "at" sign `@` is shorthand for saying `this.`
37+
38+
The code above compiles in to the following JavaScript:
39+
40+
{% highlight javascript %}
41+
String.prototype.downcase = function() {
42+
return this.toLowerCase();
43+
};
44+
"ONE TWO THREE".downcase();
45+
{% endhighlight %}

0 commit comments

Comments
 (0)