Skip to content
Merged
1 change: 1 addition & 0 deletions authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The following people are totally rad and awesome because they have contributed r
* Aaron Weinberger _aw9994@cs.ship.edu_
* James C. Holder _cs_cookbook@thirdtruck.org_
* Jason Giedymin _jasong@apache.org_
* Phil Cohen _github@phlippers.net_
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!

# Developers
Expand Down
21 changes: 21 additions & 0 deletions chapters/arrays/creating-a-string-from-an-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
layout: recipe
title: Creating a String from an Array
chapter: Arrays
---
## Problem

You want to create a string from an array.

## Solution

Use JavaScript's Array toString() method:

{% highlight coffeescript %}
["one", "two", "three"].toString()
# => 'three,two,one'
{% endhighlight %}

## Discussion

`toString()` is a standard JavaScript method. Don't forget the parentheses.
24 changes: 24 additions & 0 deletions chapters/arrays/removing-duplicate-elements-from-arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
layout: recipe
title: Removing duplicate elements from Arrays
chapter: Arrays
---
## Problem

You want to remove duplicate elements from an array.

## Solution

{% highlight coffeescript %}
Array::unique = ->
output = {}
output[@[key]] = @[key] for key in [0...@length]
value for key, value of output

[1,1,2,2,2,3,4,5,6,6,6,"a","a","b","d","b","c"].unique()
# => [ 1, 2, 3, 4, 5, 6, 'a', 'b', 'd', 'c' ]
{% endhighlight %}

## Discussion

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/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
layout: recipe
title: Replacing HTML tags with HTML named entities
chapter: Regular Expressions
---
## Problem

You need to replace HTML tags with named entities:

`<br/> => &lt;br/&gt;`

## Solution

{% highlight coffeescript %}
htmlEncode = (str) ->
str.replace /[&<>"']/g, ($0) ->
"&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";"

htmlEncode('<a href="http://bn.com">Barnes & Noble</a>')
# => '&lt;a href=&quot;http://bn.com&quot;&gt;Barnes &amp; Noble&lt;/a&gt;'
{% endhighlight %}

## Discussion

There are probably better ways to implement the above method.
45 changes: 45 additions & 0 deletions chapters/strings/lowercasing-a-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: recipe
title: Lowercasing a String
chapter: Strings
---
## Problem

You want to lowercase a string.

## Solution

Use JavaScript's String toLowerCase() method:

{% highlight coffeescript %}
"ONE TWO THREE".toLowerCase()
# => 'one two three'
{% endhighlight %}

## Discussion

`toLowerCase()` is a standard JavaScript method. Don't forget the parentheses.

### Syntax Sugar

You can add some Ruby-like syntax sugar with the following shortcut:

{% highlight coffeescript %}
String::downcase = -> @toLowerCase()
"ONE TWO THREE".downcase()
# => 'one two three'
{% endhighlight %}

The snippet above demonstrates a few features of CoffeeScript:

* The double-colon `::` is shorthand for saying `.prototype.`
* The "at" sign `@` is shorthand for saying `this.`

The code above compiles in to the following JavaScript:

{% highlight javascript %}
String.prototype.downcase = function() {
return this.toLowerCase();
};
"ONE TWO THREE".downcase();
{% endhighlight %}
65 changes: 65 additions & 0 deletions chapters/strings/trimming-whitespace-from-a-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
layout: recipe
title: Trimming whitespace from a String
chapter: Strings
---
## Problem

You want to trim whitespace from a string.

## Solution

Use JavaScript's Regular Expression support to replace whitespace.

To trim leading and trailing whitespace, use the following:

{% highlight coffeescript %}
" padded string ".replace /^\s+|\s+$/g, ""
# => 'padded string'
{% endhighlight %}

To trim only leading whitespace, use the following:

{% highlight coffeescript %}
" padded string ".replace /^\s+/g, ""
# => 'padded string '
{% endhighlight %}

To trim only trailing whitespace, use the following:

{% highlight coffeescript %}
" padded string ".replace /\s+$/g, ""
# => ' padded string'
{% endhighlight %}

## Discussion

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:

{% highlight coffeescript %}
trim = (val) ->
if String::trim? then val.trim() else val.replace /^\s+|\s+$/g, ""

trim " padded string "
# => 'padded string'
{% endhighlight %}


### Syntax Sugar

You can add some Ruby-like syntax sugar with the following shortcuts:

{% highlight coffeescript %}
String::strip = -> if String::trim? then @trim() else @replace /^\s+|\s+$/g, ""
String::lstrip = -> @replace /^\s+/g, ""
String::rstrip = -> @replace /\s+$/g, ""

" padded string ".strip()
# => 'padded string'
" padded string ".lstrip()
# => 'padded string '
" padded string ".rstrip()
# => ' padded string'
{% endhighlight %}

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.
45 changes: 45 additions & 0 deletions chapters/strings/uppercasing-a-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: recipe
title: Uppercasing a String
chapter: Strings
---
## Problem

You want to uppercase a string.

## Solution

Use JavaScript's String toUpperCase() method:

{% highlight coffeescript %}
"one two three".toUpperCase()
# => 'ONE TWO THREE'
{% endhighlight %}

## Discussion

`toUpperCase()` is a standard JavaScript method. Don't forget the parentheses.

### Syntax Sugar

You can add some Ruby-like syntax sugar with the following shortcut:

{% highlight coffeescript %}
String::upcase = -> @toUpperCase()
"one two three".upcase()
# => 'ONE TWO THREE'
{% endhighlight %}

The snippet above demonstrates a few features of CoffeeScript:

* The double-colon `::` is shorthand for saying `.prototype.`
* The "at" sign `@` is shorthand for saying `this.`

The code above compiles in to the following JavaScript:

{% highlight javascript %}
String.prototype.upcase = function() {
return this.toUpperCase();
};
"one two three".upcase();
{% endhighlight %}
5 changes: 0 additions & 5 deletions wanted-recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ In the notes below, "JS" means the recipe is just a simple passthrough to an exi
* HTML methods # JS .sup(), .sub(), .blink(), .link(url), etc. May not exist in your JS impl!
* substr # str.substr(x,y) === str[x..x+y-1] === str[x...x+y]
* substring # str.substring(x,y) === str.slice(x,y) === str[x..y-1] === str[x...y]
* Uppercasing a string # JS toUpperCase()
* Lowercasing a string # JS toLowerCase()
* Replacing substrings
* Trimming whitespace from the end of a string

## Arrays

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

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

## Networking

Expand Down