diff --git a/authors.md b/authors.md index 6d4a792..54043fd 100644 --- a/authors.md +++ b/authors.md @@ -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 diff --git a/chapters/arrays/creating-a-string-from-an-array.md b/chapters/arrays/creating-a-string-from-an-array.md new file mode 100644 index 0000000..d2b83bb --- /dev/null +++ b/chapters/arrays/creating-a-string-from-an-array.md @@ -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. diff --git a/chapters/arrays/removing-duplicate-elements-from-arrays.md b/chapters/arrays/removing-duplicate-elements-from-arrays.md new file mode 100644 index 0000000..7ecd731 --- /dev/null +++ b/chapters/arrays/removing-duplicate-elements-from-arrays.md @@ -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/). diff --git a/chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md b/chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md new file mode 100644 index 0000000..cbd23a8 --- /dev/null +++ b/chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md @@ -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/>` + +## Solution + +{% highlight coffeescript %} +htmlEncode = (str) -> + str.replace /[&<>"']/g, ($0) -> + "&" + {"&":"amp", "<":"lt", ">":"gt", '"':"quot", "'":"#39"}[$0] + ";" + +htmlEncode('Barnes & Noble') +# => '<a href="http://bn.com">Barnes & Noble</a>' +{% endhighlight %} + +## Discussion + +There are probably better ways to implement the above method. diff --git a/chapters/strings/lowercasing-a-string.md b/chapters/strings/lowercasing-a-string.md new file mode 100644 index 0000000..729dafa --- /dev/null +++ b/chapters/strings/lowercasing-a-string.md @@ -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 %} diff --git a/chapters/strings/trimming-whitespace-from-a-string.md b/chapters/strings/trimming-whitespace-from-a-string.md new file mode 100644 index 0000000..b133e7f --- /dev/null +++ b/chapters/strings/trimming-whitespace-from-a-string.md @@ -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. diff --git a/chapters/strings/uppercasing-a-string.md b/chapters/strings/uppercasing-a-string.md new file mode 100644 index 0000000..fd0e103 --- /dev/null +++ b/chapters/strings/uppercasing-a-string.md @@ -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 %} diff --git a/wanted-recipes.md b/wanted-recipes.md index 7af4511..df42004 100644 --- a/wanted-recipes.md +++ b/wanted-recipes.md @@ -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 @@ -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 @@ -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/> ## Networking