<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>*JJ</title><link href="http://blog.jdotjdot.com/" rel="alternate"></link><link href="http://blog.jdotjdot.com/feeds/tag/angularjs.atom.xml" rel="self"></link><id>http://blog.jdotjdot.com/</id><updated>2014-08-03T02:06:07-04:00</updated><entry><title>My first CoffeeScript: An angular filter</title><link href="http://blog.jdotjdot.com/2014/08/first-coffee-script-angular-filter/" rel="alternate"></link><updated>2014-08-03T02:06:07-04:00</updated><author><name>JJ</name></author><id>tag:blog.jdotjdot.com,2014-08-03:2014/08/first-coffee-script-angular-filter/</id><summary type="html">&lt;p&gt;I finally started writing my first CoffeeScript today.  I&amp;#8217;m working on a very complicated AngularJS app, so this is probably the worst time to be trying something new, but since Angular directives, filters, etc. are all of a very similar boilerplate format, this actually seemed like a good time to me to give it a shot.  Since I know what the output should look like, I was hoping that I&amp;#8217;d have an easier time writing my input in the&amp;nbsp;beginning.&lt;/p&gt;
&lt;p&gt;I started by writing a very simple filter that takes advantage of Javascript&amp;#8217;s &lt;code&gt;join&lt;/code&gt; method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;campusJobApp&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;joinBy&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="nf"&gt;(input, delimiter) -&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="nx"&gt;delimiter&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And sure enough, this actually came out as a very reasonable-looking Angular filter in&amp;nbsp;Javascript:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// Generated by CoffeeScript 1.7.1&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;campusJobApp&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;joinBy&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delimiter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delimiter&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//# sourceMappingURL=joinBy.map&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;With this success, I gave it a shot with something more&amp;nbsp;complicated:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="nx"&gt;angular&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;campusJobApp&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;idsToObjects&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;majors&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;(majors) -&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;(items, returnAttr) -&amp;gt;&lt;/span&gt;

            &lt;span class="nv"&gt;filtered = &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;

            &lt;span class="nv"&gt;getMajor = &lt;/span&gt;&lt;span class="nf"&gt;(major) -&amp;gt;&lt;/span&gt;
              &lt;span class="nx"&gt;majors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;majorId: &lt;/span&gt;&lt;span class="nx"&gt;major&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="nf"&gt;(res) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;filtered&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nf"&gt;(res) -&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;#TODO: handle error&lt;/span&gt;

            &lt;span class="nx"&gt;getMajor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;returnAttr&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
              &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;returnAttr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;
              &lt;span class="nx"&gt;filtered&lt;/span&gt;

    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I like the feel of CoffeeScript, though I am much more used to just regular Javascript, and I&amp;#8217;m concerned that I might get myself into trouble by misunderstanding or misusing CoffeeScript&amp;#8217;s syntax (particularly the optionality of parentheses).  This would be a much more egregious sin in Javascript, given the notorious type coercion and other silent &amp;#8216;features&amp;#8217; that I could accidentally introduce into my code if I don&amp;#8217;t know what I&amp;#8217;m&amp;nbsp;doing.&lt;/p&gt;
&lt;p&gt;The other thing that I&amp;#8217;m uncertain about is the mixing of CoffeeScript and Javascript.  Most of my project is written in Javascript.  The reason I&amp;#8217;m able to get away with practicing CoffeeScript is that I&amp;#8217;m using PyCharm, which watches and automatically compiles languages like CoffeeScript and &lt;span class="caps"&gt;SASS&lt;/span&gt; down to &lt;span class="caps"&gt;JS&lt;/span&gt; and &lt;span class="caps"&gt;CSS&lt;/span&gt; as I write them, so from the deployment perspective I don&amp;#8217;t have to add the compilers to the asset filter.  That being said, it will likely make it harder for other developers to edit my code if they (a) aren&amp;#8217;t familiar with the language or (b) will have to pre-compile their changed code on their own, since I haven&amp;#8217;t included the compilers as part of the asset&amp;nbsp;pipeline.&lt;/p&gt;</summary><category term="javascript"></category><category term="coffeescript"></category><category term="angularjs"></category><category term="angular-filters"></category></entry></feed>