Skip to content

Commit b72b261

Browse files
committed
Merge pull request coffeescript-cookbook#28 from phlipper/wanted-recipes
Added some "Wanted recipes"
2 parents 8332a0f + d00857d commit b72b261

8 files changed

+226
-5
lines changed

authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The following people are totally rad and awesome because they have contributed r
1515
* Aaron Weinberger _[email protected]_
1616
* James C. Holder _[email protected]_
1717
* Jason Giedymin _[email protected]_
18+
* Phil Cohen _[email protected]_
1819
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!
1920

2021
# Developers
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
layout: recipe
3+
title: Creating a String from an Array
4+
chapter: Arrays
5+
---
6+
## Problem
7+
8+
You want to create a string from an array.
9+
10+
## Solution
11+
12+
Use JavaScript's Array toString() method:
13+
14+
{% highlight coffeescript %}
15+
["one", "two", "three"].toString()
16+
# => 'three,two,one'
17+
{% endhighlight %}
18+
19+
## Discussion
20+
21+
`toString()` is a standard JavaScript method. Don't forget the parentheses.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
layout: recipe
3+
title: Removing duplicate elements from Arrays
4+
chapter: Arrays
5+
---
6+
## Problem
7+
8+
You want to remove duplicate elements from an array.
9+
10+
## Solution
11+
12+
{% highlight coffeescript %}
13+
Array::unique = ->
14+
output = {}
15+
output[@[key]] = @[key] for key in [0...@length]
16+
value for key, value of output
17+
18+
[1,1,2,2,2,3,4,5,6,6,6,"a","a","b","d","b","c"].unique()
19+
# => [ 1, 2, 3, 4, 5, 6, 'a', 'b', 'd', 'c' ]
20+
{% endhighlight %}
21+
22+
## Discussion
23+
24+
There are many implementations of the `unique` method in JavaScript. This one is based on "The fastest method to find unique items in array" found [here](http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout: recipe
3+
title: Replacing HTML tags with HTML named entities
4+
chapter: Regular Expressions
5+
---
6+
## Problem
7+
8+
You need to replace HTML tags with named entities:
9+
10+
`<br/> => &lt;br/&gt;`
11+
12+
## Solution
13+
14+
{% highlight coffeescript %}
15+
htmlEncode = (str) ->
16+
str.replace /[&<>"']/g, ($0) ->
17+
"&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";"
18+
19+
htmlEncode('<a href="http://bn.com">Barnes & Noble</a>')
20+
# => '&lt;a href=&quot;http://bn.com&quot;&gt;Barnes &amp; Noble&lt;/a&gt;'
21+
{% endhighlight %}
22+
23+
## Discussion
24+
25+
There are probably better ways to implement the above method.
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 %}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
layout: recipe
3+
title: Trimming whitespace from a String
4+
chapter: Strings
5+
---
6+
## Problem
7+
8+
You want to trim whitespace from a string.
9+
10+
## Solution
11+
12+
Use JavaScript's Regular Expression support to replace whitespace.
13+
14+
To trim leading and trailing whitespace, use the following:
15+
16+
{% highlight coffeescript %}
17+
" padded string ".replace /^\s+|\s+$/g, ""
18+
# => 'padded string'
19+
{% endhighlight %}
20+
21+
To trim only leading whitespace, use the following:
22+
23+
{% highlight coffeescript %}
24+
" padded string ".replace /^\s+/g, ""
25+
# => 'padded string '
26+
{% endhighlight %}
27+
28+
To trim only trailing whitespace, use the following:
29+
30+
{% highlight coffeescript %}
31+
" padded string ".replace /\s+$/g, ""
32+
# => ' padded string'
33+
{% endhighlight %}
34+
35+
## Discussion
36+
37+
Opera, Firefox and Chrome all have a native string prototype `trim` method, and the other browsers could add one as well. For this particular method, I would use the built-in method where possible:
38+
39+
{% highlight coffeescript %}
40+
trim = (val) ->
41+
if String::trim? then val.trim() else val.replace /^\s+|\s+$/g, ""
42+
43+
trim " padded string "
44+
# => 'padded string'
45+
{% endhighlight %}
46+
47+
48+
### Syntax Sugar
49+
50+
You can add some Ruby-like syntax sugar with the following shortcuts:
51+
52+
{% highlight coffeescript %}
53+
String::strip = -> if String::trim? then @trim() else @replace /^\s+|\s+$/g, ""
54+
String::lstrip = -> @replace /^\s+/g, ""
55+
String::rstrip = -> @replace /\s+$/g, ""
56+
57+
" padded string ".strip()
58+
# => 'padded string'
59+
" padded string ".lstrip()
60+
# => 'padded string '
61+
" padded string ".rstrip()
62+
# => ' padded string'
63+
{% endhighlight %}
64+
65+
For an interesting discussion and benchmarks of JavaScript `trim` performance, see [this blog post](http://blog.stevenlevithan.com/archives/faster-trim-javascript) by Steve Levithan.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: recipe
3+
title: Uppercasing a String
4+
chapter: Strings
5+
---
6+
## Problem
7+
8+
You want to uppercase a string.
9+
10+
## Solution
11+
12+
Use JavaScript's String toUpperCase() method:
13+
14+
{% highlight coffeescript %}
15+
"one two three".toUpperCase()
16+
# => 'ONE TWO THREE'
17+
{% endhighlight %}
18+
19+
## Discussion
20+
21+
`toUpperCase()` 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::upcase = -> @toUpperCase()
29+
"one two three".upcase()
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.upcase = function() {
42+
return this.toUpperCase();
43+
};
44+
"one two three".upcase();
45+
{% endhighlight %}

wanted-recipes.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ In the notes below, "JS" means the recipe is just a simple passthrough to an exi
1919
* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl!
2020
* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y]
2121
* substring # str.substring(x,y) === str.slice(x,y) === str[x..y-1] === str[x...y]
22-
* Uppercasing a string # JS toUpperCase()
23-
* Lowercasing a string # JS toLowerCase()
2422
* Replacing substrings
25-
* Trimming whitespace from the end of a string
2623

2724
## Arrays
2825

@@ -36,7 +33,6 @@ evens.every even
3633
* Filtering arrays # [1..10.filter (x) -> x % 2 == 0 # => [ 2, 4, 6, 8, 10 ]
3734
* Detecting presence of matching items in an array # [1..10].some (x) -> x % 2 == 0 # => true
3835
* Processing an array item by item # [10..1].forEach (x) -> console.log x # => nothing;, but a countdown is displayed on the console
39-
* Creating a string from an array
4036
* Replace all duplicates of an array
4137

4238
## Dates and Times
@@ -91,7 +87,6 @@ foo 1, 2, 3
9187
* Searching for substrings # "foo bar baz".match(/ba./) # => [ 'bar', index: 4, input: 'foo bar baz' ]
9288
* Searching for substrings # "foo bar baz".search(/ba./) # => 4
9389
* Replacing substrings # "foo bar baz".replace( /ba./, 'foo') # => "foo foo baz"
94-
* Replace HTML tags with named HTML entities # <br/> => &lt;br/&gt;
9590

9691
## Networking
9792

0 commit comments

Comments
 (0)