<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1278</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1278" rel="self"/>
  <title>Rene Saarsoo</title>
  <updated>Mon Jul 18 09:26:20 -0700 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor569694</id>
    <published>2011-07-18T09:26:20-07:00</published>
    <title>[Ruby] On append string in iterator</title>
    <content type="html">&lt;p&gt;If you have variable data, then you can basically do what Marc already suggested, but supply a block that creates the data.&lt;/p&gt;

&lt;pre&gt;def append
  Array.new(5) { generate_random_string }.join
end&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1734-append-string-in-iterator/refactors/569694" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144340</id>
    <published>2009-01-24T09:22:42-08:00</published>
    <title>[JavaScript] On jQuery One-Liner</title>
    <content type="html">&lt;p&gt;Maybe something like this...&lt;/p&gt;

&lt;pre&gt;/* jquery.fieldjump.js */
 
jQuery.fn.jumpsTo = (function(){
  function isTextKey(n) {
    // far from correct, but handles the most common cases
    return n &amp;gt; 47;
  }
  
  return function(to) {
    return this.each(function() {
      $(this).bind('keyup', function(event) {
        var keys_typed = $(this).val().length;
        var keys_required = $(this).attr('maxlength');
        if (isTextKey(event.keyCode) &amp;amp;&amp;amp; keys_typed == keys_required) {
          $(to).focus();
        }
      });
    });
  };
})();


// somewhere inside your code...

$('div.date input').jumpsTo('div.time input');
  
&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/713-jquery-one-liner/refactors/144340" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144296</id>
    <published>2009-01-23T22:28:28-08:00</published>
    <title>[JavaScript] On jQuery One-Liner</title>
    <content type="html">&lt;p&gt;What ever the rest of the code of tmpvar does, I really can't stand the isNumber() function.  It should be named isNumberKey() or something like this.  Although I can't understand how are keycodes 96..105 related to numbers.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/713-jquery-one-liner/refactors/144296" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144295</id>
    <published>2009-01-23T22:17:22-08:00</published>
    <title>[JavaScript] On Split text into unordered list.</title>
    <content type="html">

&lt;pre&gt;$(document).ready(function(){
  var resultsMax = 15;

  $.get('test-data.txt', function(textData){
    var lines = textData.split('\n').slice(0, resultsMax);
    
    $.each(lines, function(i, line){
      var term = line.split('|');
      
      // I would go for some extra variables.
      // Array indexes are quite meaningless.
      var title = term[0];
      var url = term[3];

      // This way it should be easier to understand,
      // and more effective too, 
      // and avoids using the magic number 15.
      $('#tagcloud').append(
        '&amp;lt;li value=&amp;quot;'+(resultsMax-i)+'&amp;quot;&amp;gt;' +
        '&amp;lt;a href=&amp;quot;'+url+'&amp;quot; title=&amp;quot;'+title+'&amp;quot;&amp;gt;'+title+'&amp;lt;/a&amp;gt;' +
        '&amp;lt;/li&amp;gt;');
    });
    
    // No need for any onComplete function,
    // just start it after the $.each() loop ends
    $('#tagcloud').tagcloud();
  });
});&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/712-split-text-into-unordered-list/refactors/144295" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144282</id>
    <published>2009-01-23T19:53:53-08:00</published>
    <title>[JavaScript] On Split text into unordered list.</title>
    <content type="html">

&lt;pre&gt;## JavaScript [jquery_javascript]
$(document).ready(function(){
  var MAX_NR_OF_LINES = 15;
  
  // get the text file to be parsed
  $.get('test-data.txt', function(textData){
    // get first n lines (no more than the maximum allowed)
    var lines = textData.split('\n').slice(0, MAX_NR_OF_LINES-1);
    $.each(lines, function(i, line){
      // split the line on pipe ('|') and take the first term
      var term = line.split('|')[0];

      // add new list item containing this term
      $('#tagcloud').append('&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;');
      $('#tagcloud li:last-child').text(term);

      // If the text in this file is safe, then you could instead use:
      // $('#tagcloud').append('&amp;lt;li&amp;gt;'+term+'&amp;lt;/li&amp;gt;');

      // Or when you have an html-escaping function
      // There doesn't seem to be one offered by jQuery
      // $('#tagcloud').append('&amp;lt;li&amp;gt;'+escape_html(term)+'&amp;lt;/li&amp;gt;');
    });
  });
});
&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/712-split-text-into-unordered-list/refactors/144282" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144237</id>
    <published>2009-01-23T08:52:42-08:00</published>
    <title>[JavaScript] On Format Currency</title>
    <content type="html">&lt;p&gt;Alright, lets try this.&lt;/p&gt;

&lt;p&gt;First of all I created utility function round(), because for some reason JavaScript lacks a rounding function that allows you to specify the number of digits after decimals separator.  And toFixed() does rounding in an uncommon way.&lt;/p&gt;

&lt;p&gt;The strange input handling I left out.&lt;/p&gt;

&lt;p&gt;Another difference is, that formatCurrency(true) == &amp;quot;$1.0000&amp;quot; not &amp;quot;$0.0000&amp;quot;.&lt;/p&gt;

&lt;pre&gt;## [javascript]
function formatCurrency(num) {
  num = isNaN(num) ? 0 : num;
  var rounded = round(num, 4).toFixed(4);
  
  // split to three parts
  var parts = rounded.match(/^(-?)([0-9]+)(.[0-9]+)$/);
  var sign = parts[1];
  var integer = parts[2];
  var fraction = parts[3];
  
  // split integer part into thousands
  var thousands = [];
  while (integer.length &amp;gt; 3) {
    thousands.unshift( integer.substr(integer.length - 3, 3) );
    integer = integer.substr(0, integer.length - 3);
  }
  thousands.unshift(integer);
  
  // separate thousands with comma and put it all together
  return sign + &amp;quot;$&amp;quot; + thousands.join(&amp;quot;,&amp;quot;) + fraction;
}

// Better rounding than with Math.round()
// Allows to specify the number of digits after decimal point
// round(5.1256, 2) --&amp;gt; 5.13
function round(nr, digits) {
  digits = digits || 0;
  var multiplier = Math.pow(10, digits);
  return Math.round(nr * multiplier) / multiplier;
};
&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/709-format-currency/refactors/144237" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144070</id>
    <published>2009-01-20T22:36:06-08:00</published>
    <title>[JavaScript] On Format Currency</title>
    <content type="html">&lt;p&gt;You sayd: &amp;quot;One of our requirements is that prices be displayed with four decimals.&amp;quot;&lt;/p&gt;

&lt;p&gt;What do you mean by that? Is four decimals like 1234,5678.00 or is it like 0.1234? Or even something else?&lt;/p&gt;

&lt;p&gt;Anyway... first of all I just wrote a set of unit tests to understand, whet this function actually does.  Most of it was as expected, but the first line in this function does some pretty weird stuff with input (look at the test &amp;quot;strange input&amp;quot;). Are you sure that it really needs to do that?&lt;/p&gt;

&lt;pre&gt;## JavaScript [javascript]
test(&amp;quot;fractions&amp;quot;, function() {
  equals( formatCurrency(0), &amp;quot;$0.00&amp;quot; );
  equals( formatCurrency(5.5), &amp;quot;$5.50&amp;quot; );
  equals( formatCurrency(.25), &amp;quot;$0.25&amp;quot; );
});

test(&amp;quot;rounding&amp;quot;, function() {
  equals( formatCurrency(5.204), &amp;quot;$5.20&amp;quot; );
  equals( formatCurrency(5.205), &amp;quot;$5.21&amp;quot; );
  equals( formatCurrency(5.206), &amp;quot;$5.21&amp;quot; );
});

test(&amp;quot;thousands separator&amp;quot;, function() {
  equals( formatCurrency(100), &amp;quot;$100.00&amp;quot; );
  equals( formatCurrency(1000), &amp;quot;$1,000.00&amp;quot; );
  equals( formatCurrency(10000), &amp;quot;$10,000.00&amp;quot; );
  equals( formatCurrency(100000), &amp;quot;$100,000.00&amp;quot; );
  equals( formatCurrency(1000000), &amp;quot;$1,000,000.00&amp;quot; );
});

test(&amp;quot;negative&amp;quot;, function() {
  equals( formatCurrency(-5), &amp;quot;-$5.00&amp;quot; );
});

test(&amp;quot;wrong type of input&amp;quot;, function() {
  equals( formatCurrency(&amp;quot;&amp;quot;), &amp;quot;$0.00&amp;quot; );
  equals( formatCurrency({}), &amp;quot;$0.00&amp;quot; );
  equals( formatCurrency(true), &amp;quot;$0.00&amp;quot; );
  equals( formatCurrency(false), &amp;quot;$0.00&amp;quot; );
});

test(&amp;quot;strange input&amp;quot;, function() {
  equals( formatCurrency(&amp;quot;2\\&amp;quot;), &amp;quot;$2.00&amp;quot; );
  equals( formatCurrency(&amp;quot;2\\,5&amp;quot;), &amp;quot;$25.00&amp;quot; );
  equals( formatCurrency(&amp;quot;2\\,5\\,\\,6&amp;quot;), &amp;quot;$256.00&amp;quot; );
  equals( formatCurrency(&amp;quot;\\,\\,\\,\\&amp;quot;), &amp;quot;$0.00&amp;quot; );
});
&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/709-format-currency/refactors/144070" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor143958</id>
    <published>2009-01-19T18:45:05-08:00</published>
    <title>[PHP] On innerHTML of a DOMElement</title>
    <content type="html">&lt;p&gt;This code is pretty short and simple.  But to understand it you need to know what importNode() does.  It's not that obvious when you aren't that familiar with DOM API.  At least for me it was quite hard to *get* it.&lt;/p&gt;

&lt;p&gt;There doesn't seem to be any better way to do it - as far as I digged in PHP manual.  It should also be efficient enough when you aren't working with really big documents - but then you probably shouldn't use DOM anyway.&lt;/p&gt;

&lt;p&gt;What this code really needs is a comment. A comment that explains *why* it does things the way it does.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Rene Saarsoo</name>
      <email>info@triin.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/708-innerhtml-of-a-domelement/refactors/143958" rel="alternate"/>
  </entry>
</feed>

