From 03a46f38410a0120ef8c36887f4ca2f701e25984 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Mon, 20 Jun 2011 22:39:42 -0700 Subject: [PATCH 01/13] add recipe for uppercasing a string --- chapters/strings/uppercasing-a-string.md | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 chapters/strings/uppercasing-a-string.md diff --git a/chapters/strings/uppercasing-a-string.md b/chapters/strings/uppercasing-a-string.md new file mode 100644 index 0000000..9ee762b --- /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 %} \ No newline at end of file From 8d66fb5f66b077db52b9b5706de6697a4bd8a508 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Mon, 20 Jun 2011 22:39:51 -0700 Subject: [PATCH 02/13] add recipe for lowercasing a string --- chapters/strings/lowercasing-a-string.md | 45 ++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 chapters/strings/lowercasing-a-string.md diff --git a/chapters/strings/lowercasing-a-string.md b/chapters/strings/lowercasing-a-string.md new file mode 100644 index 0000000..0a7708b --- /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 %} \ No newline at end of file From 9f2c544697e7657105ab431c2473217702f66b27 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Mon, 20 Jun 2011 22:40:26 -0700 Subject: [PATCH 03/13] update wanted recipes --- wanted-recipes.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/wanted-recipes.md b/wanted-recipes.md index 7af4511..a7f0902 100644 --- a/wanted-recipes.md +++ b/wanted-recipes.md @@ -19,8 +19,6 @@ 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 From 81bb6f9141e83582cf2ba72dc6c909b4c23e00ad Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Mon, 20 Jun 2011 23:17:01 -0700 Subject: [PATCH 04/13] add recipe for trimming whitespace from a string --- .../trimming-whitespace-from-a-string.md | 67 +++++++++++++++++++ wanted-recipes.md | 1 - 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 chapters/strings/trimming-whitespace-from-a-string.md 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..483f106 --- /dev/null +++ b/chapters/strings/trimming-whitespace-from-a-string.md @@ -0,0 +1,67 @@ +--- +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][1] by Steve Levithan. + +[1] http://blog.stevenlevithan.com/archives/faster-trim-javascript \ No newline at end of file diff --git a/wanted-recipes.md b/wanted-recipes.md index a7f0902..14a3bf0 100644 --- a/wanted-recipes.md +++ b/wanted-recipes.md @@ -20,7 +20,6 @@ In the notes below, "JS" means the recipe is just a simple passthrough to an exi * 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] * Replacing substrings -* Trimming whitespace from the end of a string ## Arrays From 49838fa54679855f0c4492066a4363ddd3806c5d Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Mon, 20 Jun 2011 23:23:23 -0700 Subject: [PATCH 05/13] fix link to blog post --- chapters/strings/trimming-whitespace-from-a-string.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chapters/strings/trimming-whitespace-from-a-string.md b/chapters/strings/trimming-whitespace-from-a-string.md index 483f106..5c08bbb 100644 --- a/chapters/strings/trimming-whitespace-from-a-string.md +++ b/chapters/strings/trimming-whitespace-from-a-string.md @@ -62,6 +62,5 @@ String::rstrip = -> @replace /\s+$/g, "" # => ' padded string' {% endhighlight %} -For an interesting discussion and benchmarks of JavaScript `trim` performance, see [this blog post][1] by Steve Levithan. +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. -[1] http://blog.stevenlevithan.com/archives/faster-trim-javascript \ No newline at end of file From b746ee1cf994d7917e4a5af1e99b7f76f1a56019 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 00:18:25 -0700 Subject: [PATCH 06/13] add recipe Removing duplicate elements from Arrays --- ...removing-duplicate-elements-from-arrays.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 chapters/arrays/removing-duplicate-elements-from-arrays.md 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..8cfdbd6 --- /dev/null +++ b/chapters/arrays/removing-duplicate-elements-from-arrays.md @@ -0,0 +1,26 @@ +--- +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 = -> + o = {} + r = [] + o[this[i]] = this[i] for i in [1...@length] + r.push(v) for k,v of o + r + +[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/). From e94f5b09bc30d8ae15acfefb9165cffadd956867 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 00:27:08 -0700 Subject: [PATCH 07/13] add recipe Creating a String from an Array --- .../arrays/creating-a-string-from-an-array.md | 21 +++++++++++++++++++ wanted-recipes.md | 1 - 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 chapters/arrays/creating-a-string-from-an-array.md 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/wanted-recipes.md b/wanted-recipes.md index 14a3bf0..d082cba 100644 --- a/wanted-recipes.md +++ b/wanted-recipes.md @@ -33,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 From 03873e87d98538eb9de94a0131ddd47cee0b7451 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 00:45:44 -0700 Subject: [PATCH 08/13] add Replacing HTML tags with HTML named entities --- ...cing-html-tags-with-html-named-entities.md | 25 +++++++++++++++++++ wanted-recipes.md | 1 - 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md 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..8c3a46d --- /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. \ No newline at end of file diff --git a/wanted-recipes.md b/wanted-recipes.md index d082cba..df42004 100644 --- a/wanted-recipes.md +++ b/wanted-recipes.md @@ -87,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 From 1da06a883615ffa42e221ef7a58ded6079f2e4fc Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 00:47:29 -0700 Subject: [PATCH 09/13] add info to authors page --- authors.md | 1 + 1 file changed, 1 insertion(+) 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 From b1866679bdc09438d5e1905fbf4420c973475944 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 01:19:16 -0700 Subject: [PATCH 10/13] refactor Array::unique to be more idiomatic --- .../arrays/removing-duplicate-elements-from-arrays.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/chapters/arrays/removing-duplicate-elements-from-arrays.md b/chapters/arrays/removing-duplicate-elements-from-arrays.md index 8cfdbd6..3842433 100644 --- a/chapters/arrays/removing-duplicate-elements-from-arrays.md +++ b/chapters/arrays/removing-duplicate-elements-from-arrays.md @@ -11,11 +11,9 @@ You want to remove duplicate elements from an array. {% highlight coffeescript %} Array::unique = -> - o = {} - r = [] - o[this[i]] = this[i] for i in [1...@length] - r.push(v) for k,v of o - r + unique = {} + unique[@[key]] = @[key] for key in [1...@length] + value for key, value of unique [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' ] From 55316d065b64eb0e57b87d51c19c36526e6cc1e5 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 01:50:11 -0700 Subject: [PATCH 11/13] rename variables for clarity --- chapters/arrays/removing-duplicate-elements-from-arrays.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chapters/arrays/removing-duplicate-elements-from-arrays.md b/chapters/arrays/removing-duplicate-elements-from-arrays.md index 3842433..fcf8434 100644 --- a/chapters/arrays/removing-duplicate-elements-from-arrays.md +++ b/chapters/arrays/removing-duplicate-elements-from-arrays.md @@ -11,9 +11,9 @@ You want to remove duplicate elements from an array. {% highlight coffeescript %} Array::unique = -> - unique = {} - unique[@[key]] = @[key] for key in [1...@length] - value for key, value of unique + output = {} + output[@[key]] = @[key] for key in [1...@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' ] From 56231d325dac2174c37ec208f42079cf7a04b55b Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 01:56:38 -0700 Subject: [PATCH 12/13] fix bug with array index --- chapters/arrays/removing-duplicate-elements-from-arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/arrays/removing-duplicate-elements-from-arrays.md b/chapters/arrays/removing-duplicate-elements-from-arrays.md index fcf8434..7ecd731 100644 --- a/chapters/arrays/removing-duplicate-elements-from-arrays.md +++ b/chapters/arrays/removing-duplicate-elements-from-arrays.md @@ -12,7 +12,7 @@ You want to remove duplicate elements from an array. {% highlight coffeescript %} Array::unique = -> output = {} - output[@[key]] = @[key] for key in [1...@length] + 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() From d00857d1b28205691675089402adbf0d386ef4c6 Mon Sep 17 00:00:00 2001 From: Phil Cohen Date: Tue, 21 Jun 2011 03:05:12 -0700 Subject: [PATCH 13/13] make EOF newlines consistent --- .../replacing-html-tags-with-html-named-entities.md | 2 +- chapters/strings/lowercasing-a-string.md | 2 +- chapters/strings/trimming-whitespace-from-a-string.md | 1 - chapters/strings/uppercasing-a-string.md | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) 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 index 8c3a46d..cbd23a8 100644 --- a/chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md +++ b/chapters/regular_expressions/replacing-html-tags-with-html-named-entities.md @@ -22,4 +22,4 @@ htmlEncode('Barnes & Noble') ## Discussion -There are probably better ways to implement the above method. \ No newline at end of file +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 index 0a7708b..729dafa 100644 --- a/chapters/strings/lowercasing-a-string.md +++ b/chapters/strings/lowercasing-a-string.md @@ -42,4 +42,4 @@ String.prototype.downcase = function() { return this.toLowerCase(); }; "ONE TWO THREE".downcase(); -{% endhighlight %} \ No newline at end of file +{% endhighlight %} diff --git a/chapters/strings/trimming-whitespace-from-a-string.md b/chapters/strings/trimming-whitespace-from-a-string.md index 5c08bbb..b133e7f 100644 --- a/chapters/strings/trimming-whitespace-from-a-string.md +++ b/chapters/strings/trimming-whitespace-from-a-string.md @@ -63,4 +63,3 @@ String::rstrip = -> @replace /\s+$/g, "" {% 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 index 9ee762b..fd0e103 100644 --- a/chapters/strings/uppercasing-a-string.md +++ b/chapters/strings/uppercasing-a-string.md @@ -42,4 +42,4 @@ String.prototype.upcase = function() { return this.toUpperCase(); }; "one two three".upcase(); -{% endhighlight %} \ No newline at end of file +{% endhighlight %}