UNPKG

conditional

Version:

A preconditions package based on Google's Preconditions library

218 lines (151 loc) 9.07 kB
<!DOCTYPE html> <html> <head> <title>util.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="debug_wrapper.html"> debug_wrapper.coffee </a> <a class="source" href="main.html"> main.coffee </a> <a class="source" href="util.html"> util.coffee </a> <a class="source" href="argument_test.html"> argument_test.coffee </a> <a class="source" href="common_test.html"> common_test.coffee </a> <a class="source" href="contains_test.html"> contains_test.coffee </a> <a class="source" href="defined_test.html"> defined_test.coffee </a> <a class="source" href="equals_test.html"> equals_test.coffee </a> <a class="source" href="not_empty_test.html"> not_empty_test.coffee </a> <a class="source" href="not_null_test.html"> not_null_test.coffee </a> <a class="source" href="number_type_test.html"> number_type_test.coffee </a> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>util.coffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">negate</span> = <span class="hljs-params">(fn)</span> -&gt;</span> (value) -&gt; <span class="hljs-keyword">not</span> fn value <span class="hljs-function"> <span class="hljs-title">isObject</span> = <span class="hljs-params">(value)</span> -&gt;</span> value? <span class="hljs-keyword">and</span> <span class="hljs-keyword">typeof</span> value <span class="hljs-keyword">is</span> <span class="hljs-string">'object'</span> isArray = Array.isArray isNotArray = negate isArray</pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>refer to this snippet from jQuery for explanation: <a href="https://github.com/jquery/jquery/blob/bbdfb/src/core.js#L212">https://github.com/jquery/jquery/blob/bbdfb/src/core.js#L212</a></p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">isNumeric</span> = <span class="hljs-params">(value)</span> -&gt;</span> !isArray(value) &amp;&amp; (value - parseFloat(value) + <span class="hljs-number">1</span>) &gt;= <span class="hljs-number">0</span> <span class="hljs-function"> <span class="hljs-title">isBoolean</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-keyword">typeof</span> value <span class="hljs-keyword">is</span> <span class="hljs-string">'boolean'</span> toString = Object.prototype.toString</pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>refer to underscore.js for explanation <a href="http://underscorejs.org/docs/underscore.html#section-107">http://underscorejs.org/docs/underscore.html#section-107</a></p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">isString</span> = <span class="hljs-params">(value)</span> -&gt;</span> toString.call(value) <span class="hljs-keyword">is</span> <span class="hljs-string">'[object String]'</span> <span class="hljs-function"> <span class="hljs-title">isEmptyString</span> = <span class="hljs-params">(value)</span> -&gt;</span> isString(value) <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> value <span class="hljs-function"> <span class="hljs-title">isUndefined</span> = <span class="hljs-params">(value)</span> -&gt;</span> <span class="hljs-keyword">typeof</span> value <span class="hljs-keyword">is</span> <span class="hljs-string">'undefined'</span> isNotUndefined = negate isUndefined <span class="hljs-function"> <span class="hljs-title">isEqual</span> = <span class="hljs-params">(actual, expected)</span> -&gt;</span> <span class="hljs-keyword">switch</span> <span class="hljs-keyword">when</span> isArray expected <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> isNotArray(actual) <span class="hljs-keyword">or</span> actual.length <span class="hljs-keyword">isnt</span> expected.length <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..expected.length] <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> isEqual(actual[i], expected[i]) <span class="hljs-keyword">and</span> isNotUndefined actual[i] <span class="hljs-keyword">when</span> isObject expected <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">of</span> expected <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">unless</span> isEqual actual[key], value <span class="hljs-keyword">when</span> actual <span class="hljs-keyword">isnt</span> expected <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-literal">true</span> <span class="hljs-function"> <span class="hljs-title">isPrimitive</span> = <span class="hljs-params">(value)</span> -&gt;</span> isNumeric(value) <span class="hljs-keyword">or</span> isBoolean(value) <span class="hljs-function"> <span class="hljs-title">xor</span> = <span class="hljs-params">(operand1, operand2)</span> -&gt;</span> !operand1 != !operand2</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>export all utility methods</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-built_in">module</span>.exports.negate = negate <span class="hljs-built_in">module</span>.exports.isObject = isObject <span class="hljs-built_in">module</span>.exports.isArray = isArray <span class="hljs-built_in">module</span>.exports.isNotArray = isNotArray <span class="hljs-built_in">module</span>.exports.isNumeric = isNumeric <span class="hljs-built_in">module</span>.exports.isString = isString <span class="hljs-built_in">module</span>.exports.isEmptyString = isEmptyString <span class="hljs-built_in">module</span>.exports.isUndefined = isUndefined <span class="hljs-built_in">module</span>.exports.isNotUndefined = isNotUndefined <span class="hljs-built_in">module</span>.exports.isEqual = isEqual <span class="hljs-built_in">module</span>.exports.isPrimitive = isPrimitive <span class="hljs-built_in">module</span>.exports.isNotPrimitive = negate isPrimitive <span class="hljs-built_in">module</span>.exports.xor = xor</pre></div></div> </li> </ul> </div> </body> </html>