<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>uxebu » blog &#187; javascript</title>
	<atom:link href="http://uxebu.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://uxebu.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 23 Apr 2012 19:10:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>To mock or not to mock</title>
		<link>http://uxebu.com/blog/2012/04/04/to-mock-or-not-to-mock/</link>
		<comments>http://uxebu.com/blog/2012/04/04/to-mock-or-not-to-mock/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 17:45:57 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[jasmine]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=2322</guid>
		<description><![CDATA[I am just hacking along, all TDD style of course, and the one problem I always come across again is to realize when I should mock and when not. My current idea is to have two blocks of tests, one with mocked data, the other non-mocking. Let me lead you to why I came up [...]]]></description>
			<content:encoded><![CDATA[<p>I am just hacking along, all TDD style of course, and the one problem I always come across again is to realize when I should mock and when not. My current idea is to have two blocks of tests, one with mocked data, the other non-mocking. Let me lead you to why I came up with that. Input and discussion welcome, I am curious about ideas and possible improvements.<br />
<span id="more-2322"></span></p>
<h2>The task</h2>
<p>I have a set of methods and want to find out if they are implemented. See the code below, sorted in logical order.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #006600; font-style: italic;">// The big summary method, that spits out the final result.</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> forMethods<span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; numTotal<span style="color: #339933;">:</span> methods.<span style="color: #660066;">length</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; numImplemented<span style="color: #339933;">:</span> getNumberOfImplementedMethods<span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> getNumberOfImplementedMethods<span style="color: #009900;">&#40;</span>methods<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// some simple map+reduce using `isMethodImplemented()`</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> isMethodImplemented<span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> details <span style="color: #339933;">=</span> getMethodDetails<span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> details.<span style="color: #660066;">exists</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>details.<span style="color: #660066;">isDummy</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">function</span> getMethodDetails<span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; exists<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">typeof</span> method <span style="color: #339933;">==</span> <span style="color: #3366CC;">'function'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; isDummy<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">+</span>method <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>And the tests for <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code> as the user expects it to work, without mocking (or spying).</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'forMethods (no mocking)'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return {numTotal:1, numImplemented:1}'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> expected <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>numTotal<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> numImplemented<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toEqual</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return {numTotal:0, numImplemented:0}'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> expected <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>numTotal<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> numImplemented<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toEqual</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This works fine as long as I don&#8217;t change any of the underlying functions or a requirement that is implemented in some function other than the <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code> function that I have tests for. For example, I define a dummyFunction myself <code class="codecolorer text mac-classic"><span class="text">var dummyFunction = function(){ console.warn('not implemented') }</span></code> to allow also <code class="codecolorer text mac-classic"><span class="text">function(){}</span></code> to be a valid method, which was not the case yet. I change the first test to</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return {numTotal:1, numImplemented:1}'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Before there was `function(){ return 1 }` in the line below.</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> expected <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>numTotal<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> numImplemented<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toEqual</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>and add a new test</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return {numTotal:1, numImplemented:0} for a dummy function'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>summary.<span style="color: #660066;">dummyFunction</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> expected <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>numTotal<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> numImplemented<span style="color: #339933;">:</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toEqual</span><span style="color: #009900;">&#40;</span>expected<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Due to the new requirement I get two failing tests.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">forMethods (no mocking)<br />
&nbsp; it should return {numTotal:1, numImplemented:1}<br />
&nbsp; Error: Failed at test/summary-spec.js:40:20<br />
&nbsp; Expected<br />
&nbsp; &nbsp; &nbsp; { numTotal : 1, numImplemented : 0 }<br />
&nbsp; to equal<br />
&nbsp; &nbsp; &nbsp; { numTotal : 1, numImplemented : 1 }.<br />
&nbsp; &nbsp; at [object Object].&lt;anonymous&gt; (test/summary-spec.js:40:20)<br />
<br />
forMethods (no mocking)<br />
&nbsp; it should return {numTotal:1, numImplemented:0} for a dummy function<br />
&nbsp; Error: Failed at test/summary-spec.js:52:20<br />
&nbsp; Expected<br />
&nbsp; &nbsp; &nbsp; { numTotal : 1, numImplemented : 1 }<br />
&nbsp; to equal<br />
&nbsp; &nbsp; &nbsp; { numTotal : 1, numImplemented : 0 }.<br />
&nbsp; &nbsp; at [object Object].&lt;anonymous&gt; (test/summary-spec.js:52:20)</div></td></tr></tbody></table></div>
<p>But I don&#8217;t see exactly where my code fails due to this new requirement. The tests tell me <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code> fails, but there is a chain of four functions below. So let&#8217;s write a test for <code class="codecolorer text mac-classic"><span class="text">getNumberOfImplementedMethods()</span></code> used by <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code>.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'getNumberOfImplementedMethods'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return 1'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">getNumberOfImplementedMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return 0'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">getNumberOfImplementedMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return 0 for dummyFunctions only'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">getNumberOfImplementedMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>summary.<span style="color: #660066;">dummyFunction</span><span style="color: #339933;">,</span> summary.<span style="color: #660066;">dummyFunction</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The last one of those tests fails too</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getNumberOfImplementedMethods<br />
&nbsp; it should return 0 for dummyFunctions only<br />
&nbsp; Error: Failed at test/summary-spec.js:81:20<br />
&nbsp; Expected<br />
&nbsp; &nbsp; &nbsp; 2<br />
&nbsp; to be<br />
&nbsp; &nbsp; &nbsp; 0.<br />
&nbsp; &nbsp; at [object Object].&lt;anonymous&gt; (test/summary-spec.js:81:20)</div></td></tr></tbody></table></div>
<p>Now I actually get more and more failing tests. Ouch, that hurts and I loose the overview quickly. In my mind I am kinda &#8220;yeah I know this one fails, it will fall into place once I am getting there&#8221;. Doesn&#8217;t feel good and if I get interrupted at this point I might come back and be very unhappy about so many failing tests. Anyway, let&#8217;s go one level deeper, and write tests for <code class="codecolorer text mac-classic"><span class="text">isMethodImplemented()</span></code>, more failing tests. Writing tests for the last function and fixing the code accordingly will solve our dilemma here:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'getMethodDetails'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return `isDummy=false`'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">getMethodDetails</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual.<span style="color: #660066;">isDummy</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return `isDummy=true`'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> actual <span style="color: #339933;">=</span> summary.<span style="color: #660066;">getMethodDetails</span><span style="color: #009900;">&#40;</span>summary.<span style="color: #660066;">dummyFunction</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>actual.<span style="color: #660066;">isDummy</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Both of the tests fail.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getMethodDetails<br />
&nbsp; it should return `isDummy=false`<br />
&nbsp; Error: Failed at test/summary-spec.js:61:28<br />
&nbsp; Expected<br />
&nbsp; &nbsp; &nbsp; true<br />
&nbsp; to be<br />
&nbsp; &nbsp; &nbsp; false.<br />
&nbsp; &nbsp; at [object Object].&lt;anonymous&gt; (test/summary-spec.js:61:28)<br />
<br />
getMethodDetails (no mocking)<br />
&nbsp; it should return `isDummy=true`<br />
&nbsp; Error: Failed at test/summary-spec.js:66:28<br />
&nbsp; Expected<br />
&nbsp; &nbsp; &nbsp; false<br />
&nbsp; to be<br />
&nbsp; &nbsp; &nbsp; true.<br />
&nbsp; &nbsp; at [object Object].&lt;anonymous&gt; (test/summary-spec.js:66:28)<br />
<br />
Finished in 0.014 seconds</div></td></tr></tbody></table></div>
<p>Of course they fail, since I didn&#8217;t fix the actual code that compares the dummyFunction, so I fix the implementation of <code class="codecolorer text mac-classic"><span class="text">getMethodDetails()</span></code> to compare the method to <code class="codecolorer text mac-classic"><span class="text">this.dummyFunction</span></code> and not <code class="codecolorer text mac-classic"><span class="text">function(){}</span></code> anymore</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">getMethodDetails<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; exists<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">typeof</span> method <span style="color: #339933;">==</span> <span style="color: #3366CC;">'function'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; isDummy<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">+</span>method <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dummyFunction</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></td></tr></tbody></table></div>
<p>If I run the tests now all of them are passing. Fine.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Started<br />
..............<br />
<br />
Finished in 0.005 seconds<br />
14 tests, 14 assertions, 0 failures, 14 passes</div></td></tr></tbody></table></div>
<p>That was a little painful. I had to go all the way down the rabbit hole to find the actual problem. If I imagine this in a bigger architecture I know why I rather start writing tests early, also with the danger of them getting thrown away, because the code becomes obsolete, gets rewritten etc. That&#8217;s fine.</p>
<p>Let&#8217;s try another approach. I write some mock tests and see if that gets me any further quicker.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'forMethods (all mocked)'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; beforeEach<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; spyOn<span style="color: #009900;">&#40;</span>summary<span style="color: #339933;">,</span> <span style="color: #3366CC;">'getNumberOfImplementedMethods'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should call `summary.getNumberOfImplementedMethods` properly'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> funcs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span>funcs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>summary.<span style="color: #660066;">getNumberOfImplementedMethods</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toHaveBeenCalledWith</span><span style="color: #009900;">&#40;</span>funcs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should have return value of `summary.getNumberOfImplementedMethods` in `numImplemnted`'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; summary.<span style="color: #660066;">getNumberOfImplementedMethods</span>.<span style="color: #660066;">andReturn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> methodsSummary <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>methodsSummary.<span style="color: #660066;">numImplemented</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should set `numTotal` to 0 for no methods'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> methodsSummary <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>methodsSummary.<span style="color: #660066;">numTotal</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should set `numTotal` to 3 for 3 methods'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// The values don't matter, since we mock all underlying functions</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// that's why we can also pass in integers, we just test the amount here.</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> methodsSummary <span style="color: #339933;">=</span> summary.<span style="color: #660066;">forMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; expect<span style="color: #009900;">&#40;</span>methodsSummary.<span style="color: #660066;">numTotal</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Notice, that those tests do also test <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code> but none of them fails. So it seems I have no problem on that level. Now I can write simple tests for each function. And I will get down to the tests I wrote above for <code class="codecolorer text mac-classic"><span class="text">getMethodDetails()</span></code> and at this point I will discover the actual failure. So mocking leaves me with a way better feeling since I don&#8217;t just add another failing test in the hope of getting to the actual failure at some point.<br />
Let me discuss in the following why I think it makes sense to mix both approaches why I think that there is not only one way to do it. The mix has to be right.</p>
<h2>Not Mocking</h2>
<p>How I started by not mocking anything, was a logical start. I have the user&#8217;s view, and in our case the API user just wants to get the result if a set of methods is implemented. Over time four methods came along, that <code class="codecolorer text mac-classic"><span class="text">forMethods()</span></code> used. At some point when the hierarchy becomes deeper, esp. when functions get reused in multiple parts you start writing tests for the functions on the level below. Those tests are definitely a good thing, since you gain confidence in the underlying parts.<br />
The problem I faced a couple of times was if I change a requirement that is handled by the fourth function down the hierarchy I get failing tests for all four functions. That means you need to know where to look first, either you know your code well or you have sorted the tests to run in a certain order, so that the failing functions lowest in the hierarchy show up first and you can work top down. But even knowing how to put them in the right order requires knowledge of the entire architecture and dependencies. And that will fail when your team grows because not everyone knows how to sort the tests right. And maintaining such an order will become tedious task and is prone to errors.<br />
But non-mocking tests are an important part of testing, they allow me to stay on the same level where the API is used and I don&#8217;t loose the real use cases out of sight, but still they might be misleading.</p>
<h2>Mocking</h2>
<p>I experienced that starting out with mock tests is not the way to go either. I would nail down too many dependencies too soon. Therefore I stay with unmocked tests for the most part in the beginning, but once the architecture shapes I start mocking away stuff and therefore decoupling the different levels. In the above case I can easily find errors on each of the four levels by having mock tests. Yes, a mock test is describing the implementation closer, but if the requirements change the implementation might change too and then it is ok to change the according tests too. The great thing is that I can focus on one level of implementation and I am confident that the levels below are working so by mocking I am just assuring that each of the working pieces is just wired up correctly.<br />
The mock tests do also have the advantage of running much faster since underlying functionality is not invoked that often. Asynchronous behavior can also be avoided nicely this way.<br />
So it&#8217;s still a learning path for me and finding the right balance turns out to be the important thing.</p>
<h2>Conclusion</h2>
<p>I decided to have three sets of tests, which are always grouped into their own <code class="codecolorer text mac-classic"><span class="text">describe()</span></code> each:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'xxx (all mocked)'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// all tests that mock their dependencies</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'xxx (no mocking)'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// all tests that may use the entire stack of underlying functions</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'xxx'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// mostly the low level functions that can not be mocked anyways</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Now I would need a way to run all the &#8220;mocked&#8221; tests only to see just those failures. If I don&#8217;t touch jasmine I guess I will have to continue identifying the failing mock tests by searching for the suffix.<br />
The unmocked tests will fall into place later too, if that is not the case I have an edge case that I didn&#8217;t write a mock test for yet, so I will need to do that.<br />
One question that still keeps going around in my head is: are those unmocked tests actually integration tests, or as bad as them?</p>
<p>I am looking forward to input. How do you do it? What can I improve? If someone learned something here I am happy. And please share your experiences too, I want to learn too.<br />
Find the <a href="https://github.com/uxebu/mock-or-not-article" title="Source code" target="_blank">source code on github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2012/04/04/to-mock-or-not-to-mock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Snippets: isArray, arguments to Array, [x..y], padding, array.unique()</title>
		<link>http://uxebu.com/blog/2012/01/19/javascript-snippets-isarray-arguments-to-array-x-y-padding-array-unique/</link>
		<comments>http://uxebu.com/blog/2012/01/19/javascript-snippets-isarray-arguments-to-array-x-y-padding-array-unique/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 08:41:53 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1942</guid>
		<description><![CDATA[Now let me post a couple of code snippets that are lying around here already for a while. For the uber geeks out there you might yawn seeing them, but there are a lot of people who are searching for this stuff over and over, including myself. So let me post some small useful snippets [...]]]></description>
			<content:encoded><![CDATA[<p>Now let me post a couple of code snippets that are lying around here already for a while. For the uber geeks out there you might yawn seeing them, but there are a lot of people who are searching for this stuff over and over, including myself. So let me post some small useful snippets that have went through my fingers lately.<br />
Reading on, you will see snippets that help you with 1) detecting if a variable is a real array 2) how to convert it into a proper array 3) how to mimic a x = [3..25] kinda thing that other languages offer 4) how to easily pad a string with leading chararacter (i.e. zeros) and finally 5) how to create a unique function for arrays.<br />
<span id="more-1942"></span></p>
<h2>isArray()</h2>
<p>There are a lot of ways to check for a variable being an array, but the safest one and also <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray" title="isArray()">most promoted and secure</a> one is</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Some examples from the console demostrate well how to use it and that it really works :)</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> arr <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span> <span style="color: #006600; font-style: italic;">// That's a simple integer, of course.</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">false</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> arr <span style="color: #339933;">=</span> <span style="color: #3366CC;">'1'</span> <span style="color: #006600; font-style: italic;">// Just a string.</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">false</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> arr <span style="color: #339933;">=</span> <span style="color: #3366CC;">'[1,2,3]'</span> <span style="color: #006600; font-style: italic;">// Also a string.</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">false</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// That's an object, no array either!</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">false</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #006600; font-style: italic;">// An array, so it should be true.</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'[object Array]'</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">true</span></div></td></tr></tbody></table></div>
<h2>Convert arguments to an array</h2>
<p>One of the pains JavaScript developers have to fight with which will get solved with future versions (fortunately) is that the arguments object looks like an array, e.g. it has the property length but you can&#8217;t work with it as if it was an array, e.g. here:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>arguments.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> f<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #CC0000;">3</span> <span style="color: #006600; font-style: italic;">// arguments.length works</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> arguments.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> f<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><br />
TypeError<span style="color: #339933;">:</span> arguments.<span style="color: #660066;">slice</span> <span style="color: #000066; font-weight: bold;">is</span> not a <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #006600; font-style: italic;">// slice() is not available :(</span></div></td></tr></tbody></table></div>
<p>But their is a workaround until the language handles it. It is</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #003366; font-weight: bold;">var</span> realArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>realArray.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> f<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #006600; font-style: italic;">// slice throws no error, hurray!</span></div></td></tr></tbody></table></div>
<h2>Range function, or mimic &#8220;x = [3..25]&#8220;</h2>
<p>Sometimes you need just a plain array with a set of numbers of a certain range, i.e. when generating a set of chars, like all upper case characters. But there is no &#8220;x = [1..5]&#8221; in JavaScript yet(!). But I needed one, and it&#8217;s easy to make one, as you can see:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">var</span> chars <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">65</span><span style="color: #339933;">;</span> chars.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> i<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">90</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">65</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">66</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">67</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">68</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">69</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">70</span><span style="color: #339933;">,</span> ... <span style="color: #CC0000;">85</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">86</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">87</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">88</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">89</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">90</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #006600; font-style: italic;">// Let's print them.</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> chars.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>String.<span style="color: #660066;">fromCharCode</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
A<br />
...<br />
<span style="color: #660066;">Z</span></div></td></tr></tbody></table></div>
<h2>Padding a string</h2>
<p>Sometimes you want to pad numbers with zeros e.g. to align them on the console. Here is how I did that:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'00'</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;003&quot;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'00'</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">42</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;042&quot;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'00'</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">103</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;103&quot;</span></div></td></tr></tbody></table></div>
<p>The -3 is the number*-1 of characters my string shall have in the end. I am adding two zeros at the beginning, because I am sure that a number is never an empty string. And after the string concatenation I just take only the last three characters. Simple but works nice without any kind of IFs or complex logic. </p>
<h2>array.unique()</h2>
<p>PHP has a hell lot of array functions, a bit wild &#8211; yeah. But sometimes you wished you had them all too. A unique() function is one of those that I just needed lately again, so here is a simple one:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">function</span> unique<span style="color: #009900;">&#40;</span>arrayData<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> arrayData.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> idx<span style="color: #339933;">,</span> arr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> arr.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> idx<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span><span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> unique<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> unique<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> unique<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;B&quot;</span><span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>So have fun, hope something was useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2012/01/19/javascript-snippets-isarray-arguments-to-array-x-y-padding-array-unique/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>State of &lt;audio&gt; in iOS5</title>
		<link>http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/</link>
		<comments>http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 09:00:26 +0000</pubDate>
		<dc:creator>David Pfahler &#38; Stephan Bönnemann</dc:creator>
				<category><![CDATA[html5]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1977</guid>
		<description><![CDATA[<em>This is a guest post by <a href="http://twitter.com/davidpfahler">David Pfahler</a> and <a href="http://twitter.com/boennemann">Stephan Bönnemann</a>.</em>

<h2>Why you’d like to use HTML5 audio in iOS 5</h2>
It’s all about apps these days. The question is whether you want to make the commitment and investment (of learning a new language) to program natively for one platform or leverage the web dev skills you already have? HTML5 is the way to go if you sympathize with the latter.]]></description>
			<content:encoded><![CDATA[<p><em>This is a guest post by David Pfahler Stephan Bönnemann of <a href="http://keeplook.in" title="keeplook.in">keeplook.in</a>.</em></p>
<h2>Why you’d like to use HTML5 audio in iOS 5</h2>
<p>It’s all about apps these days. The question is whether you want to make the commitment and investment (of learning a new language) to program natively for one platform or leverage the web dev skills you already have? HTML5 is the way to go if you sympathize with the latter.<br />
<span id="more-1977"></span></p>
<p>Let&#8217;s face it: The main problem for web developers that want to make apps is to deliver a great <strong>user experience</strong>. Hence, you&#8217;d like to use as much of today&#8217;s web technologies as possible to enhance the UX. One profound way of doing this is audio feedback. For example, many twitter apps use sounds if new tweets come in and another sound if no new tweets could be found. This kind of response is intuitive to the user. This is what you should go for.</p>
<p>Also, imagine a game without sounds. Would be kind of boring, wouldn&#8217;t it?</p>
<h2>Naive Assumptions: What you’d expect</h2>
<p>Fortunately in iOS we have rich HTML5 support built into mobile Safari. Initially, Steve Jobs wanted all apps to be web apps! So what you&#8217;d assume regarding audio is the presence of the HTML5 audio tag. And guess what, there <em>is</em> an implementation of the audio tag in iOS. Hurray!</p>
<p>So what do you expect from an audio tag implementation:</p>
<ul>
<li>call .load(), .play(), .pause() and other methods via JavaScript</li>
<li>use remote files, dataURIs, cached files and other sources</li>
<li>play several audio files / tags at once</li>
<li>mix audio from several sources with different volumes, etc.</li>
</ul>
<h2>Reality checks: What should work but doesn’t</h2>
<p>It becomes clear pretty fast that it&#8217;s not that simple. With iOS it&#8217;s a disaster. The bad news first:</p>
<p><strong>It is not possible to load or play audio without user interaction.</strong></p>
<p>Sad, but true. <a title="Safari Developer Library" href="http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/ControllingMediaWithJavaScript/ControllingMediaWithJavaScript.html#//apple_ref/doc/uid/TP40009523-CH3-SW5">Apple deliberately decided</a> that a touch event is mandatory to load and play audio. There is no workaround for this.</p>
<p>If we accept the fact that we need a touch event to play the audio, is it possible to use the audio tag as expected? Hmm, no, not really. So what&#8217;s broken as well?</p>
<p><strong>Latency</strong>: Loading audio files on demand (after the touch event fired) is unusably slow. The desired user experience can not be achieved using this technique.</p>
<p><strong>One audio tag at a time:</strong> You can never play more than one audio tag at once. This makes it impossible to mix sounds on the fly which would be necessary for games that have a background music and must also play sounds dynamically (e.g. when the character jumps).</p>
<p><strong>Audio files only:</strong> You can not use anything as the source besides audio files (uncompressed WAV and AIF audio, MP3 audio, and AAC-LC or HE-AAC audio). If you could use dataURIs then it would be possible to preload the audio.</p>

<a href='http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/mp3vsm4a/' title='mp3 vs m4a on average'><img width="150" height="150" src="http://uxebu.com/blog/wp-content/uploads/2011/11/mp3vsm4a-150x150.png" class="attachment-thumbnail" alt="mp3 vs m4a on average" title="mp3 vs m4a on average" /></a>
<a href='http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/mp3_latency/' title='mp3 latency test results'><img width="150" height="150" src="http://uxebu.com/blog/wp-content/uploads/2011/11/mp3_latency-150x150.png" class="attachment-thumbnail" alt="mp3 latency test results" title="mp3 latency test results" /></a>
<a href='http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/m4a_latency/' title='m4a latency test results'><img width="150" height="150" src="http://uxebu.com/blog/wp-content/uploads/2011/11/m4a_latency-150x150.png" class="attachment-thumbnail" alt="m4a latency test results" title="m4a latency test results" /></a>
<a href='http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/dataurifail/' title='epic dataUri fail on iOS 5'><img width="150" height="150" src="http://uxebu.com/blog/wp-content/uploads/2011/11/dataUriFail-150x150.png" class="attachment-thumbnail" alt="epic dataUri fail on iOS 5" title="epic dataUri fail on iOS 5" /></a>

<h2>Workarounds: How to play anyways (at least somehow)</h2>
<p>We invested a lot of time researching the audio tag on iOS 5. If you want to know how we play audio, read on.</p>
<p>So what do you do with this crippled audio tag? Well, you trick it a little bit and use it as much as possible. Here are several ideas that work best for different scenarios:</p>
<p><strong>Bind touchstart event to body:</strong> As you have to play on a touch event, we bind a touchstart handler on the body. So wherever the user touches first, we can use this event to load the audio file. Even better, from now on, we can load and play different sources from this audio tag.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">bodyEl.addEventListener(&quot;touchstart&quot;, function() {<br />
audioNode.load(); audioNode.play();<br />
}, false);</div></td></tr></tbody></table></div>
<p><strong>Hot swapping sources:</strong> We can now change the &#8220;activated&#8221; audio tag&#8217;s source on the fly. So it is now possible &#8211; without user interaction (!) &#8211; to load and play different sources. But you can only play one source at a time, of course.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">audioNode.addEventListener(&quot;ended&quot;, function() {<br />
audioNode.src = &quot;newSource.mp3&quot;;<br />
audioNode.load(); audioNode.play();<br />
}, false);</div></td></tr></tbody></table></div>
<p><strong>Use audio sprites:</strong> The guys over at Zynga use audio sprites in their <a title="Zynga Jukebox on github" href="https://github.com/zynga/jukebox">Jukebox</a>. This means that you have a huge audio file containing one sound that you want to play, then one second of silence, then another sound and so on.</p>
<p>Example for a sound sprite structure:<br />
1 second silence<br />
First sound<br />
1 second silence<br />
Second sound<br />
1 second silence<br />
Third sound</p>
<p>In this file you include all the audio that you possibly want to play in your app. You only load the source once (as described above) and then jump to the relevant parts in the file. So the file is always playing but most of the time it just plays silence. When you need the sound, you can quickly jump to the part in the file that you need to play. This solution is quick (much faster than hot swapping!) but requires a long preloading phase before you can play the most sounds, because the file is bigger.</p>
<h2>tl;dr</h2>
<p>So audio in iOS is pretty much broken. If you are using a native wrapper like PhoneGap you could use native APIs via plugins like <a href="https://github.com/purplecabbage/phonegap-plugins/tree/master/iPhone/SoundPlug" title="SoundPlug on github">SoundPlug</a>. We&#8217;ll try what is possible in the upcoming versions of iOS. Please let us know your ideas and feedback in the comments.</p>
<h2>Update:</h2>
<p>Regarding Philip&#8217;s comment, this is how we think it could be.</p>
<p><img src="http://img577.imageshack.us/img577/516/audiopref.png" width="200" class="size-medium" alt="Settings" style="border: 2px solid #CFCFCF;float: left" /><img src="http://img259.imageshack.us/img259/2771/audioalert.png" width="200" class="size-medium" alt="Alert" style="border: 2px solid #CFCFCF;float: left" /></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/11/29/state-of-html5-audio-in-ios-5/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>EmbedJS 0.2 and AMD</title>
		<link>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/</link>
		<comments>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 15:43:16 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[development tools]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[embedjs]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1843</guid>
		<description><![CDATA[During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and what this means for the user. Modules AMD stands for &#8220;Asynchronous Module [...]]]></description>
			<content:encoded><![CDATA[<p>During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and what this means for the user.<br />
<span id="more-1843"></span></p>
<h2>Modules</h2>
<p>AMD stands for &#8220;Asynchronous Module Definition&#8221;. So, what are modules? I&#8217;ll quote the definition from the <a href="http://wiki.commonjs.org/wiki/Modules/1.1" target="_blank">CommonJS Module specification</a>, as it describes it very well:</p>
<blockquote><p>This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API.</p></blockquote>
<p>So, modules are isolated parts of code that live in their own scope and are kind of self-contained. Well, that is pretty much how we see the individual Features in EmbedJS – we made our feature implementations as small as possible and reduced dependencies as much as possible. Many of EmbedJS&#8217; features are also browser-independent, so why not make it possible to use some of the tools in a non-browser environment?</p>
<p>(If you want to learn more on modules and why we need them, I recommend <a href="http://twitter.com/briancavalier" target="_blank">@briancavalier</a>&#8216;s <a href="http://briancavalier.com/presentations/pgh-js-amd-10-2011/" target="_blank">presentation on modules</a>.)</p>
<h2>Features in EmbedJS</h2>
<p>So far, I didn&#8217;t really explain what I mean with &#8220;Features&#8221;, so let me do another quote, this time from the <a href="http://uxebu.github.com/embedjs/" target="_blank">Project Page</a>:</p>
<blockquote><p>EmbedJS uses the concept of features: Functionalities are split up into features, as fine-grained as possible, and each feature might have multiple implementations.</p></blockquote>
<p>Our Features are kinda small and contain only few methods, sometimes even just one. The asyc-promise feature, for example, will get you Promises, and the transport-jsonp feature will get you a method for pull in data via jsonp – and that&#8217;s it.  If multiple methods are contained within one feature, this is because they are too closely related to each other to split them up, like the html-class feature, which contains the <code class="codecolorer text mac-classic"><span class="text">addClass</span></code>, <code class="codecolorer text mac-classic"><span class="text">removeClass</span></code>, <code class="codecolorer text mac-classic"><span class="text">toggleClass</span></code> and <code class="codecolorer text mac-classic"><span class="text">hasClass</span></code> methods.</p>
<h2>AMD vs CommonJS Modules 1.1</h2>
<p>Now that we knew modules were the way to go, there was another, quite important question to solve: There are two common patterns for modules, so which one do we want to apply? One pattern is described in the above mentioned CommonJS Wiki, the other pattern is described in the <a href="https://github.com/amdjs/amdjs-api/wiki/AMD" target="_blank">AMD wiki</a>.</p>
<p>Well, there have been <a href="http://tagneto.blogspot.com/2011/04/on-inventing-js-module-formats-and.html" target="_blank">some</a> <a href="http://unscriptable.com/index.php/2011/09/30/amd-versus-cjs-whats-the-best-format/" target="_blank">blog</a> <a href="http://blog.millermedeiros.com/2011/09/amd-is-better-for-the-web-than-commonjs-modules/" target="_blank">posts</a> lately that compare the pros and cons of each of them, so I don&#8217;t want to repeat all of this – but I&#8217;m hiving a similar tendency towards AMD. Besides this, there was another big reason to go with AMD: Remember, EmbedJS is based on the <a href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit</a>, and it&#8217;s a <a href="http://dojofoundation.org/" target="_blank">Dojo Foundation Project</a> – and we hope that the efforts that went into making EmbedJS might flow back into the Dojo Toolkit one day. And as the Dojo Toolkit uses the AMD approach, the decision was quickly made to do so as well.</p>
<h2>Implications for EmbedJS</h2>
<p>There are several huge benefits for EmbedJS in using the AMD approach. The most noteworthy surely is the ease-of-use for developers. In your code, when you find you want an EmbedJS method, you <a href="http://uxebu.github.com/embedjs/docs/" target="_blank">look up the feature name</a> that provides this method, add it to the require statement and there you go. Man, easy.</p>
<p>A second important thing is that EmbedJS now plays really nice with AMD based projects. In fact, you could see EmbedJS now as a collection of small useful modules, that can be easily integrated – and also easily extended.</p>
<p>The third major benefit is that we can now get rid of all the custom tools we built around EmbedJS – you can load features with any AMD loader (though we use and recommend RequireJS) and build it with any AMD optimizer/builder (we recommend r.js).</p>
<p>Let&#8217;s have an extra word on</p>
<p><strong>Building</strong></p>
<p>It was always possible with EmbedJS to create a custom build that would contain only the features you wanted/needed in your project. But, you had to use our build tool that we created. And learning &amp; using Yet-Another-Build-Tool™ is always… well, let&#8217;s just say it would be cool to avoid it (and, not to forget, so is maintaining it). We did a lot to improve that, we created a GUI that allowed you to just &#8220;click together&#8221; the features you wanted, and we worked on a dynamic loader that would ease development. But still, the situation was far from ideal, and people just ended up going for our ready-made &#8220;kitchensink&#8221; builds that contained <em>all</em> of the features.</p>
<p>Now, we still provide those ready-made builds that contain only the implementations for a given platform (and are stripped of require/define calls), but I really hope that using the AMD approach and using an AMD optimizer to deploy will become the more adopted style.</p>
<h2>Q: Cross-target? A: The Feature Plugin</h2>
<p>One goal of EmbedJS is to allow for easy cross-device development. We got rid of run-time branching but instead went for an approach that held the different implementations for a given feature in separate files, so that one only has to deploy the actually needed code to a given device – and not all the alternate implementations, that sit useless in the device&#8217;s memory and never get used. We needed to achieve that with &#8220;in-house&#8221; means provided by the AMD approach. Lucky us, there&#8217;s the concept of <a href="https://github.com/amdjs/amdjs-api/wiki/Loader-Plugins" target="_blank">loader-plugins</a>, and so I created a <a href="https://github.com/jensarps/AMD-feature" target="_blank">feature-plugin for AMD loaders</a>. The idea is simple yet effective: You create an implementation map that maps a feature name to an implementation, and if there are multiple implementations, you supply tests that tells the plugin which feature to use.</p>
<p>When you&#8217;re ready to deploy, you have two choices: I f you really don&#8217;t know on what target your code is going to end up, you can use the dynamic implementation map containing the tests for the optimizer, and you&#8217;ll get a build that contains all of the available implementations. Then, during runtime, the feature plugin will run the tests and load the specific implementation. But, if you <em>do</em> know what target your code will run on, you provide a specific implementation map that doesn&#8217;t contain tests but directly maps to the according implementations, and you will only have those in your built file.</p>
<h2>Synopsis</h2>
<p>AMD is exactly what we needed to make EmbedJS more what we want it to be – an easy to use JavaScript toolbox that is especially handy for cross-target scenarios such as cross-platform mobile development. So, please don&#8217;t hesitate and check it out on Github, read the project page and check out the examples!</p>
<p>&nbsp;</p>
<p><strong>Resources / Further Reading</strong></p>
<ul>
<li><a href="https://github.com/uxebu/embedjs" target="_blank">EmbedJS on GitHub</a></li>
<li><a href="http://uxebu.github.com/embedjs/" target="_blank">EmbedJS Project Page</a></li>
<li><a href="http://uxebu.github.com/embedjs/docs/" target="_blank">EmbedJS API Docs</a></li>
<li><a href="https://github.com/jensarps/AMD-feature" target="_blank">AMD-Feature plugin</a></li>
<li><a href="https://github.com/amdjs/amdjs-api/wiki" target="_blank">AMD Wiki</a></li>
<li><a href="http://wiki.commonjs.org/wiki/CommonJS" target="_blank">CommonJS Wiki</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>My console.log() strategy</title>
		<link>http://uxebu.com/blog/2011/09/02/my-console-log-strategy/</link>
		<comments>http://uxebu.com/blog/2011/09/02/my-console-log-strategy/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 10:00:36 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[development tools]]></category>
		<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[console.log]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1723</guid>
		<description><![CDATA[This time I feel like sharing my (maybe) strange looking console.log() strategy, but I find it very effective, read on to learn why. If I can&#8217;t debug, for whatever reason and have to fall back to console.log()s you normally see my code looking like this: 123456789console.log&#40;&#34;i = &#34;, i&#41;; &#160; &#160; if &#40;i==0&#41;&#123; console.log&#40;1&#41;; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>This time I feel like sharing my (maybe) strange looking <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code> strategy, but I find it very effective, read on to learn why.<br />
<span id="more-1723"></span></p>
<p>If I can&#8217;t debug, for whatever reason and have to fall back to <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code>s you normally see my code looking like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;i = &quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Do something here</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Do else </span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>You can see two things here:</p>
<ul>
<li>the first line prints out a variable&#8217;s value, simple I guess</li>
<li>all <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code>s are not indented at all</li>
</ul>
<p>Let me touch on the &#8220;not indenting&#8221; first.<br />
I don&#8217;t like to indent <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code>s because this just makes the code look ugly, and that is intentional, because <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code> normally just doesn&#8217;t belong in the code. By making the code look ugly I am quickly forced to remove them again, the latest I will stumble on it before committing, because I never commit without looking at the diff.<br />
If I ever forget to remove the <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code> I can be sure that a coworker will be annoyed by it and hopefully either ping me or remove it. But I am trying my best not to forget it :).</p>
<h2>Komodo macro</h2>
<p>Back to the first <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code> you see in the code example up there, that one is actually one keystroke away for me, I have hacked a simple macro in my editor (<a href="http://www.activestate.com/komodo-edit">Komodo Edit</a>) that takes the text I selected and creates a <code class="codecolorer text mac-classic"><span class="text">console.log()</span></code> in the line below throwing out this variable and it&#8217;s value.<br />
This little script looks like this:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> ko.<span style="color: #660066;">views</span>.<span style="color: #660066;">manager</span>.<span style="color: #660066;">currentView</span>.<span style="color: #660066;">scimoz</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> s.<span style="color: #660066;">selText</span><span style="color: #339933;">;</span><br />
komodo.<span style="color: #660066;">doCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cmd_end'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
komodo.<span style="color: #660066;">doCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cmd_newlineBare'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Only if something was selected put it there.</span><br />
t <span style="color: #339933;">=</span> t <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">+</span>t<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; = ', &quot;</span><span style="color: #339933;">+</span>t<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;arguments&quot;</span><span style="color: #339933;">;</span><br />
s.<span style="color: #660066;">insertText</span><span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">currentPos</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;console.log(&quot;</span><span style="color: #339933;">+</span>t<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;);&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Set the cursor inside the &quot;()&quot; at the end, so we can add</span><br />
<span style="color: #006600; font-style: italic;">// parameters</span><br />
komodo.<span style="color: #660066;">doCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cmd_end'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
komodo.<span style="color: #660066;">doCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cmd_left'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
komodo.<span style="color: #660066;">doCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cmd_left'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Just create a new macro with it and attach a key-combination to it and logging a variable is just one key stroke away.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/09/02/my-console-log-strategy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Snippets: create a sequence, chop off last character, convert int to hex</title>
		<link>http://uxebu.com/blog/2011/08/09/javascript-snippets-create-a-sequence-chop-off-last-character-convert-int-to-hex/</link>
		<comments>http://uxebu.com/blog/2011/08/09/javascript-snippets-create-a-sequence-chop-off-last-character-convert-int-to-hex/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 03:55:49 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1592</guid>
		<description><![CDATA[As I had promised, I will start blogging a couple more code snippets, so (basically) I can look them up and don&#8217;t forget how to do small things. If you find them useful, I am glad, if not it&#8217;s a good lookup resource (for me). So this time, I have three code snippets. Create a [...]]]></description>
			<content:encoded><![CDATA[<p>As I had promised, I will start blogging a couple more code snippets, so (basically) I can look them up and don&#8217;t forget how to do small things. If you find them useful, I am glad, if not it&#8217;s a good lookup resource (for me).<br />
So this time, I have three code snippets.<br />
<span id="more-1670"></span></p>
<h2>Create a string of sequenced numbers</h2>
<p>That is definitely not the solution I really like, but that&#8217;s the one I have decided on (just because I don&#8217;t remember the cool one I had created some time ago, and I can&#8217;t find it).</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #006600; font-style: italic;">// Create a string &quot;12345678910&quot; by just giving the endpoint. </span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #003366; font-weight: bold;">var</span> endPoint <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> arr<span style="color: #339933;">=</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>i <span style="color: #339933;">&lt;</span> endPoint<span style="color: #339933;">;</span>i<span style="color: #339933;">++,</span> arr.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> arr.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;12345678910&quot;</span></div></td></tr></tbody></table></div>
<h2>Chop of the last character of a string</h2>
<p>[Update]<br />
Doh, <a href="http://twitter.com/wpbasti/statuses/100813445741547520">thx to Sebastian</a> this is actually really easy :)</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #3366CC;">&quot;abc&quot;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #3366CC;">&quot;ab&quot;</span></div></td></tr></tbody></table></div>
<p>[/Update]</p>
<p>Actually this one works (could work) for any number of character, that you want to chop off the end of a string. After throwing <a href="http://twitter.com/wolframkriesing/statuses/100706693553471488">ideas forth</a> <a href="http://twitter.com/Tojiro/statuses/100710329780154369">and back</a> <a href="http://twitter.com/wolframkriesing/statuses/100710994069815297">on twitter</a>, this one was the outcome.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #006600; font-style: italic;">// Cut off the last character, to make &quot;abc&quot; into &quot;ab&quot;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> initialString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;abc&quot;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>initialString<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;ab&quot;</span></div></td></tr></tbody></table></div>
<p>I like this approach.</p>
<h2>Convert int to hex</h2>
<p>Thanks to <a href="http://twitter.com/#!/jdalton">John</a> I learned after a couple (more complex) approaches that converting decimal numbers into hexadecimals is as easy as:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> Number<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;b&quot;</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #006600; font-style: italic;">// Or to make it a &quot;real&quot; hex number :)</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #3366CC;">&quot;0x0&quot;</span> <span style="color: #339933;">+</span> Number<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #3366CC;">&quot;0x0B&quot;</span></div></td></tr></tbody></table></div>
<p>If you wonder now how to convert back, from a hex number to a decimal, try this on your JavaScript console.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&gt;&gt;&gt;</span> 0x0B<br />
<span style="color: #CC0000;">11</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #006600; font-style: italic;">// If you get the &quot;B&quot; as a string, just do:</span><br />
<span style="color: #339933;">&gt;&gt;&gt;</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0x0B&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #CC0000;">11</span></div></td></tr></tbody></table></div>
<p>Easy, ha!?</p>
<p>Have fun snippeting around, I hope some was helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/08/09/javascript-snippets-create-a-sequence-chop-off-last-character-convert-int-to-hex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comparing (in) JavaScript and Python</title>
		<link>http://uxebu.com/blog/2011/08/08/comparing-in-javascript-and-python/</link>
		<comments>http://uxebu.com/blog/2011/08/08/comparing-in-javascript-and-python/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 05:00:08 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[pyhton]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1581</guid>
		<description><![CDATA[I just wrote a little bit of intuitive code, at least I thought it was intuitive but it bit me. As it was supposed to and I think it shouldn&#8217;t have bitten me, but after I saw why I understood that some languages are more intuitive in parts than others, and in this case it [...]]]></description>
			<content:encoded><![CDATA[<p>I just wrote a little bit of intuitive code, at least I thought it was intuitive but it bit me. As it was supposed to and I think it shouldn&#8217;t have bitten me, but after I saw why I understood that some languages are more intuitive in parts than others, and in this case it was Python that won over JavaScript. Ooops, I said that on our JavaScript company blog. Well, even from truths we can&#8217;t run. So we don&#8217;t and just add another piece of learning in the front parts of our brains instead in the back part.<br />
<span id="more-1669"></span><br />
In JavaScript I did:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">=</span> y <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<span style="color: #ff4500;">1</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">1</span> <span style="color: #66cc66;">==</span> y<br />
true</div></td></tr></tbody></table></div>
<p>seems good to me.<br />
Now in Python:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">=</span> y <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">1</span> <span style="color: #66cc66;">==</span> y<br />
<span style="color: #008000;">True</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">=</span> y <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">2</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">==</span> y<br />
<span style="color: #008000;">True</span></div></td></tr></tbody></table></div>
<p>Way to go. Now comes the biting part:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">=</span> y <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">2</span><br />
<span style="color: #ff4500;">2</span><br />
<span style="color: #66cc66;">&gt;&gt;&gt;</span> x <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">==</span> y<br />
false // ouch<span style="color: #66cc66;">,</span> right<span style="color: #66cc66;">,</span> so JavaScript doesn<span style="color: #483d8b;">'t compare as Pyhton does, aha!<br />
&gt;&gt;&gt; (x == 2) == y // That is what JavaScript actually does.<br />
false<br />
&gt;&gt;&gt; (x == 2) == true // Ok, now I see it.<br />
true<br />
&gt;&gt;&gt; x == 2 &amp;&amp; y == 2 // Ok, so we have to do the extra code, ok, relearned.<br />
true</span></div></td></tr></tbody></table></div>
<p>Was obvious right, yes it was, but Python does it more intuitively.<br />
PS: I know the uber geeks will just yawn, but that&#8217;s ok.<br />
PS2: I just decided to start sharing bits and pieces of code again by blogging. Sometimes I need them again. I used to do that on my private blog but didn&#8217;t continue, so now I restart and hope to continue.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/08/08/comparing-in-javascript-and-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaScript, HTML5, CSS3 workshop</title>
		<link>http://uxebu.com/blog/2011/06/10/javascript-html5-css3-workshop/</link>
		<comments>http://uxebu.com/blog/2011/06/10/javascript-html5-css3-workshop/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 13:58:56 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[frontend engineering]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5apps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[uxebu]]></category>
		<category><![CDATA[workshop]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1554</guid>
		<description><![CDATA[Uxebu is spreading its knowledge, get some of it &#8211; for 699€ if you are an early bird (999€ later)! Uxebu is now operating it&#8217;s business purely in the JavaScript space for about three years (oh that reminds me we should have a party!). We are convinced that JavaScript is ramping up now and we [...]]]></description>
			<content:encoded><![CDATA[<h2>Uxebu is spreading its knowledge, get some of it &#8211; for 699€ if you are an early bird (999€ later)!</h2>
<p><span id="more-1554"></span></p>
<p>Uxebu is now operating it&#8217;s business purely in the JavaScript space for about three years (oh that reminds me we should have a party!). We are convinced that JavaScript is ramping up now and we see that there are more and more things done and possible with the language. The browser landscape is developing rapidly and changing every day. The mobile industry is pushing the limits and we are helping to do our part in it. We learned a lot and are still learning. As it is in the nature of our company to spread our knowledge to allow everyone to use the technology at it&#8217;s best and push the limits with us, we think now it&#8217;s the time that we share our knowledge and pass it on to those hungering for diving into JavaScript!</p>
<p>We really have <a href="http://twitter.com/void_0">gathered</a> <a href="http://twitter.com/jensarps">a lot</a> of <a href="http://twitter.com/tobeytailor">bright</a>, <a href="http://twitter.com/evilhackerdude">bleeding-edge</a> <a href="http://twitter.com/kuvos">people</a> and each of us is learning from another everyday. If you want to get some of the know how (at least as much as we can pass on in a two day workshop) you are invited to <a href="http://uxebu.com/html5-workshop/">join our workshops</a>. There will be one held in <a href="http://de.amiando.com/jsmunich.html"><strong>Munich 29th and 30th of June</strong></a> and another one in <a href="http://de.amiando.com/jsamsterdam.html"><strong>Amsterdam 11th and 12th of July</strong></a>. We will always have at least two uxebus teaching you the latest and hottest. We might even have some of the latest <a href="http://twitter.com/twtomcat/statuses/79157866954637313">bleeding-edge mobile devices</a> for you to run the app you will develop in the workshop.</p>
<hr style="margin: 4em 0;" />
<h2>Workshop Details</h2>
<p>In this course we will cover a broad spectrum from JavaScript essentials to the bleeding edge of cross platform HTML5 development. We start from the ground up and go step by step, depending on the level of the participants we accelerate to show you the deep and dark sides of JavaScript.</p>
<p>Each day of the course is split into an intensive “knowledge transfer” session in the morning and a hands on hacking workshop in the afternoon in which you will apply the contents learned during the morning. At the end you will have touched all contents not only through theory but also through intensive real life application and coding.</p>
<p><em>Note that this course will not explain you how to work with jQuery and also not how to work without jQuery. The course mainly focuses on JavaScript, mobile and HTML5.</em></p>
<h2>Day 1 (part 1) &#8211; JavaScript</h2>
<p>We start with a small overview of the history. We tackle the basic structure of the language and move on with <strong>statements</strong>, <strong>expressions</strong>, <strong>functions</strong> and <strong>objects</strong>. We touch the different <strong>operators</strong> and when semi colons are introduced.</p>
<p>We continue our way through the JavaScript landscape by taking a look at <strong>objects</strong>, <strong>properties</strong> and <strong>built-in functionality</strong>. At that point we’ll encounter our first major obstacles; <strong>scopes</strong> and <strong>closures</strong>. Once we conquer these we touch on the <strong>difference between functions and constructors</strong>. This is followed by our second major obstacle; <strong>prototype</strong> and the various values of the <strong>this keyword</strong>.</p>
<h2>Day 1 (part 2) &#8211; Hackday: HTML5, CSS3</h2>
<p>During the second part of day one, we will take what we have learned in the morning and mix it with a little shot of HTML5 fanciness. We will look at all those new APIs we can now access on our (mobile) devices to bring JavaScript to a new level. We will include cool APIs such as the <strong>Audio API, Video API, history, Canvas, SVG, File API, WebGL, DeviceMotion, WebWorkers, WebSocket, GPS</strong> &#8211; and if all ingredients fit well you might just end up with your first app written in JavaScript.</p>
<h2>Day 2 (part 1) &#8211; The App Day</h2>
<p>During the second day we willl continue with <strong>timers</strong>, <strong>callbacks</strong>, <strong>web workers</strong>, <strong>eval</strong> and the <strong>future of JavaScript</strong>. The speed of the course is matched with it’s participants. The important parts of the languages will be handled in the workshop. During the first part of day two we will also specifically look into topics which you want to cover after determining those during day one. This will give you the chance to get all your open questions answered so that you have your pockets full of knowledge after the workshop is over.</p>
<h2>Day 2 &#8211; part two (Mobile)</h2>
<p>JavaScript has grown strongly into the mobile space, so we will show you how to get the app you have started building during day one onto <strong>mobile devices</strong>. You will learn about <strong>media queries</strong>, some <strong>device sensors</strong>, <strong>performance</strong> and HTML5/CSS3/JavaScript tricks and how to optimize some parts for mobile and how to make a <strong>native app</strong> out of it, so you finally could even make get it into the <strong>app store</strong> and make money with it.</p>
<h2>Now you want to join our workshop?</h2>
<p>Join us either</p>
<ul>
<li>in <a href="http://de.amiando.com/jsmunich.html"><strong>Munich 29th and 30th of June</strong></a>or</li>
<li>in <a href="http://de.amiando.com/jsamsterdam.html"><strong>Amsterdam 11th and 12th of July</strong> </a></li>
</ul>
<p><a href="http://uxebu.com/html5-workshop/"><img style="position: fixed; left: 0; top: 0;" src="http://uxebu.com/html5-workshop/workshop-badge.png" alt="Workshop: " /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/06/10/javascript-html5-css3-workshop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating a persistent Dojo Object Store</title>
		<link>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/</link>
		<comments>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 16:13:45 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[offline]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1540</guid>
		<description><![CDATA[As of version 1.6, dojo comes with the new Dojo Object Store API. This is an awesome thing, as it greatly simplifies the work with data stores in Dojo. Everybody who had to do with the traditional dojo.data API felt it was overly complex and hard to use – this has finally changed now. There [...]]]></description>
			<content:encoded><![CDATA[<p>As of version 1.6, dojo comes with the new <a href="http://docs.dojocampus.org/dojo/store" target="_blank">Dojo Object Store API</a>. This is an awesome thing, as it greatly simplifies the work with data stores in Dojo. Everybody who had to do with the traditional dojo.data API felt it was overly complex and hard to use – this has finally changed now. There are also wrappers from and to the old and new APIs, so that you can do stuff like using your traditional data-aware widgets with a new Object Store. And the goodness doesn&#8217;t end here; but more on this later. If you haven&#8217;t done so yet, you might want to read the <a href="https://www.sitepen.com/blog/2011/02/15/dojo-object-stores/" target="_blank">excellent post on the new Dojo Object Stores</a> by Kris Zyp where he explains all the awesomeness he created.</p>
<p>Dojo also comes with two fresh implementations for the API, a non-persistent memory store and a JsonRest store that interacts with a server through RESTful HTTP requests. You can also observe changes made to objects in the store. What is missing is a store that uses a client-side persistent backend, which would be useful for a couple of reasons (e.g., as one commenter in that post asks, to use it as chache store for dojo.store.Cache). As Kris already mentions, it&#8217;s a piece of cake to create one, so let&#8217;s go ahead and do it!<br />
<span id="more-1540"></span></p>
<h2>Choosing the Backend</h2>
<p>The post mentions two possible candidates: dojox.storage and <a href="https://github.com/jensarps/StorageJS" target="_blank">StorageJS</a>, both already having a similar API, so making them compliant to the Dojo Object Store API is fairly easy. For this tutorial I choose StorageJS – because it is very lightweight and still provides all we need (and, well, for a couple of other reasons that don&#8217;t belong in this post). So, what is the exact API we need to comply to? Check it out <a href="http://docs.dojocampus.org/dojo/store#id2" target="_blank">in the docs</a>. See the first sentence in that paragraph? <em>&#8220;Every method in the API is optional, it&#8217;s presence indicating support for that feature.&#8221;</em> Wow, that&#8217;s nice!</p>
<h2>Basic compliance</h2>
<p>Let&#8217;s start with the methods get(), put(), add() and remove(). That will make our store pretty usable for most store scenarios.</p>
<p>First, create a &#8216;store&#8217; object we can work with (StorageJS creates a global variable called <code class="codecolorer text mac-classic"><span class="text">storage</span></code> where all it&#8217;s methods reside):</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> store <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span>storage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>&#8216;store&#8217; now contains everything that &#8216;storage&#8217; does, but we can leave the original &#8216;storage&#8217; object alone and modify our &#8216;store&#8217; object if needed. Time for the first method, let&#8217;s start with <code class="codecolorer text mac-classic"><span class="text">get()</span></code>. StorageJS works with Strings as keys and values only, so we need to parse what we get from it.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">get</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Need to parse the string we get from backend:</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">fromJson</span><span style="color: #009900;">&#40;</span>storage.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Easy one, now move on to <code class="codecolorer text mac-classic"><span class="text">put()</span></code>. The Object Store API allows user to do stuff like this: <code class="codecolorer text mac-classic"><span class="text">store.put({foo: 'bar'}, {id: 3})</span></code> as well as <code class="codecolorer text mac-classic"><span class="text">store.put({id: 3, foo: 'bar'})</span></code>. Also, StorageJS only has the method set() instead of put() and add().</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">put</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get the id, wherever it comes from</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> options <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> options.<span style="color: #660066;">id</span> <span style="color: #339933;">||</span> object.<span style="color: #660066;">id</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Stringify data -- remember, StorageJS is a key/value backend wrapper.</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">toJson</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now save the data away</span><br />
&nbsp; &nbsp; storage.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Easy, as well – you could write this as a one-liner. Now, <code class="codecolorer text mac-classic"><span class="text">add()</span></code>. It&#8217;s close to put, except that it won&#8217;t overwrite data that already exists. So we just need to check if something with the given id already exists and throw an error if so.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get the id</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> options <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> options.<span style="color: #660066;">id</span> <span style="color: #339933;">||</span> object.<span style="color: #660066;">id</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Check if something exists under the given</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// id -- StorageJS always returns null when</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// there's no data for a certain key.</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>storage.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Object already exists&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// We're save to go now! So hand over the</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// request to the put() method.</span><br />
&nbsp; &nbsp; store.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>object<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>What about <code class="codecolorer text mac-classic"><span class="text">remove()</span></code>? Nothing to do there, it already has the desired signature and functionality.</p>
<p>That&#8217;s it, we&#8217;re done with basic compliance. Our &#8216;store&#8217; object now implements the basic methods of the Dojo Object Store API and can be used by anything that can work with a Dojo Object Store – and it will keep it&#8217;s state persistent in the browser.</p>
<h2>More compliance? Querying!</h2>
<p>There&#8217;s one interesting thing in the API: the <code class="codecolorer text mac-classic"><span class="text">query()</span></code> method. Uh, yeah, we want that! Lucky us, Dojo already provides a query engine as well as a method that makes sure there are iterative methods and a <code class="codecolorer text mac-classic"><span class="text">total</span></code> property containing the number of hits available in our result (well, it also abstracts away the differences between sync and async results, but as StorageJS is purely synchronous, this is not important to us). First, define a queryEngine:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Let's take the provided queryEngine :)</span><br />
store.<span style="color: #660066;">queryEngine</span> <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">store</span>.<span style="color: #660066;">util</span>.<span style="color: #660066;">SimpleQueryEngine</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>So, if we want our store to be able to run queries, all we need to do is to take the store&#8217;s data and hand it over to the query engine. We use StorageJS&#8217; getAll() method to acquire the data. It will return an array of objects in the form { key: &#8216;theKey&#8217;, value: &#8216;theValue&#8217;}, but the queryEngine wants an array containing only the objects, so we need to do a conversion.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">store.<span style="color: #660066;">query</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Get all data from the backend</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rawData <span style="color: #339933;">=</span> storage.<span style="color: #660066;">getAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// And convert it into a format that the</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// queryEngine can work with</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> rawData.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> rawData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dataObject <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">fromJson</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; data.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>dataObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now call the queryEngine on the data</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">queryEngine</span><span style="color: #009900;">&#40;</span>query<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// And return the result, after it has been</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// enhanced.</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">store</span>.<span style="color: #660066;">util</span>.<span style="color: #660066;">QueryResults</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we can query our store for specific data, like so: <code class="codecolorer text mac-classic"><span class="text">var results = store.query({ prime: true });</span></code> This will give us all objects in the store that have a property called &#8216;prime&#8217; and have that property set to true.</p>
<h2>Hierarchie and Transactions</h2>
<p>The API also mentions methods about data hierarchie and transactions, but I&#8217;m not going to cover these in this tutorial.</p>
<h2>Convenience and Optimization</h2>
<p>Currently, every time we run a query, we fetch all data again from our storage engine. So, if you don&#8217;t have a massive amount of data and want to query the store a lot, it might be wise to maintain a copy of all data in memory. There are other things you could optimize as well – or you could write your own queryEngine.</p>
<p>For your convenience,  <a href="http://statis.uxebu.com/~jens/dojo.store/PersistentLocal.js" target="_blank">here&#8217;s a wrapper</a> that creates a Dojo Object Store like we did above and accepts a useMemory parameter during instantiation that denotes whether a copy of the data should be kept in memory or not. It uses the traditional require/provide instead of the AMD format. If you want to use it, make sure you use the right namespace in the declare() call! (Or, not recommended, copy it in the /dojo/store directory.)</p>
<p>If you want to fool around with it in the console first, you can go <a href="http://static.uxebu.com/~jens/dojo.store/index-persistent-nomemory.html" target="_blank">here (non-memory-using)</a> or <a href="http://static.uxebu.com/~jens/dojo.store/index-persistent-usememory.html" target="_blank">here (memory-using)</a>, check out the source and fire up the console and try things out.</p>
<h2>Getting StorageJS</h2>
<p>The above code uses the features base and getAll, so you could do a specific build of StorageJS with these features or just <a href="https://github.com/jensarps/StorageJS/tree/master/builds?raw=true" target="_blank">grab a full build</a> of it for your desired storage engine (the storage-full-[engine].js files). If you don&#8217;t know about storage engines, all modern browser support localStorage, so <a href="https://github.com/jensarps/StorageJS/raw/master/builds/storage-full-localStorage.js" target="_blank">this is the one</a> what you might want.</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/04/27/creating-a-persistent-dojo-object-store/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coworking, Spanish geeks and more</title>
		<link>http://uxebu.com/blog/2011/04/06/coworking-spanish-geeks-and-more/</link>
		<comments>http://uxebu.com/blog/2011/04/06/coworking-spanish-geeks-and-more/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 08:09:43 +0000</pubDate>
		<dc:creator>Wolfram Kriesing</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[uxebu]]></category>
		<category><![CDATA[coding dojo]]></category>
		<category><![CDATA[coworking]]></category>
		<category><![CDATA[valencia]]></category>

		<guid isPermaLink="false">http://uxebu.com/blog/?p=1519</guid>
		<description><![CDATA[After the MWC in Barcelona I moved my working place to Valencia for a week, a little further south. That is already a while ago, but still I wanted to write a little bit about all the experiences. Not last because I very much want to thank all the people who I have met, who [...]]]></description>
			<content:encoded><![CDATA[<p>After the MWC in Barcelona I moved my working place to Valencia for a week, a little further south. That is already a while ago, but still I wanted to write a little bit about all the experiences. Not last because I very much want to thank all the people who I have met, who introduced me into the spanish community and made all this so much fun. Thank you very much first and foremost <a href="http://twitter.com/#!/coworkvalencia">Rosa</a> &#8211; you rock! Thank you <a href="http://twitter.com/#!/XaV1uzz">Xavi</a>, <a href="http://twitter.com/#!/txau">Jaume</a>, Jorge, Luis, and &#8230; fuck I am so bad with names :(.<br />
<span id="more-1519"></span></p>
<h2>Coworking in Valencia</h2>
<p>My week in Valencia started out to look very quiet and just pure work. I tried to contact some JavaScript/mobile/web guys in this area before hand, but somehow didn&#8217;t really succeed. Some tweets went around but I didn&#8217;t really reach anybody. But I made sure I have a great place to work at, which was <a href="http://coworkingvalencia.com/">Coworking Valencia</a>, Rosa Montesa runs this place with so much passion &#8211; wow. And I just had to think of the <a href="http://www.combinat56.de">Combinat56 Coworking</a> space in Munich where we normally go, which is run by Sina with an equal lot of passion. Just great girls!</p>
<p>So I arrived there Monday morning to do my work, and off it went. I chatted a while with Rosa and some other coworkers and from there I couldn&#8217;t stop it. At the end of the day my calendar was filled with lunches and meetups. I met a lot of interesting people, first Rosa had organized a small come-together the next day with the company <a href="http://www.brainstorm.es">Brainstorm</a> from right above, mainly hackers too, so we had quite some stuff in common. There I met Jorge who introduced me to more geeky people, thank you so much!</p>
<h2>Spanish way of life</h2>
<p>So the week went on and I tried to work a little bit, but I was basically so happy to live the spanish way of life a little bit. In the morning you don&#8217;t buy a take away coffee, actually you don&#8217;t see the busy people running around with their coffee mugs in there hands. I also just did that the first day, after that I adopted the spanish way, which is sitting down in a bar (also if it just for five minutes) and have a coffee. A lot of people there have a <a href="http://en.wikipedia.org/wiki/Carajillo">carajillo</a> in the morning but that was not appropriate for my mornings :).<br />
For lunch (later than normal for me, around after 2pm) we mostly went to some place outside, since it was at least 20°C, beautiful sun just perfect. And then on the way home you just take some tapas maybe a beer in the bar around the corner and late at night (normally after 9pm) we had dinner. For me working there felt really comfortable and easy going. But I have also heard that normally working in Spain is not normally that relaxed.</p>
<h2>The real geeks</h2>
<p>On Thursday I went to the <a href="http://www.becodemyfriend.com/">BeCode</a> guys&#8217; office in the nice <a href="http://es.wikipedia.org/wiki/El_Carmen_(Valencia)">Barrio del Carmen</a>. The first thing after I arrived was &#8220;let&#8217;s go for a coffee&#8221; :). That is the spanish style! We exchanged the ideas of how we work and the kind of projects we do. Xavi the daddy of BeCode (scnr) told me that they are an incubator and try to have people come in and realize their ideas, pretty cool stuff. Was so refreshing and forward-thinking, I felt home.<br />
Funny enough that day I learned about <a href="http://pivotal.github.com/jasmine/">Jasmine</a> there, they used in a project. Unfortunately we had no internet that day, so I was pretty much offline until the afternoon, which was somehow good, but not when you really want to get some things done. So I promised to be back the next day and then the office was also really crowded and I got to meet all of the <a href="http://www.becodemyfriend.com/equipo/">BeCode team</a>. In the evening I joined my first <a href="http://blog.dev.openfinance.es/2011/03/ii-coding-dojo/">coding dojo</a>, this one was held in the offices of <a href="http://blog.dev.openfinance.es/">OpenFinance</a>. We were given the task to write a roman to latin numbers converter in whatever programming language you like. You are given half an hour and try to get as far as you can, I joined forces with <a href="http://twitter.com/#!/txau">Jaume</a> we got it easily done in the half an hour time, we simply used the <a href="https://github.com/vf/web-testsuite">webtestsuite</a> as our testing framework since I know that one best and of course we wrote the converter in JavaScript :). As a second Kata you could hack the conversion the other way around. Jaume and I felt that this would be just another hacking it the other way job was, so we looked for a new challenge and we said let&#8217;s just use the google search engine for converting it for us and we just grab the result from there. We looked at a couple of APIs, at YQL but there was no real solution, so we went the scraping route. We got it almost done :). But as you <a href="http://www.flickr.com/photos/57436095@N03/sets/72157626167343224/with/5485645335/">can see</a> <a href="http://blog.dev.openfinance.es/2011/03/ii-coding-dojo/">and read</a> we had lots of fun and enough beer!<br />
Of course we went for dinner later and were still hanging out some time around Valencia and in the BeCode office later. There the guys told me about an Code Retreat which is going to take place somewhere near Valencia &#8220;en el campo&#8221; with <a href="http://en.wikipedia.org/wiki/Paella">Paella</a> cooking and hacking, I hope that some of us uxebus may join, hope to find out details soon.</p>
<p>All in all a big thank you to all you guys and I can just say I had a great, though also really exhausting, two weeks in Spain. Hope to see you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://uxebu.com/blog/2011/04/06/coworking-spanish-geeks-and-more/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

