diff --git a/.DS_Store b/.DS_Store index 8a3db83..06347e3 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Frogger/README.md b/Frogger/README.md deleted file mode 100644 index 8b15aae..0000000 --- a/Frogger/README.md +++ /dev/null @@ -1 +0,0 @@ -Use this folder for the final project code from the Udacity course diff --git a/Koans/.gitignore b/Koans/.gitignore new file mode 100644 index 0000000..986544f --- /dev/null +++ b/Koans/.gitignore @@ -0,0 +1,2 @@ +*.swp +.idea \ No newline at end of file diff --git a/Koans/README.md b/Koans/README.md new file mode 100644 index 0000000..6a5de4a --- /dev/null +++ b/Koans/README.md @@ -0,0 +1,12 @@ +Update +====== + +JavaScript Koans is an interactive learning environment that uses failing tests to introduce students to aspects of JavaScript in a logical sequence. + +The inspiration for this project comes from the Edgecase Ruby Koans and the book 'Javascript: The Good Parts'. + +Open the file jskoans.htm in your favourite browser and make the tests pass. + +The koans use the [Qunit](http://qunitjs.com/) test syntax and test runner. + +Get started with Ryan Anklam's [Learn JavaScript completely On the Cloud With the JavaScript Koans and Cloud9 IDE](http://blog.bittersweetryan.com/2011/08/learn-some-javascript-completely-on.html) diff --git a/Koans/jskoans.htm b/Koans/jskoans.htm new file mode 100644 index 0000000..f942752 --- /dev/null +++ b/Koans/jskoans.htm @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

JavaScript Koans

+

+

+

To begin, find the file 'topics/about_asserts.js', and complete the tests.

+ +
    +
    test markup, will be hidden
    + + diff --git a/Koans/jskoansbasics.html b/Koans/jskoansbasics.html new file mode 100644 index 0000000..a5e78f0 --- /dev/null +++ b/Koans/jskoansbasics.html @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + +

    JavaScript Koans

    +

    +

    +

    To begin, find the file 'topics/about_asserts.js', and complete the tests.

    + +
      +
      test markup, will be hidden
      + + diff --git a/Koans/license.txt b/Koans/license.txt new file mode 100644 index 0000000..55b9901 --- /dev/null +++ b/Koans/license.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Liam McLennan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Koans/support/jquery-1.4.1.js b/Koans/support/jquery-1.4.1.js new file mode 100644 index 0000000..b5c779c --- /dev/null +++ b/Koans/support/jquery-1.4.1.js @@ -0,0 +1,6111 @@ +/*! + * jQuery JavaScript Library v1.4.1 + * http://jquery.com/ + * + * Copyright 2010, John Resig + * + * Includes Sizzle.js + * http://sizzlejs.com/ + * Copyright 2010, The Dojo Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * Date: Mon Jan 25 19:43:33 2010 -0500 + */ +(function( window, undefined ) { + +// Define a local copy of jQuery +var jQuery = function( selector, context ) { + // The jQuery object is actually just the init constructor 'enhanced' + return new jQuery.fn.init( selector, context ); + }, + + // Map over jQuery in case of overwrite + _jQuery = window.jQuery, + + // Map over the $ in case of overwrite + _$ = window.$, + + // Use the correct document accordingly with window argument (sandbox) + document = window.document, + + // A central reference to the root jQuery(document) + rootjQuery, + + // A simple way to check for HTML strings or ID strings + // (both of which we optimize for) + quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/, + + // Is it a simple selector + isSimple = /^.[^:#\[\.,]*$/, + + // Check if a string has a non-whitespace character in it + rnotwhite = /\S/, + + // Used for trimming whitespace + rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g, + + // Match a standalone tag + rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/, + + // Keep a UserAgent string for use with jQuery.browser + userAgent = navigator.userAgent, + + // For matching the engine and version of the browser + browserMatch, + + // Has the ready events already been bound? + readyBound = false, + + // The functions to execute on DOM ready + readyList = [], + + // The ready event handler + DOMContentLoaded, + + // Save a reference to some core methods + toString = Object.prototype.toString, + hasOwnProperty = Object.prototype.hasOwnProperty, + push = Array.prototype.push, + slice = Array.prototype.slice, + indexOf = Array.prototype.indexOf; + +jQuery.fn = jQuery.prototype = { + init: function( selector, context ) { + var match, elem, ret, doc; + + // Handle $(""), $(null), or $(undefined) + if ( !selector ) { + return this; + } + + // Handle $(DOMElement) + if ( selector.nodeType ) { + this.context = this[0] = selector; + this.length = 1; + return this; + } + + // Handle HTML strings + if ( typeof selector === "string" ) { + // Are we dealing with HTML string or an ID? + match = quickExpr.exec( selector ); + + // Verify a match, and that no context was specified for #id + if ( match && (match[1] || !context) ) { + + // HANDLE: $(html) -> $(array) + if ( match[1] ) { + doc = (context ? context.ownerDocument || context : document); + + // If a single string is passed in and it's a single tag + // just do a createElement and skip the rest + ret = rsingleTag.exec( selector ); + + if ( ret ) { + if ( jQuery.isPlainObject( context ) ) { + selector = [ document.createElement( ret[1] ) ]; + jQuery.fn.attr.call( selector, context, true ); + + } else { + selector = [ doc.createElement( ret[1] ) ]; + } + + } else { + ret = buildFragment( [ match[1] ], [ doc ] ); + selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes; + } + + // HANDLE: $("#id") + } else { + elem = document.getElementById( match[2] ); + + if ( elem ) { + // Handle the case where IE and Opera return items + // by name instead of ID + if ( elem.id !== match[2] ) { + return rootjQuery.find( selector ); + } + + // Otherwise, we inject the element directly into the jQuery object + this.length = 1; + this[0] = elem; + } + + this.context = document; + this.selector = selector; + return this; + } + + // HANDLE: $("TAG") + } else if ( !context && /^\w+$/.test( selector ) ) { + this.selector = selector; + this.context = document; + selector = document.getElementsByTagName( selector ); + + // HANDLE: $(expr, $(...)) + } else if ( !context || context.jquery ) { + return (context || rootjQuery).find( selector ); + + // HANDLE: $(expr, context) + // (which is just equivalent to: $(context).find(expr) + } else { + return jQuery( context ).find( selector ); + } + + // HANDLE: $(function) + // Shortcut for document ready + } else if ( jQuery.isFunction( selector ) ) { + return rootjQuery.ready( selector ); + } + + if (selector.selector !== undefined) { + this.selector = selector.selector; + this.context = selector.context; + } + + return jQuery.isArray( selector ) ? + this.setArray( selector ) : + jQuery.makeArray( selector, this ); + }, + + // Start with an empty selector + selector: "", + + // The current version of jQuery being used + jquery: "1.4.1", + + // The default length of a jQuery object is 0 + length: 0, + + // The number of elements contained in the matched element set + size: function() { + return this.length; + }, + + toArray: function() { + return slice.call( this, 0 ); + }, + + // Get the Nth element in the matched element set OR + // Get the whole matched element set as a clean array + get: function( num ) { + return num == null ? + + // Return a 'clean' array + this.toArray() : + + // Return just the object + ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] ); + }, + + // Take an array of elements and push it onto the stack + // (returning the new matched element set) + pushStack: function( elems, name, selector ) { + // Build a new jQuery matched element set + var ret = jQuery( elems || null ); + + // Add the old object onto the stack (as a reference) + ret.prevObject = this; + + ret.context = this.context; + + if ( name === "find" ) { + ret.selector = this.selector + (this.selector ? " " : "") + selector; + } else if ( name ) { + ret.selector = this.selector + "." + name + "(" + selector + ")"; + } + + // Return the newly-formed element set + return ret; + }, + + // Force the current matched set of elements to become + // the specified array of elements (destroying the stack in the process) + // You should use pushStack() in order to do this, but maintain the stack + setArray: function( elems ) { + // Resetting the length to 0, then using the native Array push + // is a super-fast way to populate an object with array-like properties + this.length = 0; + push.apply( this, elems ); + + return this; + }, + + // Execute a callback for every element in the matched set. + // (You can seed the arguments with an array of args, but this is + // only used internally.) + each: function( callback, args ) { + return jQuery.each( this, callback, args ); + }, + + ready: function( fn ) { + // Attach the listeners + jQuery.bindReady(); + + // If the DOM is already ready + if ( jQuery.isReady ) { + // Execute the function immediately + fn.call( document, jQuery ); + + // Otherwise, remember the function for later + } else if ( readyList ) { + // Add the function to the wait list + readyList.push( fn ); + } + + return this; + }, + + eq: function( i ) { + return i === -1 ? + this.slice( i ) : + this.slice( i, +i + 1 ); + }, + + first: function() { + return this.eq( 0 ); + }, + + last: function() { + return this.eq( -1 ); + }, + + slice: function() { + return this.pushStack( slice.apply( this, arguments ), + "slice", slice.call(arguments).join(",") ); + }, + + map: function( callback ) { + return this.pushStack( jQuery.map(this, function( elem, i ) { + return callback.call( elem, i, elem ); + })); + }, + + end: function() { + return this.prevObject || jQuery(null); + }, + + // For internal use only. + // Behaves like an Array's method, not like a jQuery method. + push: push, + sort: [].sort, + splice: [].splice +}; + +// Give the init function the jQuery prototype for later instantiation +jQuery.fn.init.prototype = jQuery.fn; + +jQuery.extend = jQuery.fn.extend = function() { + // copy reference to target object + var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy; + + // Handle a deep copy situation + if ( typeof target === "boolean" ) { + deep = target; + target = arguments[1] || {}; + // skip the boolean and the target + i = 2; + } + + // Handle case when target is a string or something (possible in deep copy) + if ( typeof target !== "object" && !jQuery.isFunction(target) ) { + target = {}; + } + + // extend jQuery itself if only one argument is passed + if ( length === i ) { + target = this; + --i; + } + + for ( ; i < length; i++ ) { + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) != null ) { + // Extend the base object + for ( name in options ) { + src = target[ name ]; + copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) { + continue; + } + + // Recurse if we're merging object literal values or arrays + if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) { + var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src + : jQuery.isArray(copy) ? [] : {}; + + // Never move original objects, clone them + target[ name ] = jQuery.extend( deep, clone, copy ); + + // Don't bring in undefined values + } else if ( copy !== undefined ) { + target[ name ] = copy; + } + } + } + } + + // Return the modified object + return target; +}; + +jQuery.extend({ + noConflict: function( deep ) { + window.$ = _$; + + if ( deep ) { + window.jQuery = _jQuery; + } + + return jQuery; + }, + + // Is the DOM ready to be used? Set to true once it occurs. + isReady: false, + + // Handle when the DOM is ready + ready: function() { + // Make sure that the DOM is not already loaded + if ( !jQuery.isReady ) { + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( !document.body ) { + return setTimeout( jQuery.ready, 13 ); + } + + // Remember that the DOM is ready + jQuery.isReady = true; + + // If there are functions bound, to execute + if ( readyList ) { + // Execute all of them + var fn, i = 0; + while ( (fn = readyList[ i++ ]) ) { + fn.call( document, jQuery ); + } + + // Reset the list of functions + readyList = null; + } + + // Trigger any bound ready events + if ( jQuery.fn.triggerHandler ) { + jQuery( document ).triggerHandler( "ready" ); + } + } + }, + + bindReady: function() { + if ( readyBound ) { + return; + } + + readyBound = true; + + // Catch cases where $(document).ready() is called after the + // browser event has already occurred. + if ( document.readyState === "complete" ) { + return jQuery.ready(); + } + + // Mozilla, Opera and webkit nightlies currently support this event + if ( document.addEventListener ) { + // Use the handy event callback + document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); + + // A fallback to window.onload, that will always work + window.addEventListener( "load", jQuery.ready, false ); + + // If IE event model is used + } else if ( document.attachEvent ) { + // ensure firing before onload, + // maybe late but safe also for iframes + document.attachEvent("onreadystatechange", DOMContentLoaded); + + // A fallback to window.onload, that will always work + window.attachEvent( "onload", jQuery.ready ); + + // If IE and not a frame + // continually check to see if the document is ready + var toplevel = false; + + try { + toplevel = window.frameElement == null; + } catch(e) {} + + if ( document.documentElement.doScroll && toplevel ) { + doScrollCheck(); + } + } + }, + + // See test/unit/core.js for details concerning isFunction. + // Since version 1.3, DOM methods and functions like alert + // aren't supported. They return false on IE (#2968). + isFunction: function( obj ) { + return toString.call(obj) === "[object Function]"; + }, + + isArray: function( obj ) { + return toString.call(obj) === "[object Array]"; + }, + + isPlainObject: function( obj ) { + // Must be an Object. + // Because of IE, we also have to check the presence of the constructor property. + // Make sure that DOM nodes and window objects don't pass through, as well + if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) { + return false; + } + + // Not own constructor property must be Object + if ( obj.constructor + && !hasOwnProperty.call(obj, "constructor") + && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) { + return false; + } + + // Own properties are enumerated firstly, so to speed up, + // if last one is own, then all properties are own. + + var key; + for ( key in obj ) {} + + return key === undefined || hasOwnProperty.call( obj, key ); + }, + + isEmptyObject: function( obj ) { + for ( var name in obj ) { + return false; + } + return true; + }, + + error: function( msg ) { + throw msg; + }, + + parseJSON: function( data ) { + if ( typeof data !== "string" || !data ) { + return null; + } + + // Make sure the incoming data is actual JSON + // Logic borrowed from http://json.org/json2.js + if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@") + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]") + .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) { + + // Try to use the native JSON parser first + return window.JSON && window.JSON.parse ? + window.JSON.parse( data ) : + (new Function("return " + data))(); + + } else { + jQuery.error( "Invalid JSON: " + data ); + } + }, + + noop: function() {}, + + // Evalulates a script in a global context + globalEval: function( data ) { + if ( data && rnotwhite.test(data) ) { + // Inspired by code by Andrea Giammarchi + // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html + var head = document.getElementsByTagName("head")[0] || document.documentElement, + script = document.createElement("script"); + + script.type = "text/javascript"; + + if ( jQuery.support.scriptEval ) { + script.appendChild( document.createTextNode( data ) ); + } else { + script.text = data; + } + + // Use insertBefore instead of appendChild to circumvent an IE6 bug. + // This arises when a base node is used (#2709). + head.insertBefore( script, head.firstChild ); + head.removeChild( script ); + } + }, + + nodeName: function( elem, name ) { + return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase(); + }, + + // args is for internal usage only + each: function( object, callback, args ) { + var name, i = 0, + length = object.length, + isObj = length === undefined || jQuery.isFunction(object); + + if ( args ) { + if ( isObj ) { + for ( name in object ) { + if ( callback.apply( object[ name ], args ) === false ) { + break; + } + } + } else { + for ( ; i < length; ) { + if ( callback.apply( object[ i++ ], args ) === false ) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if ( isObj ) { + for ( name in object ) { + if ( callback.call( object[ name ], name, object[ name ] ) === false ) { + break; + } + } + } else { + for ( var value = object[0]; + i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {} + } + } + + return object; + }, + + trim: function( text ) { + return (text || "").replace( rtrim, "" ); + }, + + // results is for internal usage only + makeArray: function( array, results ) { + var ret = results || []; + + if ( array != null ) { + // The window, strings (and functions) also have 'length' + // The extra typeof function check is to prevent crashes + // in Safari 2 (See: #3039) + if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) { + push.call( ret, array ); + } else { + jQuery.merge( ret, array ); + } + } + + return ret; + }, + + inArray: function( elem, array ) { + if ( array.indexOf ) { + return array.indexOf( elem ); + } + + for ( var i = 0, length = array.length; i < length; i++ ) { + if ( array[ i ] === elem ) { + return i; + } + } + + return -1; + }, + + merge: function( first, second ) { + var i = first.length, j = 0; + + if ( typeof second.length === "number" ) { + for ( var l = second.length; j < l; j++ ) { + first[ i++ ] = second[ j ]; + } + } else { + while ( second[j] !== undefined ) { + first[ i++ ] = second[ j++ ]; + } + } + + first.length = i; + + return first; + }, + + grep: function( elems, callback, inv ) { + var ret = []; + + // Go through the array, only saving the items + // that pass the validator function + for ( var i = 0, length = elems.length; i < length; i++ ) { + if ( !inv !== !callback( elems[ i ], i ) ) { + ret.push( elems[ i ] ); + } + } + + return ret; + }, + + // arg is for internal usage only + map: function( elems, callback, arg ) { + var ret = [], value; + + // Go through the array, translating each of the items to their + // new value (or values). + for ( var i = 0, length = elems.length; i < length; i++ ) { + value = callback( elems[ i ], i, arg ); + + if ( value != null ) { + ret[ ret.length ] = value; + } + } + + return ret.concat.apply( [], ret ); + }, + + // A global GUID counter for objects + guid: 1, + + proxy: function( fn, proxy, thisObject ) { + if ( arguments.length === 2 ) { + if ( typeof proxy === "string" ) { + thisObject = fn; + fn = thisObject[ proxy ]; + proxy = undefined; + + } else if ( proxy && !jQuery.isFunction( proxy ) ) { + thisObject = proxy; + proxy = undefined; + } + } + + if ( !proxy && fn ) { + proxy = function() { + return fn.apply( thisObject || this, arguments ); + }; + } + + // Set the guid of unique handler to the same of original handler, so it can be removed + if ( fn ) { + proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; + } + + // So proxy can be declared as an argument + return proxy; + }, + + // Use of jQuery.browser is frowned upon. + // More details: http://docs.jquery.com/Utilities/jQuery.browser + uaMatch: function( ua ) { + ua = ua.toLowerCase(); + + var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) || + /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) || + /(msie) ([\w.]+)/.exec( ua ) || + !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) || + []; + + return { browser: match[1] || "", version: match[2] || "0" }; + }, + + browser: {} +}); + +browserMatch = jQuery.uaMatch( userAgent ); +if ( browserMatch.browser ) { + jQuery.browser[ browserMatch.browser ] = true; + jQuery.browser.version = browserMatch.version; +} + +// Deprecated, use jQuery.browser.webkit instead +if ( jQuery.browser.webkit ) { + jQuery.browser.safari = true; +} + +if ( indexOf ) { + jQuery.inArray = function( elem, array ) { + return indexOf.call( array, elem ); + }; +} + +// All jQuery objects should point back to these +rootjQuery = jQuery(document); + +// Cleanup functions for the document ready method +if ( document.addEventListener ) { + DOMContentLoaded = function() { + document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); + jQuery.ready(); + }; + +} else if ( document.attachEvent ) { + DOMContentLoaded = function() { + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( document.readyState === "complete" ) { + document.detachEvent( "onreadystatechange", DOMContentLoaded ); + jQuery.ready(); + } + }; +} + +// The DOM ready check for Internet Explorer +function doScrollCheck() { + if ( jQuery.isReady ) { + return; + } + + try { + // If IE is used, use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + document.documentElement.doScroll("left"); + } catch( error ) { + setTimeout( doScrollCheck, 1 ); + return; + } + + // and execute any waiting functions + jQuery.ready(); +} + +function evalScript( i, elem ) { + if ( elem.src ) { + jQuery.ajax({ + url: elem.src, + async: false, + dataType: "script" + }); + } else { + jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" ); + } + + if ( elem.parentNode ) { + elem.parentNode.removeChild( elem ); + } +} + +// Mutifunctional method to get and set values to a collection +// The value/s can be optionally by executed if its a function +function access( elems, key, value, exec, fn, pass ) { + var length = elems.length; + + // Setting many attributes + if ( typeof key === "object" ) { + for ( var k in key ) { + access( elems, k, key[k], exec, fn, value ); + } + return elems; + } + + // Setting one attribute + if ( value !== undefined ) { + // Optionally, function values get executed if exec is true + exec = !pass && exec && jQuery.isFunction(value); + + for ( var i = 0; i < length; i++ ) { + fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass ); + } + + return elems; + } + + // Getting an attribute + return length ? fn( elems[0], key ) : null; +} + +function now() { + return (new Date).getTime(); +} +(function() { + + jQuery.support = {}; + + var root = document.documentElement, + script = document.createElement("script"), + div = document.createElement("div"), + id = "script" + now(); + + div.style.display = "none"; + div.innerHTML = "
      a"; + + var all = div.getElementsByTagName("*"), + a = div.getElementsByTagName("a")[0]; + + // Can't get basic test support + if ( !all || !all.length || !a ) { + return; + } + + jQuery.support = { + // IE strips leading whitespace when .innerHTML is used + leadingWhitespace: div.firstChild.nodeType === 3, + + // Make sure that tbody elements aren't automatically inserted + // IE will insert them into empty tables + tbody: !div.getElementsByTagName("tbody").length, + + // Make sure that link elements get serialized correctly by innerHTML + // This requires a wrapper element in IE + htmlSerialize: !!div.getElementsByTagName("link").length, + + // Get the style information from getAttribute + // (IE uses .cssText insted) + style: /red/.test( a.getAttribute("style") ), + + // Make sure that URLs aren't manipulated + // (IE normalizes it by default) + hrefNormalized: a.getAttribute("href") === "/a", + + // Make sure that element opacity exists + // (IE uses filter instead) + // Use a regex to work around a WebKit issue. See #5145 + opacity: /^0.55$/.test( a.style.opacity ), + + // Verify style float existence + // (IE uses styleFloat instead of cssFloat) + cssFloat: !!a.style.cssFloat, + + // Make sure that if no value is specified for a checkbox + // that it defaults to "on". + // (WebKit defaults to "" instead) + checkOn: div.getElementsByTagName("input")[0].value === "on", + + // Make sure that a selected-by-default option has a working selected property. + // (WebKit defaults to false instead of true, IE too, if it's in an optgroup) + optSelected: document.createElement("select").appendChild( document.createElement("option") ).selected, + + // Will be defined later + checkClone: false, + scriptEval: false, + noCloneEvent: true, + boxModel: null + }; + + script.type = "text/javascript"; + try { + script.appendChild( document.createTextNode( "window." + id + "=1;" ) ); + } catch(e) {} + + root.insertBefore( script, root.firstChild ); + + // Make sure that the execution of code works by injecting a script + // tag with appendChild/createTextNode + // (IE doesn't support this, fails, and uses .text instead) + if ( window[ id ] ) { + jQuery.support.scriptEval = true; + delete window[ id ]; + } + + root.removeChild( script ); + + if ( div.attachEvent && div.fireEvent ) { + div.attachEvent("onclick", function click() { + // Cloning a node shouldn't copy over any + // bound event handlers (IE does this) + jQuery.support.noCloneEvent = false; + div.detachEvent("onclick", click); + }); + div.cloneNode(true).fireEvent("onclick"); + } + + div = document.createElement("div"); + div.innerHTML = ""; + + var fragment = document.createDocumentFragment(); + fragment.appendChild( div.firstChild ); + + // WebKit doesn't clone checked state correctly in fragments + jQuery.support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked; + + // Figure out if the W3C box model works as expected + // document.body must exist before we can do this + jQuery(function() { + var div = document.createElement("div"); + div.style.width = div.style.paddingLeft = "1px"; + + document.body.appendChild( div ); + jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2; + document.body.removeChild( div ).style.display = 'none'; + div = null; + }); + + // Technique from Juriy Zaytsev + // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/ + var eventSupported = function( eventName ) { + var el = document.createElement("div"); + eventName = "on" + eventName; + + var isSupported = (eventName in el); + if ( !isSupported ) { + el.setAttribute(eventName, "return;"); + isSupported = typeof el[eventName] === "function"; + } + el = null; + + return isSupported; + }; + + jQuery.support.submitBubbles = eventSupported("submit"); + jQuery.support.changeBubbles = eventSupported("change"); + + // release memory in IE + root = script = div = all = a = null; +})(); + +jQuery.props = { + "for": "htmlFor", + "class": "className", + readonly: "readOnly", + maxlength: "maxLength", + cellspacing: "cellSpacing", + rowspan: "rowSpan", + colspan: "colSpan", + tabindex: "tabIndex", + usemap: "useMap", + frameborder: "frameBorder" +}; +var expando = "jQuery" + now(), uuid = 0, windowData = {}; +var emptyObject = {}; + +jQuery.extend({ + cache: {}, + + expando:expando, + + // The following elements throw uncatchable exceptions if you + // attempt to add expando properties to them. + noData: { + "embed": true, + "object": true, + "applet": true + }, + + data: function( elem, name, data ) { + if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) { + return; + } + + elem = elem == window ? + windowData : + elem; + + var id = elem[ expando ], cache = jQuery.cache, thisCache; + + // Handle the case where there's no name immediately + if ( !name && !id ) { + return null; + } + + // Compute a unique ID for the element + if ( !id ) { + id = ++uuid; + } + + // Avoid generating a new cache unless none exists and we + // want to manipulate it. + if ( typeof name === "object" ) { + elem[ expando ] = id; + thisCache = cache[ id ] = jQuery.extend(true, {}, name); + } else if ( cache[ id ] ) { + thisCache = cache[ id ]; + } else if ( typeof data === "undefined" ) { + thisCache = emptyObject; + } else { + thisCache = cache[ id ] = {}; + } + + // Prevent overriding the named cache with undefined values + if ( data !== undefined ) { + elem[ expando ] = id; + thisCache[ name ] = data; + } + + return typeof name === "string" ? thisCache[ name ] : thisCache; + }, + + removeData: function( elem, name ) { + if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) { + return; + } + + elem = elem == window ? + windowData : + elem; + + var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ]; + + // If we want to remove a specific section of the element's data + if ( name ) { + if ( thisCache ) { + // Remove the section of cache data + delete thisCache[ name ]; + + // If we've removed all the data, remove the element's cache + if ( jQuery.isEmptyObject(thisCache) ) { + jQuery.removeData( elem ); + } + } + + // Otherwise, we want to remove all of the element's data + } else { + // Clean up the element expando + try { + delete elem[ expando ]; + } catch( e ) { + // IE has trouble directly removing the expando + // but it's ok with using removeAttribute + if ( elem.removeAttribute ) { + elem.removeAttribute( expando ); + } + } + + // Completely remove the data cache + delete cache[ id ]; + } + } +}); + +jQuery.fn.extend({ + data: function( key, value ) { + if ( typeof key === "undefined" && this.length ) { + return jQuery.data( this[0] ); + + } else if ( typeof key === "object" ) { + return this.each(function() { + jQuery.data( this, key ); + }); + } + + var parts = key.split("."); + parts[1] = parts[1] ? "." + parts[1] : ""; + + if ( value === undefined ) { + var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]); + + if ( data === undefined && this.length ) { + data = jQuery.data( this[0], key ); + } + return data === undefined && parts[1] ? + this.data( parts[0] ) : + data; + } else { + return this.trigger("setData" + parts[1] + "!", [parts[0], value]).each(function() { + jQuery.data( this, key, value ); + }); + } + }, + + removeData: function( key ) { + return this.each(function() { + jQuery.removeData( this, key ); + }); + } +}); +jQuery.extend({ + queue: function( elem, type, data ) { + if ( !elem ) { + return; + } + + type = (type || "fx") + "queue"; + var q = jQuery.data( elem, type ); + + // Speed up dequeue by getting out quickly if this is just a lookup + if ( !data ) { + return q || []; + } + + if ( !q || jQuery.isArray(data) ) { + q = jQuery.data( elem, type, jQuery.makeArray(data) ); + + } else { + q.push( data ); + } + + return q; + }, + + dequeue: function( elem, type ) { + type = type || "fx"; + + var queue = jQuery.queue( elem, type ), fn = queue.shift(); + + // If the fx queue is dequeued, always remove the progress sentinel + if ( fn === "inprogress" ) { + fn = queue.shift(); + } + + if ( fn ) { + // Add a progress sentinel to prevent the fx queue from being + // automatically dequeued + if ( type === "fx" ) { + queue.unshift("inprogress"); + } + + fn.call(elem, function() { + jQuery.dequeue(elem, type); + }); + } + } +}); + +jQuery.fn.extend({ + queue: function( type, data ) { + if ( typeof type !== "string" ) { + data = type; + type = "fx"; + } + + if ( data === undefined ) { + return jQuery.queue( this[0], type ); + } + return this.each(function( i, elem ) { + var queue = jQuery.queue( this, type, data ); + + if ( type === "fx" && queue[0] !== "inprogress" ) { + jQuery.dequeue( this, type ); + } + }); + }, + dequeue: function( type ) { + return this.each(function() { + jQuery.dequeue( this, type ); + }); + }, + + // Based off of the plugin by Clint Helfers, with permission. + // http://blindsignals.com/index.php/2009/07/jquery-delay/ + delay: function( time, type ) { + time = jQuery.fx ? jQuery.fx.speeds[time] || time : time; + type = type || "fx"; + + return this.queue( type, function() { + var elem = this; + setTimeout(function() { + jQuery.dequeue( elem, type ); + }, time ); + }); + }, + + clearQueue: function( type ) { + return this.queue( type || "fx", [] ); + } +}); +var rclass = /[\n\t]/g, + rspace = /\s+/, + rreturn = /\r/g, + rspecialurl = /href|src|style/, + rtype = /(button|input)/i, + rfocusable = /(button|input|object|select|textarea)/i, + rclickable = /^(a|area)$/i, + rradiocheck = /radio|checkbox/; + +jQuery.fn.extend({ + attr: function( name, value ) { + return access( this, name, value, true, jQuery.attr ); + }, + + removeAttr: function( name, fn ) { + return this.each(function(){ + jQuery.attr( this, name, "" ); + if ( this.nodeType === 1 ) { + this.removeAttribute( name ); + } + }); + }, + + addClass: function( value ) { + if ( jQuery.isFunction(value) ) { + return this.each(function(i) { + var self = jQuery(this); + self.addClass( value.call(this, i, self.attr("class")) ); + }); + } + + if ( value && typeof value === "string" ) { + var classNames = (value || "").split( rspace ); + + for ( var i = 0, l = this.length; i < l; i++ ) { + var elem = this[i]; + + if ( elem.nodeType === 1 ) { + if ( !elem.className ) { + elem.className = value; + + } else { + var className = " " + elem.className + " "; + for ( var c = 0, cl = classNames.length; c < cl; c++ ) { + if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) { + elem.className += " " + classNames[c]; + } + } + } + } + } + } + + return this; + }, + + removeClass: function( value ) { + if ( jQuery.isFunction(value) ) { + return this.each(function(i) { + var self = jQuery(this); + self.removeClass( value.call(this, i, self.attr("class")) ); + }); + } + + if ( (value && typeof value === "string") || value === undefined ) { + var classNames = (value || "").split(rspace); + + for ( var i = 0, l = this.length; i < l; i++ ) { + var elem = this[i]; + + if ( elem.nodeType === 1 && elem.className ) { + if ( value ) { + var className = (" " + elem.className + " ").replace(rclass, " "); + for ( var c = 0, cl = classNames.length; c < cl; c++ ) { + className = className.replace(" " + classNames[c] + " ", " "); + } + elem.className = className.substring(1, className.length - 1); + + } else { + elem.className = ""; + } + } + } + } + + return this; + }, + + toggleClass: function( value, stateVal ) { + var type = typeof value, isBool = typeof stateVal === "boolean"; + + if ( jQuery.isFunction( value ) ) { + return this.each(function(i) { + var self = jQuery(this); + self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal ); + }); + } + + return this.each(function() { + if ( type === "string" ) { + // toggle individual class names + var className, i = 0, self = jQuery(this), + state = stateVal, + classNames = value.split( rspace ); + + while ( (className = classNames[ i++ ]) ) { + // check each className given, space seperated list + state = isBool ? state : !self.hasClass( className ); + self[ state ? "addClass" : "removeClass" ]( className ); + } + + } else if ( type === "undefined" || type === "boolean" ) { + if ( this.className ) { + // store className if set + jQuery.data( this, "__className__", this.className ); + } + + // toggle whole className + this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || ""; + } + }); + }, + + hasClass: function( selector ) { + var className = " " + selector + " "; + for ( var i = 0, l = this.length; i < l; i++ ) { + if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) { + return true; + } + } + + return false; + }, + + val: function( value ) { + if ( value === undefined ) { + var elem = this[0]; + + if ( elem ) { + if ( jQuery.nodeName( elem, "option" ) ) { + return (elem.attributes.value || {}).specified ? elem.value : elem.text; + } + + // We need to handle select boxes special + if ( jQuery.nodeName( elem, "select" ) ) { + var index = elem.selectedIndex, + values = [], + options = elem.options, + one = elem.type === "select-one"; + + // Nothing was selected + if ( index < 0 ) { + return null; + } + + // Loop through all the selected options + for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) { + var option = options[ i ]; + + if ( option.selected ) { + // Get the specifc value for the option + value = jQuery(option).val(); + + // We don't need an array for one selects + if ( one ) { + return value; + } + + // Multi-Selects return an array + values.push( value ); + } + } + + return values; + } + + // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified + if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) { + return elem.getAttribute("value") === null ? "on" : elem.value; + } + + + // Everything else, we just grab the value + return (elem.value || "").replace(rreturn, ""); + + } + + return undefined; + } + + var isFunction = jQuery.isFunction(value); + + return this.each(function(i) { + var self = jQuery(this), val = value; + + if ( this.nodeType !== 1 ) { + return; + } + + if ( isFunction ) { + val = value.call(this, i, self.val()); + } + + // Typecast each time if the value is a Function and the appended + // value is therefore different each time. + if ( typeof val === "number" ) { + val += ""; + } + + if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) { + this.checked = jQuery.inArray( self.val(), val ) >= 0; + + } else if ( jQuery.nodeName( this, "select" ) ) { + var values = jQuery.makeArray(val); + + jQuery( "option", this ).each(function() { + this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0; + }); + + if ( !values.length ) { + this.selectedIndex = -1; + } + + } else { + this.value = val; + } + }); + } +}); + +jQuery.extend({ + attrFn: { + val: true, + css: true, + html: true, + text: true, + data: true, + width: true, + height: true, + offset: true + }, + + attr: function( elem, name, value, pass ) { + // don't set attributes on text and comment nodes + if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { + return undefined; + } + + if ( pass && name in jQuery.attrFn ) { + return jQuery(elem)[name](value); + } + + var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ), + // Whether we are setting (or getting) + set = value !== undefined; + + // Try to normalize/fix the name + name = notxml && jQuery.props[ name ] || name; + + // Only do all the following if this is a node (faster for style) + if ( elem.nodeType === 1 ) { + // These attributes require special treatment + var special = rspecialurl.test( name ); + + // Safari mis-reports the default selected property of an option + // Accessing the parent's selectedIndex property fixes it + if ( name === "selected" && !jQuery.support.optSelected ) { + var parent = elem.parentNode; + if ( parent ) { + parent.selectedIndex; + + // Make sure that it also works with optgroups, see #5701 + if ( parent.parentNode ) { + parent.parentNode.selectedIndex; + } + } + } + + // If applicable, access the attribute via the DOM 0 way + if ( name in elem && notxml && !special ) { + if ( set ) { + // We can't allow the type property to be changed (since it causes problems in IE) + if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) { + jQuery.error( "type property can't be changed" ); + } + + elem[ name ] = value; + } + + // browsers index elements by id/name on forms, give priority to attributes. + if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) { + return elem.getAttributeNode( name ).nodeValue; + } + + // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set + // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/ + if ( name === "tabIndex" ) { + var attributeNode = elem.getAttributeNode( "tabIndex" ); + + return attributeNode && attributeNode.specified ? + attributeNode.value : + rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ? + 0 : + undefined; + } + + return elem[ name ]; + } + + if ( !jQuery.support.style && notxml && name === "style" ) { + if ( set ) { + elem.style.cssText = "" + value; + } + + return elem.style.cssText; + } + + if ( set ) { + // convert the value to a string (all browsers do this but IE) see #1070 + elem.setAttribute( name, "" + value ); + } + + var attr = !jQuery.support.hrefNormalized && notxml && special ? + // Some attributes require a special call on IE + elem.getAttribute( name, 2 ) : + elem.getAttribute( name ); + + // Non-existent attributes return null, we normalize to undefined + return attr === null ? undefined : attr; + } + + // elem is actually elem.style ... set the style + // Using attr for specific style information is now deprecated. Use style insead. + return jQuery.style( elem, name, value ); + } +}); +var fcleanup = function( nm ) { + return nm.replace(/[^\w\s\.\|`]/g, function( ch ) { + return "\\" + ch; + }); +}; + +/* + * A number of helper functions used for managing events. + * Many of the ideas behind this code originated from + * Dean Edwards' addEvent library. + */ +jQuery.event = { + + // Bind an event to an element + // Original by Dean Edwards + add: function( elem, types, handler, data ) { + if ( elem.nodeType === 3 || elem.nodeType === 8 ) { + return; + } + + // For whatever reason, IE has trouble passing the window object + // around, causing it to be cloned in the process + if ( elem.setInterval && ( elem !== window && !elem.frameElement ) ) { + elem = window; + } + + // Make sure that the function being executed has a unique ID + if ( !handler.guid ) { + handler.guid = jQuery.guid++; + } + + // if data is passed, bind to handler + if ( data !== undefined ) { + // Create temporary function pointer to original handler + var fn = handler; + + // Create unique handler function, wrapped around original handler + handler = jQuery.proxy( fn ); + + // Store data in unique handler + handler.data = data; + } + + // Init the element's event structure + var events = jQuery.data( elem, "events" ) || jQuery.data( elem, "events", {} ), + handle = jQuery.data( elem, "handle" ), eventHandle; + + if ( !handle ) { + eventHandle = function() { + // Handle the second event of a trigger and when + // an event is called after a page has unloaded + return typeof jQuery !== "undefined" && !jQuery.event.triggered ? + jQuery.event.handle.apply( eventHandle.elem, arguments ) : + undefined; + }; + + handle = jQuery.data( elem, "handle", eventHandle ); + } + + // If no handle is found then we must be trying to bind to one of the + // banned noData elements + if ( !handle ) { + return; + } + + // Add elem as a property of the handle function + // This is to prevent a memory leak with non-native + // event in IE. + handle.elem = elem; + + // Handle multiple events separated by a space + // jQuery(...).bind("mouseover mouseout", fn); + types = types.split( /\s+/ ); + + var type, i = 0; + + while ( (type = types[ i++ ]) ) { + // Namespaced event handlers + var namespaces = type.split("."); + type = namespaces.shift(); + + if ( i > 1 ) { + handler = jQuery.proxy( handler ); + + if ( data !== undefined ) { + handler.data = data; + } + } + + handler.type = namespaces.slice(0).sort().join("."); + + // Get the current list of functions bound to this event + var handlers = events[ type ], + special = this.special[ type ] || {}; + + // Init the event handler queue + if ( !handlers ) { + handlers = events[ type ] = {}; + + // Check for a special event handler + // Only use addEventListener/attachEvent if the special + // events handler returns false + if ( !special.setup || special.setup.call( elem, data, namespaces, handler) === false ) { + // Bind the global event handler to the element + if ( elem.addEventListener ) { + elem.addEventListener( type, handle, false ); + } else if ( elem.attachEvent ) { + elem.attachEvent( "on" + type, handle ); + } + } + } + + if ( special.add ) { + var modifiedHandler = special.add.call( elem, handler, data, namespaces, handlers ); + if ( modifiedHandler && jQuery.isFunction( modifiedHandler ) ) { + modifiedHandler.guid = modifiedHandler.guid || handler.guid; + modifiedHandler.data = modifiedHandler.data || handler.data; + modifiedHandler.type = modifiedHandler.type || handler.type; + handler = modifiedHandler; + } + } + + // Add the function to the element's handler list + handlers[ handler.guid ] = handler; + + // Keep track of which events have been used, for global triggering + this.global[ type ] = true; + } + + // Nullify elem to prevent memory leaks in IE + elem = null; + }, + + global: {}, + + // Detach an event or set of events from an element + remove: function( elem, types, handler ) { + // don't do events on text and comment nodes + if ( elem.nodeType === 3 || elem.nodeType === 8 ) { + return; + } + + var events = jQuery.data( elem, "events" ), ret, type, fn; + + if ( events ) { + // Unbind all events for the element + if ( types === undefined || (typeof types === "string" && types.charAt(0) === ".") ) { + for ( type in events ) { + this.remove( elem, type + (types || "") ); + } + } else { + // types is actually an event object here + if ( types.type ) { + handler = types.handler; + types = types.type; + } + + // Handle multiple events separated by a space + // jQuery(...).unbind("mouseover mouseout", fn); + types = types.split(/\s+/); + var i = 0; + while ( (type = types[ i++ ]) ) { + // Namespaced event handlers + var namespaces = type.split("."); + type = namespaces.shift(); + var all = !namespaces.length, + cleaned = jQuery.map( namespaces.slice(0).sort(), fcleanup ), + namespace = new RegExp("(^|\\.)" + cleaned.join("\\.(?:.*\\.)?") + "(\\.|$)"), + special = this.special[ type ] || {}; + + if ( events[ type ] ) { + // remove the given handler for the given type + if ( handler ) { + fn = events[ type ][ handler.guid ]; + delete events[ type ][ handler.guid ]; + + // remove all handlers for the given type + } else { + for ( var handle in events[ type ] ) { + // Handle the removal of namespaced events + if ( all || namespace.test( events[ type ][ handle ].type ) ) { + delete events[ type ][ handle ]; + } + } + } + + if ( special.remove ) { + special.remove.call( elem, namespaces, fn); + } + + // remove generic event handler if no more handlers exist + for ( ret in events[ type ] ) { + break; + } + if ( !ret ) { + if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) { + if ( elem.removeEventListener ) { + elem.removeEventListener( type, jQuery.data( elem, "handle" ), false ); + } else if ( elem.detachEvent ) { + elem.detachEvent( "on" + type, jQuery.data( elem, "handle" ) ); + } + } + ret = null; + delete events[ type ]; + } + } + } + } + + // Remove the expando if it's no longer used + for ( ret in events ) { + break; + } + if ( !ret ) { + var handle = jQuery.data( elem, "handle" ); + if ( handle ) { + handle.elem = null; + } + jQuery.removeData( elem, "events" ); + jQuery.removeData( elem, "handle" ); + } + } + }, + + // bubbling is internal + trigger: function( event, data, elem /*, bubbling */ ) { + // Event object or event type + var type = event.type || event, + bubbling = arguments[3]; + + if ( !bubbling ) { + event = typeof event === "object" ? + // jQuery.Event object + event[expando] ? event : + // Object literal + jQuery.extend( jQuery.Event(type), event ) : + // Just the event type (string) + jQuery.Event(type); + + if ( type.indexOf("!") >= 0 ) { + event.type = type = type.slice(0, -1); + event.exclusive = true; + } + + // Handle a global trigger + if ( !elem ) { + // Don't bubble custom events when global (to avoid too much overhead) + event.stopPropagation(); + + // Only trigger if we've ever bound an event for it + if ( this.global[ type ] ) { + jQuery.each( jQuery.cache, function() { + if ( this.events && this.events[type] ) { + jQuery.event.trigger( event, data, this.handle.elem ); + } + }); + } + } + + // Handle triggering a single element + + // don't do events on text and comment nodes + if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) { + return undefined; + } + + // Clean up in case it is reused + event.result = undefined; + event.target = elem; + + // Clone the incoming data, if any + data = jQuery.makeArray( data ); + data.unshift( event ); + } + + event.currentTarget = elem; + + // Trigger the event, it is assumed that "handle" is a function + var handle = jQuery.data( elem, "handle" ); + if ( handle ) { + handle.apply( elem, data ); + } + + var parent = elem.parentNode || elem.ownerDocument; + + // Trigger an inline bound script + try { + if ( !(elem && elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()]) ) { + if ( elem[ "on" + type ] && elem[ "on" + type ].apply( elem, data ) === false ) { + event.result = false; + } + } + + // prevent IE from throwing an error for some elements with some event types, see #3533 + } catch (e) {} + + if ( !event.isPropagationStopped() && parent ) { + jQuery.event.trigger( event, data, parent, true ); + + } else if ( !event.isDefaultPrevented() ) { + var target = event.target, old, + isClick = jQuery.nodeName(target, "a") && type === "click"; + + if ( !isClick && !(target && target.nodeName && jQuery.noData[target.nodeName.toLowerCase()]) ) { + try { + if ( target[ type ] ) { + // Make sure that we don't accidentally re-trigger the onFOO events + old = target[ "on" + type ]; + + if ( old ) { + target[ "on" + type ] = null; + } + + this.triggered = true; + target[ type ](); + } + + // prevent IE from throwing an error for some elements with some event types, see #3533 + } catch (e) {} + + if ( old ) { + target[ "on" + type ] = old; + } + + this.triggered = false; + } + } + }, + + handle: function( event ) { + // returned undefined or false + var all, handlers; + + event = arguments[0] = jQuery.event.fix( event || window.event ); + event.currentTarget = this; + + // Namespaced event handlers + var namespaces = event.type.split("."); + event.type = namespaces.shift(); + + // Cache this now, all = true means, any handler + all = !namespaces.length && !event.exclusive; + + var namespace = new RegExp("(^|\\.)" + namespaces.slice(0).sort().join("\\.(?:.*\\.)?") + "(\\.|$)"); + + handlers = ( jQuery.data(this, "events") || {} )[ event.type ]; + + for ( var j in handlers ) { + var handler = handlers[ j ]; + + // Filter the functions by class + if ( all || namespace.test(handler.type) ) { + // Pass in a reference to the handler function itself + // So that we can later remove it + event.handler = handler; + event.data = handler.data; + + var ret = handler.apply( this, arguments ); + + if ( ret !== undefined ) { + event.result = ret; + if ( ret === false ) { + event.preventDefault(); + event.stopPropagation(); + } + } + + if ( event.isImmediatePropagationStopped() ) { + break; + } + + } + } + + return event.result; + }, + + props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "), + + fix: function( event ) { + if ( event[ expando ] ) { + return event; + } + + // store a copy of the original event object + // and "clone" to set read-only properties + var originalEvent = event; + event = jQuery.Event( originalEvent ); + + for ( var i = this.props.length, prop; i; ) { + prop = this.props[ --i ]; + event[ prop ] = originalEvent[ prop ]; + } + + // Fix target property, if necessary + if ( !event.target ) { + event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either + } + + // check if target is a textnode (safari) + if ( event.target.nodeType === 3 ) { + event.target = event.target.parentNode; + } + + // Add relatedTarget, if necessary + if ( !event.relatedTarget && event.fromElement ) { + event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement; + } + + // Calculate pageX/Y if missing and clientX/Y available + if ( event.pageX == null && event.clientX != null ) { + var doc = document.documentElement, body = document.body; + event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); + event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0); + } + + // Add which for key events + if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) ) { + event.which = event.charCode || event.keyCode; + } + + // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs) + if ( !event.metaKey && event.ctrlKey ) { + event.metaKey = event.ctrlKey; + } + + // Add which for click: 1 === left; 2 === middle; 3 === right + // Note: button is not normalized, so don't use it + if ( !event.which && event.button !== undefined ) { + event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); + } + + return event; + }, + + // Deprecated, use jQuery.guid instead + guid: 1E8, + + // Deprecated, use jQuery.proxy instead + proxy: jQuery.proxy, + + special: { + ready: { + // Make sure the ready event is setup + setup: jQuery.bindReady, + teardown: jQuery.noop + }, + + live: { + add: function( proxy, data, namespaces, live ) { + jQuery.extend( proxy, data || {} ); + + proxy.guid += data.selector + data.live; + data.liveProxy = proxy; + + jQuery.event.add( this, data.live, liveHandler, data ); + + }, + + remove: function( namespaces ) { + if ( namespaces.length ) { + var remove = 0, name = new RegExp("(^|\\.)" + namespaces[0] + "(\\.|$)"); + + jQuery.each( (jQuery.data(this, "events").live || {}), function() { + if ( name.test(this.type) ) { + remove++; + } + }); + + if ( remove < 1 ) { + jQuery.event.remove( this, namespaces[0], liveHandler ); + } + } + }, + special: {} + }, + beforeunload: { + setup: function( data, namespaces, fn ) { + // We only want to do this special case on windows + if ( this.setInterval ) { + this.onbeforeunload = fn; + } + + return false; + }, + teardown: function( namespaces, fn ) { + if ( this.onbeforeunload === fn ) { + this.onbeforeunload = null; + } + } + } + } +}; + +jQuery.Event = function( src ) { + // Allow instantiation without the 'new' keyword + if ( !this.preventDefault ) { + return new jQuery.Event( src ); + } + + // Event object + if ( src && src.type ) { + this.originalEvent = src; + this.type = src.type; + // Event type + } else { + this.type = src; + } + + // timeStamp is buggy for some events on Firefox(#3843) + // So we won't rely on the native value + this.timeStamp = now(); + + // Mark it as fixed + this[ expando ] = true; +}; + +function returnFalse() { + return false; +} +function returnTrue() { + return true; +} + +// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding +// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html +jQuery.Event.prototype = { + preventDefault: function() { + this.isDefaultPrevented = returnTrue; + + var e = this.originalEvent; + if ( !e ) { + return; + } + + // if preventDefault exists run it on the original event + if ( e.preventDefault ) { + e.preventDefault(); + } + // otherwise set the returnValue property of the original event to false (IE) + e.returnValue = false; + }, + stopPropagation: function() { + this.isPropagationStopped = returnTrue; + + var e = this.originalEvent; + if ( !e ) { + return; + } + // if stopPropagation exists run it on the original event + if ( e.stopPropagation ) { + e.stopPropagation(); + } + // otherwise set the cancelBubble property of the original event to true (IE) + e.cancelBubble = true; + }, + stopImmediatePropagation: function() { + this.isImmediatePropagationStopped = returnTrue; + this.stopPropagation(); + }, + isDefaultPrevented: returnFalse, + isPropagationStopped: returnFalse, + isImmediatePropagationStopped: returnFalse +}; + +// Checks if an event happened on an element within another element +// Used in jQuery.event.special.mouseenter and mouseleave handlers +var withinElement = function( event ) { + // Check if mouse(over|out) are still within the same parent element + var parent = event.relatedTarget; + + // Traverse up the tree + while ( parent && parent !== this ) { + // Firefox sometimes assigns relatedTarget a XUL element + // which we cannot access the parentNode property of + try { + parent = parent.parentNode; + + // assuming we've left the element since we most likely mousedover a xul element + } catch(e) { + break; + } + } + + if ( parent !== this ) { + // set the correct event type + event.type = event.data; + + // handle event if we actually just moused on to a non sub-element + jQuery.event.handle.apply( this, arguments ); + } + +}, + +// In case of event delegation, we only need to rename the event.type, +// liveHandler will take care of the rest. +delegate = function( event ) { + event.type = event.data; + jQuery.event.handle.apply( this, arguments ); +}; + +// Create mouseenter and mouseleave events +jQuery.each({ + mouseenter: "mouseover", + mouseleave: "mouseout" +}, function( orig, fix ) { + jQuery.event.special[ orig ] = { + setup: function( data ) { + jQuery.event.add( this, fix, data && data.selector ? delegate : withinElement, orig ); + }, + teardown: function( data ) { + jQuery.event.remove( this, fix, data && data.selector ? delegate : withinElement ); + } + }; +}); + +// submit delegation +if ( !jQuery.support.submitBubbles ) { + +jQuery.event.special.submit = { + setup: function( data, namespaces, fn ) { + if ( this.nodeName.toLowerCase() !== "form" ) { + jQuery.event.add(this, "click.specialSubmit." + fn.guid, function( e ) { + var elem = e.target, type = elem.type; + + if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) { + return trigger( "submit", this, arguments ); + } + }); + + jQuery.event.add(this, "keypress.specialSubmit." + fn.guid, function( e ) { + var elem = e.target, type = elem.type; + + if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) { + return trigger( "submit", this, arguments ); + } + }); + + } else { + return false; + } + }, + + remove: function( namespaces, fn ) { + jQuery.event.remove( this, "click.specialSubmit" + (fn ? "."+fn.guid : "") ); + jQuery.event.remove( this, "keypress.specialSubmit" + (fn ? "."+fn.guid : "") ); + } +}; + +} + +// change delegation, happens here so we have bind. +if ( !jQuery.support.changeBubbles ) { + +var formElems = /textarea|input|select/i; + +function getVal( elem ) { + var type = elem.type, val = elem.value; + + if ( type === "radio" || type === "checkbox" ) { + val = elem.checked; + + } else if ( type === "select-multiple" ) { + val = elem.selectedIndex > -1 ? + jQuery.map( elem.options, function( elem ) { + return elem.selected; + }).join("-") : + ""; + + } else if ( elem.nodeName.toLowerCase() === "select" ) { + val = elem.selectedIndex; + } + + return val; +} + +function testChange( e ) { + var elem = e.target, data, val; + + if ( !formElems.test( elem.nodeName ) || elem.readOnly ) { + return; + } + + data = jQuery.data( elem, "_change_data" ); + val = getVal(elem); + + // the current data will be also retrieved by beforeactivate + if ( e.type !== "focusout" || elem.type !== "radio" ) { + jQuery.data( elem, "_change_data", val ); + } + + if ( data === undefined || val === data ) { + return; + } + + if ( data != null || val ) { + e.type = "change"; + return jQuery.event.trigger( e, arguments[1], elem ); + } +} + +jQuery.event.special.change = { + filters: { + focusout: testChange, + + click: function( e ) { + var elem = e.target, type = elem.type; + + if ( type === "radio" || type === "checkbox" || elem.nodeName.toLowerCase() === "select" ) { + return testChange.call( this, e ); + } + }, + + // Change has to be called before submit + // Keydown will be called before keypress, which is used in submit-event delegation + keydown: function( e ) { + var elem = e.target, type = elem.type; + + if ( (e.keyCode === 13 && elem.nodeName.toLowerCase() !== "textarea") || + (e.keyCode === 32 && (type === "checkbox" || type === "radio")) || + type === "select-multiple" ) { + return testChange.call( this, e ); + } + }, + + // Beforeactivate happens also before the previous element is blurred + // with this event you can't trigger a change event, but you can store + // information/focus[in] is not needed anymore + beforeactivate: function( e ) { + var elem = e.target; + + if ( elem.nodeName.toLowerCase() === "input" && elem.type === "radio" ) { + jQuery.data( elem, "_change_data", getVal(elem) ); + } + } + }, + setup: function( data, namespaces, fn ) { + for ( var type in changeFilters ) { + jQuery.event.add( this, type + ".specialChange." + fn.guid, changeFilters[type] ); + } + + return formElems.test( this.nodeName ); + }, + remove: function( namespaces, fn ) { + for ( var type in changeFilters ) { + jQuery.event.remove( this, type + ".specialChange" + (fn ? "."+fn.guid : ""), changeFilters[type] ); + } + + return formElems.test( this.nodeName ); + } +}; + +var changeFilters = jQuery.event.special.change.filters; + +} + +function trigger( type, elem, args ) { + args[0].type = type; + return jQuery.event.handle.apply( elem, args ); +} + +// Create "bubbling" focus and blur events +if ( document.addEventListener ) { + jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { + jQuery.event.special[ fix ] = { + setup: function() { + this.addEventListener( orig, handler, true ); + }, + teardown: function() { + this.removeEventListener( orig, handler, true ); + } + }; + + function handler( e ) { + e = jQuery.event.fix( e ); + e.type = fix; + return jQuery.event.handle.call( this, e ); + } + }); +} + +jQuery.each(["bind", "one"], function( i, name ) { + jQuery.fn[ name ] = function( type, data, fn ) { + // Handle object literals + if ( typeof type === "object" ) { + for ( var key in type ) { + this[ name ](key, data, type[key], fn); + } + return this; + } + + if ( jQuery.isFunction( data ) ) { + fn = data; + data = undefined; + } + + var handler = name === "one" ? jQuery.proxy( fn, function( event ) { + jQuery( this ).unbind( event, handler ); + return fn.apply( this, arguments ); + }) : fn; + + return type === "unload" && name !== "one" ? + this.one( type, data, fn ) : + this.each(function() { + jQuery.event.add( this, type, handler, data ); + }); + }; +}); + +jQuery.fn.extend({ + unbind: function( type, fn ) { + // Handle object literals + if ( typeof type === "object" && !type.preventDefault ) { + for ( var key in type ) { + this.unbind(key, type[key]); + } + return this; + } + + return this.each(function() { + jQuery.event.remove( this, type, fn ); + }); + }, + trigger: function( type, data ) { + return this.each(function() { + jQuery.event.trigger( type, data, this ); + }); + }, + + triggerHandler: function( type, data ) { + if ( this[0] ) { + var event = jQuery.Event( type ); + event.preventDefault(); + event.stopPropagation(); + jQuery.event.trigger( event, data, this[0] ); + return event.result; + } + }, + + toggle: function( fn ) { + // Save reference to arguments for access in closure + var args = arguments, i = 1; + + // link all the functions, so any of them can unbind this click handler + while ( i < args.length ) { + jQuery.proxy( fn, args[ i++ ] ); + } + + return this.click( jQuery.proxy( fn, function( event ) { + // Figure out which function to execute + var lastToggle = ( jQuery.data( this, "lastToggle" + fn.guid ) || 0 ) % i; + jQuery.data( this, "lastToggle" + fn.guid, lastToggle + 1 ); + + // Make sure that clicks stop + event.preventDefault(); + + // and execute the function + return args[ lastToggle ].apply( this, arguments ) || false; + })); + }, + + hover: function( fnOver, fnOut ) { + return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver ); + } +}); + +jQuery.each(["live", "die"], function( i, name ) { + jQuery.fn[ name ] = function( types, data, fn ) { + var type, i = 0; + + if ( jQuery.isFunction( data ) ) { + fn = data; + data = undefined; + } + + types = (types || "").split( /\s+/ ); + + while ( (type = types[ i++ ]) != null ) { + type = type === "focus" ? "focusin" : // focus --> focusin + type === "blur" ? "focusout" : // blur --> focusout + type === "hover" ? types.push("mouseleave") && "mouseenter" : // hover support + type; + + if ( name === "live" ) { + // bind live handler + jQuery( this.context ).bind( liveConvert( type, this.selector ), { + data: data, selector: this.selector, live: type + }, fn ); + + } else { + // unbind live handler + jQuery( this.context ).unbind( liveConvert( type, this.selector ), fn ? { guid: fn.guid + this.selector + type } : null ); + } + } + + return this; + } +}); + +function liveHandler( event ) { + var stop, elems = [], selectors = [], args = arguments, + related, match, fn, elem, j, i, l, data, + live = jQuery.extend({}, jQuery.data( this, "events" ).live); + + // Make sure we avoid non-left-click bubbling in Firefox (#3861) + if ( event.button && event.type === "click" ) { + return; + } + + for ( j in live ) { + fn = live[j]; + if ( fn.live === event.type || + fn.altLive && jQuery.inArray(event.type, fn.altLive) > -1 ) { + + data = fn.data; + if ( !(data.beforeFilter && data.beforeFilter[event.type] && + !data.beforeFilter[event.type](event)) ) { + selectors.push( fn.selector ); + } + } else { + delete live[j]; + } + } + + match = jQuery( event.target ).closest( selectors, event.currentTarget ); + + for ( i = 0, l = match.length; i < l; i++ ) { + for ( j in live ) { + fn = live[j]; + elem = match[i].elem; + related = null; + + if ( match[i].selector === fn.selector ) { + // Those two events require additional checking + if ( fn.live === "mouseenter" || fn.live === "mouseleave" ) { + related = jQuery( event.relatedTarget ).closest( fn.selector )[0]; + } + + if ( !related || related !== elem ) { + elems.push({ elem: elem, fn: fn }); + } + } + } + } + + for ( i = 0, l = elems.length; i < l; i++ ) { + match = elems[i]; + event.currentTarget = match.elem; + event.data = match.fn.data; + if ( match.fn.apply( match.elem, args ) === false ) { + stop = false; + break; + } + } + + return stop; +} + +function liveConvert( type, selector ) { + return "live." + (type ? type + "." : "") + selector.replace(/\./g, "`").replace(/ /g, "&"); +} + +jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " + + "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " + + "change select submit keydown keypress keyup error").split(" "), function( i, name ) { + + // Handle event binding + jQuery.fn[ name ] = function( fn ) { + return fn ? this.bind( name, fn ) : this.trigger( name ); + }; + + if ( jQuery.attrFn ) { + jQuery.attrFn[ name ] = true; + } +}); + +// Prevent memory leaks in IE +// Window isn't included so as not to unbind existing unload events +// More info: +// - http://isaacschlueter.com/2006/10/msie-memory-leaks/ +if ( window.attachEvent && !window.addEventListener ) { + window.attachEvent("onunload", function() { + for ( var id in jQuery.cache ) { + if ( jQuery.cache[ id ].handle ) { + // Try/Catch is to handle iframes being unloaded, see #4280 + try { + jQuery.event.remove( jQuery.cache[ id ].handle.elem ); + } catch(e) {} + } + } + }); +} +/*! + * Sizzle CSS Selector Engine - v1.0 + * Copyright 2009, The Dojo Foundation + * More information: http://sizzlejs.com/ + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +(function(){ + +var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, + done = 0, + toString = Object.prototype.toString, + hasDuplicate = false, + baseHasDuplicate = true; + +// Here we check if the JavaScript engine is using some sort of +// optimization where it does not always call our comparision +// function. If that is the case, discard the hasDuplicate value. +// Thus far that includes Google Chrome. +[0, 0].sort(function(){ + baseHasDuplicate = false; + return 0; +}); + +var Sizzle = function(selector, context, results, seed) { + results = results || []; + var origContext = context = context || document; + + if ( context.nodeType !== 1 && context.nodeType !== 9 ) { + return []; + } + + if ( !selector || typeof selector !== "string" ) { + return results; + } + + var parts = [], m, set, checkSet, extra, prune = true, contextXML = isXML(context), + soFar = selector; + + // Reset the position of the chunker regexp (start from head) + while ( (chunker.exec(""), m = chunker.exec(soFar)) !== null ) { + soFar = m[3]; + + parts.push( m[1] ); + + if ( m[2] ) { + extra = m[3]; + break; + } + } + + if ( parts.length > 1 && origPOS.exec( selector ) ) { + if ( parts.length === 2 && Expr.relative[ parts[0] ] ) { + set = posProcess( parts[0] + parts[1], context ); + } else { + set = Expr.relative[ parts[0] ] ? + [ context ] : + Sizzle( parts.shift(), context ); + + while ( parts.length ) { + selector = parts.shift(); + + if ( Expr.relative[ selector ] ) { + selector += parts.shift(); + } + + set = posProcess( selector, set ); + } + } + } else { + // Take a shortcut and set the context if the root selector is an ID + // (but not if it'll be faster if the inner selector is an ID) + if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML && + Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) { + var ret = Sizzle.find( parts.shift(), context, contextXML ); + context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0]; + } + + if ( context ) { + var ret = seed ? + { expr: parts.pop(), set: makeArray(seed) } : + Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML ); + set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set; + + if ( parts.length > 0 ) { + checkSet = makeArray(set); + } else { + prune = false; + } + + while ( parts.length ) { + var cur = parts.pop(), pop = cur; + + if ( !Expr.relative[ cur ] ) { + cur = ""; + } else { + pop = parts.pop(); + } + + if ( pop == null ) { + pop = context; + } + + Expr.relative[ cur ]( checkSet, pop, contextXML ); + } + } else { + checkSet = parts = []; + } + } + + if ( !checkSet ) { + checkSet = set; + } + + if ( !checkSet ) { + Sizzle.error( cur || selector ); + } + + if ( toString.call(checkSet) === "[object Array]" ) { + if ( !prune ) { + results.push.apply( results, checkSet ); + } else if ( context && context.nodeType === 1 ) { + for ( var i = 0; checkSet[i] != null; i++ ) { + if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) { + results.push( set[i] ); + } + } + } else { + for ( var i = 0; checkSet[i] != null; i++ ) { + if ( checkSet[i] && checkSet[i].nodeType === 1 ) { + results.push( set[i] ); + } + } + } + } else { + makeArray( checkSet, results ); + } + + if ( extra ) { + Sizzle( extra, origContext, results, seed ); + Sizzle.uniqueSort( results ); + } + + return results; +}; + +Sizzle.uniqueSort = function(results){ + if ( sortOrder ) { + hasDuplicate = baseHasDuplicate; + results.sort(sortOrder); + + if ( hasDuplicate ) { + for ( var i = 1; i < results.length; i++ ) { + if ( results[i] === results[i-1] ) { + results.splice(i--, 1); + } + } + } + } + + return results; +}; + +Sizzle.matches = function(expr, set){ + return Sizzle(expr, null, null, set); +}; + +Sizzle.find = function(expr, context, isXML){ + var set, match; + + if ( !expr ) { + return []; + } + + for ( var i = 0, l = Expr.order.length; i < l; i++ ) { + var type = Expr.order[i], match; + + if ( (match = Expr.leftMatch[ type ].exec( expr )) ) { + var left = match[1]; + match.splice(1,1); + + if ( left.substr( left.length - 1 ) !== "\\" ) { + match[1] = (match[1] || "").replace(/\\/g, ""); + set = Expr.find[ type ]( match, context, isXML ); + if ( set != null ) { + expr = expr.replace( Expr.match[ type ], "" ); + break; + } + } + } + } + + if ( !set ) { + set = context.getElementsByTagName("*"); + } + + return {set: set, expr: expr}; +}; + +Sizzle.filter = function(expr, set, inplace, not){ + var old = expr, result = [], curLoop = set, match, anyFound, + isXMLFilter = set && set[0] && isXML(set[0]); + + while ( expr && set.length ) { + for ( var type in Expr.filter ) { + if ( (match = Expr.leftMatch[ type ].exec( expr )) != null && match[2] ) { + var filter = Expr.filter[ type ], found, item, left = match[1]; + anyFound = false; + + match.splice(1,1); + + if ( left.substr( left.length - 1 ) === "\\" ) { + continue; + } + + if ( curLoop === result ) { + result = []; + } + + if ( Expr.preFilter[ type ] ) { + match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter ); + + if ( !match ) { + anyFound = found = true; + } else if ( match === true ) { + continue; + } + } + + if ( match ) { + for ( var i = 0; (item = curLoop[i]) != null; i++ ) { + if ( item ) { + found = filter( item, match, i, curLoop ); + var pass = not ^ !!found; + + if ( inplace && found != null ) { + if ( pass ) { + anyFound = true; + } else { + curLoop[i] = false; + } + } else if ( pass ) { + result.push( item ); + anyFound = true; + } + } + } + } + + if ( found !== undefined ) { + if ( !inplace ) { + curLoop = result; + } + + expr = expr.replace( Expr.match[ type ], "" ); + + if ( !anyFound ) { + return []; + } + + break; + } + } + } + + // Improper expression + if ( expr === old ) { + if ( anyFound == null ) { + Sizzle.error( expr ); + } else { + break; + } + } + + old = expr; + } + + return curLoop; +}; + +Sizzle.error = function( msg ) { + throw "Syntax error, unrecognized expression: " + msg; +}; + +var Expr = Sizzle.selectors = { + order: [ "ID", "NAME", "TAG" ], + match: { + ID: /#((?:[\w\u00c0-\uFFFF-]|\\.)+)/, + CLASS: /\.((?:[\w\u00c0-\uFFFF-]|\\.)+)/, + NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF-]|\\.)+)['"]*\]/, + ATTR: /\[\s*((?:[\w\u00c0-\uFFFF-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/, + TAG: /^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)/, + CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/, + POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/, + PSEUDO: /:((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\((['"]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?/ + }, + leftMatch: {}, + attrMap: { + "class": "className", + "for": "htmlFor" + }, + attrHandle: { + href: function(elem){ + return elem.getAttribute("href"); + } + }, + relative: { + "+": function(checkSet, part){ + var isPartStr = typeof part === "string", + isTag = isPartStr && !/\W/.test(part), + isPartStrNotTag = isPartStr && !isTag; + + if ( isTag ) { + part = part.toLowerCase(); + } + + for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) { + if ( (elem = checkSet[i]) ) { + while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {} + + checkSet[i] = isPartStrNotTag || elem && elem.nodeName.toLowerCase() === part ? + elem || false : + elem === part; + } + } + + if ( isPartStrNotTag ) { + Sizzle.filter( part, checkSet, true ); + } + }, + ">": function(checkSet, part){ + var isPartStr = typeof part === "string"; + + if ( isPartStr && !/\W/.test(part) ) { + part = part.toLowerCase(); + + for ( var i = 0, l = checkSet.length; i < l; i++ ) { + var elem = checkSet[i]; + if ( elem ) { + var parent = elem.parentNode; + checkSet[i] = parent.nodeName.toLowerCase() === part ? parent : false; + } + } + } else { + for ( var i = 0, l = checkSet.length; i < l; i++ ) { + var elem = checkSet[i]; + if ( elem ) { + checkSet[i] = isPartStr ? + elem.parentNode : + elem.parentNode === part; + } + } + + if ( isPartStr ) { + Sizzle.filter( part, checkSet, true ); + } + } + }, + "": function(checkSet, part, isXML){ + var doneName = done++, checkFn = dirCheck; + + if ( typeof part === "string" && !/\W/.test(part) ) { + var nodeCheck = part = part.toLowerCase(); + checkFn = dirNodeCheck; + } + + checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML); + }, + "~": function(checkSet, part, isXML){ + var doneName = done++, checkFn = dirCheck; + + if ( typeof part === "string" && !/\W/.test(part) ) { + var nodeCheck = part = part.toLowerCase(); + checkFn = dirNodeCheck; + } + + checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML); + } + }, + find: { + ID: function(match, context, isXML){ + if ( typeof context.getElementById !== "undefined" && !isXML ) { + var m = context.getElementById(match[1]); + return m ? [m] : []; + } + }, + NAME: function(match, context){ + if ( typeof context.getElementsByName !== "undefined" ) { + var ret = [], results = context.getElementsByName(match[1]); + + for ( var i = 0, l = results.length; i < l; i++ ) { + if ( results[i].getAttribute("name") === match[1] ) { + ret.push( results[i] ); + } + } + + return ret.length === 0 ? null : ret; + } + }, + TAG: function(match, context){ + return context.getElementsByTagName(match[1]); + } + }, + preFilter: { + CLASS: function(match, curLoop, inplace, result, not, isXML){ + match = " " + match[1].replace(/\\/g, "") + " "; + + if ( isXML ) { + return match; + } + + for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) { + if ( elem ) { + if ( not ^ (elem.className && (" " + elem.className + " ").replace(/[\t\n]/g, " ").indexOf(match) >= 0) ) { + if ( !inplace ) { + result.push( elem ); + } + } else if ( inplace ) { + curLoop[i] = false; + } + } + } + + return false; + }, + ID: function(match){ + return match[1].replace(/\\/g, ""); + }, + TAG: function(match, curLoop){ + return match[1].toLowerCase(); + }, + CHILD: function(match){ + if ( match[1] === "nth" ) { + // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6' + var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec( + match[2] === "even" && "2n" || match[2] === "odd" && "2n+1" || + !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]); + + // calculate the numbers (first)n+(last) including if they are negative + match[2] = (test[1] + (test[2] || 1)) - 0; + match[3] = test[3] - 0; + } + + // TODO: Move to normal caching system + match[0] = done++; + + return match; + }, + ATTR: function(match, curLoop, inplace, result, not, isXML){ + var name = match[1].replace(/\\/g, ""); + + if ( !isXML && Expr.attrMap[name] ) { + match[1] = Expr.attrMap[name]; + } + + if ( match[2] === "~=" ) { + match[4] = " " + match[4] + " "; + } + + return match; + }, + PSEUDO: function(match, curLoop, inplace, result, not){ + if ( match[1] === "not" ) { + // If we're dealing with a complex expression, or a simple one + if ( ( chunker.exec(match[3]) || "" ).length > 1 || /^\w/.test(match[3]) ) { + match[3] = Sizzle(match[3], null, null, curLoop); + } else { + var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not); + if ( !inplace ) { + result.push.apply( result, ret ); + } + return false; + } + } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) { + return true; + } + + return match; + }, + POS: function(match){ + match.unshift( true ); + return match; + } + }, + filters: { + enabled: function(elem){ + return elem.disabled === false && elem.type !== "hidden"; + }, + disabled: function(elem){ + return elem.disabled === true; + }, + checked: function(elem){ + return elem.checked === true; + }, + selected: function(elem){ + // Accessing this property makes selected-by-default + // options in Safari work properly + elem.parentNode.selectedIndex; + return elem.selected === true; + }, + parent: function(elem){ + return !!elem.firstChild; + }, + empty: function(elem){ + return !elem.firstChild; + }, + has: function(elem, i, match){ + return !!Sizzle( match[3], elem ).length; + }, + header: function(elem){ + return /h\d/i.test( elem.nodeName ); + }, + text: function(elem){ + return "text" === elem.type; + }, + radio: function(elem){ + return "radio" === elem.type; + }, + checkbox: function(elem){ + return "checkbox" === elem.type; + }, + file: function(elem){ + return "file" === elem.type; + }, + password: function(elem){ + return "password" === elem.type; + }, + submit: function(elem){ + return "submit" === elem.type; + }, + image: function(elem){ + return "image" === elem.type; + }, + reset: function(elem){ + return "reset" === elem.type; + }, + button: function(elem){ + return "button" === elem.type || elem.nodeName.toLowerCase() === "button"; + }, + input: function(elem){ + return /input|select|textarea|button/i.test(elem.nodeName); + } + }, + setFilters: { + first: function(elem, i){ + return i === 0; + }, + last: function(elem, i, match, array){ + return i === array.length - 1; + }, + even: function(elem, i){ + return i % 2 === 0; + }, + odd: function(elem, i){ + return i % 2 === 1; + }, + lt: function(elem, i, match){ + return i < match[3] - 0; + }, + gt: function(elem, i, match){ + return i > match[3] - 0; + }, + nth: function(elem, i, match){ + return match[3] - 0 === i; + }, + eq: function(elem, i, match){ + return match[3] - 0 === i; + } + }, + filter: { + PSEUDO: function(elem, match, i, array){ + var name = match[1], filter = Expr.filters[ name ]; + + if ( filter ) { + return filter( elem, i, match, array ); + } else if ( name === "contains" ) { + return (elem.textContent || elem.innerText || getText([ elem ]) || "").indexOf(match[3]) >= 0; + } else if ( name === "not" ) { + var not = match[3]; + + for ( var i = 0, l = not.length; i < l; i++ ) { + if ( not[i] === elem ) { + return false; + } + } + + return true; + } else { + Sizzle.error( "Syntax error, unrecognized expression: " + name ); + } + }, + CHILD: function(elem, match){ + var type = match[1], node = elem; + switch (type) { + case 'only': + case 'first': + while ( (node = node.previousSibling) ) { + if ( node.nodeType === 1 ) { + return false; + } + } + if ( type === "first" ) { + return true; + } + node = elem; + case 'last': + while ( (node = node.nextSibling) ) { + if ( node.nodeType === 1 ) { + return false; + } + } + return true; + case 'nth': + var first = match[2], last = match[3]; + + if ( first === 1 && last === 0 ) { + return true; + } + + var doneName = match[0], + parent = elem.parentNode; + + if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) { + var count = 0; + for ( node = parent.firstChild; node; node = node.nextSibling ) { + if ( node.nodeType === 1 ) { + node.nodeIndex = ++count; + } + } + parent.sizcache = doneName; + } + + var diff = elem.nodeIndex - last; + if ( first === 0 ) { + return diff === 0; + } else { + return ( diff % first === 0 && diff / first >= 0 ); + } + } + }, + ID: function(elem, match){ + return elem.nodeType === 1 && elem.getAttribute("id") === match; + }, + TAG: function(elem, match){ + return (match === "*" && elem.nodeType === 1) || elem.nodeName.toLowerCase() === match; + }, + CLASS: function(elem, match){ + return (" " + (elem.className || elem.getAttribute("class")) + " ") + .indexOf( match ) > -1; + }, + ATTR: function(elem, match){ + var name = match[1], + result = Expr.attrHandle[ name ] ? + Expr.attrHandle[ name ]( elem ) : + elem[ name ] != null ? + elem[ name ] : + elem.getAttribute( name ), + value = result + "", + type = match[2], + check = match[4]; + + return result == null ? + type === "!=" : + type === "=" ? + value === check : + type === "*=" ? + value.indexOf(check) >= 0 : + type === "~=" ? + (" " + value + " ").indexOf(check) >= 0 : + !check ? + value && result !== false : + type === "!=" ? + value !== check : + type === "^=" ? + value.indexOf(check) === 0 : + type === "$=" ? + value.substr(value.length - check.length) === check : + type === "|=" ? + value === check || value.substr(0, check.length + 1) === check + "-" : + false; + }, + POS: function(elem, match, i, array){ + var name = match[2], filter = Expr.setFilters[ name ]; + + if ( filter ) { + return filter( elem, i, match, array ); + } + } + } +}; + +var origPOS = Expr.match.POS; + +for ( var type in Expr.match ) { + Expr.match[ type ] = new RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source ); + Expr.leftMatch[ type ] = new RegExp( /(^(?:.|\r|\n)*?)/.source + Expr.match[ type ].source.replace(/\\(\d+)/g, function(all, num){ + return "\\" + (num - 0 + 1); + })); +} + +var makeArray = function(array, results) { + array = Array.prototype.slice.call( array, 0 ); + + if ( results ) { + results.push.apply( results, array ); + return results; + } + + return array; +}; + +// Perform a simple check to determine if the browser is capable of +// converting a NodeList to an array using builtin methods. +try { + Array.prototype.slice.call( document.documentElement.childNodes, 0 ); + +// Provide a fallback method if it does not work +} catch(e){ + makeArray = function(array, results) { + var ret = results || []; + + if ( toString.call(array) === "[object Array]" ) { + Array.prototype.push.apply( ret, array ); + } else { + if ( typeof array.length === "number" ) { + for ( var i = 0, l = array.length; i < l; i++ ) { + ret.push( array[i] ); + } + } else { + for ( var i = 0; array[i]; i++ ) { + ret.push( array[i] ); + } + } + } + + return ret; + }; +} + +var sortOrder; + +if ( document.documentElement.compareDocumentPosition ) { + sortOrder = function( a, b ) { + if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) { + if ( a == b ) { + hasDuplicate = true; + } + return a.compareDocumentPosition ? -1 : 1; + } + + var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1; + if ( ret === 0 ) { + hasDuplicate = true; + } + return ret; + }; +} else if ( "sourceIndex" in document.documentElement ) { + sortOrder = function( a, b ) { + if ( !a.sourceIndex || !b.sourceIndex ) { + if ( a == b ) { + hasDuplicate = true; + } + return a.sourceIndex ? -1 : 1; + } + + var ret = a.sourceIndex - b.sourceIndex; + if ( ret === 0 ) { + hasDuplicate = true; + } + return ret; + }; +} else if ( document.createRange ) { + sortOrder = function( a, b ) { + if ( !a.ownerDocument || !b.ownerDocument ) { + if ( a == b ) { + hasDuplicate = true; + } + return a.ownerDocument ? -1 : 1; + } + + var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange(); + aRange.setStart(a, 0); + aRange.setEnd(a, 0); + bRange.setStart(b, 0); + bRange.setEnd(b, 0); + var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange); + if ( ret === 0 ) { + hasDuplicate = true; + } + return ret; + }; +} + +// Utility function for retreiving the text value of an array of DOM nodes +function getText( elems ) { + var ret = "", elem; + + for ( var i = 0; elems[i]; i++ ) { + elem = elems[i]; + + // Get the text from text nodes and CDATA nodes + if ( elem.nodeType === 3 || elem.nodeType === 4 ) { + ret += elem.nodeValue; + + // Traverse everything else, except comment nodes + } else if ( elem.nodeType !== 8 ) { + ret += getText( elem.childNodes ); + } + } + + return ret; +} + +// Check to see if the browser returns elements by name when +// querying by getElementById (and provide a workaround) +(function(){ + // We're going to inject a fake input element with a specified name + var form = document.createElement("div"), + id = "script" + (new Date).getTime(); + form.innerHTML = ""; + + // Inject it into the root element, check its status, and remove it quickly + var root = document.documentElement; + root.insertBefore( form, root.firstChild ); + + // The workaround has to do additional checks after a getElementById + // Which slows things down for other browsers (hence the branching) + if ( document.getElementById( id ) ) { + Expr.find.ID = function(match, context, isXML){ + if ( typeof context.getElementById !== "undefined" && !isXML ) { + var m = context.getElementById(match[1]); + return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : []; + } + }; + + Expr.filter.ID = function(elem, match){ + var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id"); + return elem.nodeType === 1 && node && node.nodeValue === match; + }; + } + + root.removeChild( form ); + root = form = null; // release memory in IE +})(); + +(function(){ + // Check to see if the browser returns only elements + // when doing getElementsByTagName("*") + + // Create a fake element + var div = document.createElement("div"); + div.appendChild( document.createComment("") ); + + // Make sure no comments are found + if ( div.getElementsByTagName("*").length > 0 ) { + Expr.find.TAG = function(match, context){ + var results = context.getElementsByTagName(match[1]); + + // Filter out possible comments + if ( match[1] === "*" ) { + var tmp = []; + + for ( var i = 0; results[i]; i++ ) { + if ( results[i].nodeType === 1 ) { + tmp.push( results[i] ); + } + } + + results = tmp; + } + + return results; + }; + } + + // Check to see if an attribute returns normalized href attributes + div.innerHTML = ""; + if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" && + div.firstChild.getAttribute("href") !== "#" ) { + Expr.attrHandle.href = function(elem){ + return elem.getAttribute("href", 2); + }; + } + + div = null; // release memory in IE +})(); + +if ( document.querySelectorAll ) { + (function(){ + var oldSizzle = Sizzle, div = document.createElement("div"); + div.innerHTML = "

      "; + + // Safari can't handle uppercase or unicode characters when + // in quirks mode. + if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) { + return; + } + + Sizzle = function(query, context, extra, seed){ + context = context || document; + + // Only use querySelectorAll on non-XML documents + // (ID selectors don't work in non-HTML documents) + if ( !seed && context.nodeType === 9 && !isXML(context) ) { + try { + return makeArray( context.querySelectorAll(query), extra ); + } catch(e){} + } + + return oldSizzle(query, context, extra, seed); + }; + + for ( var prop in oldSizzle ) { + Sizzle[ prop ] = oldSizzle[ prop ]; + } + + div = null; // release memory in IE + })(); +} + +(function(){ + var div = document.createElement("div"); + + div.innerHTML = "
      "; + + // Opera can't find a second classname (in 9.6) + // Also, make sure that getElementsByClassName actually exists + if ( !div.getElementsByClassName || div.getElementsByClassName("e").length === 0 ) { + return; + } + + // Safari caches class attributes, doesn't catch changes (in 3.2) + div.lastChild.className = "e"; + + if ( div.getElementsByClassName("e").length === 1 ) { + return; + } + + Expr.order.splice(1, 0, "CLASS"); + Expr.find.CLASS = function(match, context, isXML) { + if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) { + return context.getElementsByClassName(match[1]); + } + }; + + div = null; // release memory in IE +})(); + +function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { + for ( var i = 0, l = checkSet.length; i < l; i++ ) { + var elem = checkSet[i]; + if ( elem ) { + elem = elem[dir]; + var match = false; + + while ( elem ) { + if ( elem.sizcache === doneName ) { + match = checkSet[elem.sizset]; + break; + } + + if ( elem.nodeType === 1 && !isXML ){ + elem.sizcache = doneName; + elem.sizset = i; + } + + if ( elem.nodeName.toLowerCase() === cur ) { + match = elem; + break; + } + + elem = elem[dir]; + } + + checkSet[i] = match; + } + } +} + +function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { + for ( var i = 0, l = checkSet.length; i < l; i++ ) { + var elem = checkSet[i]; + if ( elem ) { + elem = elem[dir]; + var match = false; + + while ( elem ) { + if ( elem.sizcache === doneName ) { + match = checkSet[elem.sizset]; + break; + } + + if ( elem.nodeType === 1 ) { + if ( !isXML ) { + elem.sizcache = doneName; + elem.sizset = i; + } + if ( typeof cur !== "string" ) { + if ( elem === cur ) { + match = true; + break; + } + + } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) { + match = elem; + break; + } + } + + elem = elem[dir]; + } + + checkSet[i] = match; + } + } +} + +var contains = document.compareDocumentPosition ? function(a, b){ + return a.compareDocumentPosition(b) & 16; +} : function(a, b){ + return a !== b && (a.contains ? a.contains(b) : true); +}; + +var isXML = function(elem){ + // documentElement is verified for cases where it doesn't yet exist + // (such as loading iframes in IE - #4833) + var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement; + return documentElement ? documentElement.nodeName !== "HTML" : false; +}; + +var posProcess = function(selector, context){ + var tmpSet = [], later = "", match, + root = context.nodeType ? [context] : context; + + // Position selectors must be done after the filter + // And so must :not(positional) so we move all PSEUDOs to the end + while ( (match = Expr.match.PSEUDO.exec( selector )) ) { + later += match[0]; + selector = selector.replace( Expr.match.PSEUDO, "" ); + } + + selector = Expr.relative[selector] ? selector + "*" : selector; + + for ( var i = 0, l = root.length; i < l; i++ ) { + Sizzle( selector, root[i], tmpSet ); + } + + return Sizzle.filter( later, tmpSet ); +}; + +// EXPOSE +jQuery.find = Sizzle; +jQuery.expr = Sizzle.selectors; +jQuery.expr[":"] = jQuery.expr.filters; +jQuery.unique = Sizzle.uniqueSort; +jQuery.getText = getText; +jQuery.isXMLDoc = isXML; +jQuery.contains = contains; + +return; + +window.Sizzle = Sizzle; + +})(); +var runtil = /Until$/, + rparentsprev = /^(?:parents|prevUntil|prevAll)/, + // Note: This RegExp should be improved, or likely pulled from Sizzle + rmultiselector = /,/, + slice = Array.prototype.slice; + +// Implement the identical functionality for filter and not +var winnow = function( elements, qualifier, keep ) { + if ( jQuery.isFunction( qualifier ) ) { + return jQuery.grep(elements, function( elem, i ) { + return !!qualifier.call( elem, i, elem ) === keep; + }); + + } else if ( qualifier.nodeType ) { + return jQuery.grep(elements, function( elem, i ) { + return (elem === qualifier) === keep; + }); + + } else if ( typeof qualifier === "string" ) { + var filtered = jQuery.grep(elements, function( elem ) { + return elem.nodeType === 1; + }); + + if ( isSimple.test( qualifier ) ) { + return jQuery.filter(qualifier, filtered, !keep); + } else { + qualifier = jQuery.filter( qualifier, filtered ); + } + } + + return jQuery.grep(elements, function( elem, i ) { + return (jQuery.inArray( elem, qualifier ) >= 0) === keep; + }); +}; + +jQuery.fn.extend({ + find: function( selector ) { + var ret = this.pushStack( "", "find", selector ), length = 0; + + for ( var i = 0, l = this.length; i < l; i++ ) { + length = ret.length; + jQuery.find( selector, this[i], ret ); + + if ( i > 0 ) { + // Make sure that the results are unique + for ( var n = length; n < ret.length; n++ ) { + for ( var r = 0; r < length; r++ ) { + if ( ret[r] === ret[n] ) { + ret.splice(n--, 1); + break; + } + } + } + } + } + + return ret; + }, + + has: function( target ) { + var targets = jQuery( target ); + return this.filter(function() { + for ( var i = 0, l = targets.length; i < l; i++ ) { + if ( jQuery.contains( this, targets[i] ) ) { + return true; + } + } + }); + }, + + not: function( selector ) { + return this.pushStack( winnow(this, selector, false), "not", selector); + }, + + filter: function( selector ) { + return this.pushStack( winnow(this, selector, true), "filter", selector ); + }, + + is: function( selector ) { + return !!selector && jQuery.filter( selector, this ).length > 0; + }, + + closest: function( selectors, context ) { + if ( jQuery.isArray( selectors ) ) { + var ret = [], cur = this[0], match, matches = {}, selector; + + if ( cur && selectors.length ) { + for ( var i = 0, l = selectors.length; i < l; i++ ) { + selector = selectors[i]; + + if ( !matches[selector] ) { + matches[selector] = jQuery.expr.match.POS.test( selector ) ? + jQuery( selector, context || this.context ) : + selector; + } + } + + while ( cur && cur.ownerDocument && cur !== context ) { + for ( selector in matches ) { + match = matches[selector]; + + if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) { + ret.push({ selector: selector, elem: cur }); + delete matches[selector]; + } + } + cur = cur.parentNode; + } + } + + return ret; + } + + var pos = jQuery.expr.match.POS.test( selectors ) ? + jQuery( selectors, context || this.context ) : null; + + return this.map(function( i, cur ) { + while ( cur && cur.ownerDocument && cur !== context ) { + if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) { + return cur; + } + cur = cur.parentNode; + } + return null; + }); + }, + + // Determine the position of an element within + // the matched set of elements + index: function( elem ) { + if ( !elem || typeof elem === "string" ) { + return jQuery.inArray( this[0], + // If it receives a string, the selector is used + // If it receives nothing, the siblings are used + elem ? jQuery( elem ) : this.parent().children() ); + } + // Locate the position of the desired element + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem.jquery ? elem[0] : elem, this ); + }, + + add: function( selector, context ) { + var set = typeof selector === "string" ? + jQuery( selector, context || this.context ) : + jQuery.makeArray( selector ), + all = jQuery.merge( this.get(), set ); + + return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ? + all : + jQuery.unique( all ) ); + }, + + andSelf: function() { + return this.add( this.prevObject ); + } +}); + +// A painfully simple check to see if an element is disconnected +// from a document (should be improved, where feasible). +function isDisconnected( node ) { + return !node || !node.parentNode || node.parentNode.nodeType === 11; +} + +jQuery.each({ + parent: function( elem ) { + var parent = elem.parentNode; + return parent && parent.nodeType !== 11 ? parent : null; + }, + parents: function( elem ) { + return jQuery.dir( elem, "parentNode" ); + }, + parentsUntil: function( elem, i, until ) { + return jQuery.dir( elem, "parentNode", until ); + }, + next: function( elem ) { + return jQuery.nth( elem, 2, "nextSibling" ); + }, + prev: function( elem ) { + return jQuery.nth( elem, 2, "previousSibling" ); + }, + nextAll: function( elem ) { + return jQuery.dir( elem, "nextSibling" ); + }, + prevAll: function( elem ) { + return jQuery.dir( elem, "previousSibling" ); + }, + nextUntil: function( elem, i, until ) { + return jQuery.dir( elem, "nextSibling", until ); + }, + prevUntil: function( elem, i, until ) { + return jQuery.dir( elem, "previousSibling", until ); + }, + siblings: function( elem ) { + return jQuery.sibling( elem.parentNode.firstChild, elem ); + }, + children: function( elem ) { + return jQuery.sibling( elem.firstChild ); + }, + contents: function( elem ) { + return jQuery.nodeName( elem, "iframe" ) ? + elem.contentDocument || elem.contentWindow.document : + jQuery.makeArray( elem.childNodes ); + } +}, function( name, fn ) { + jQuery.fn[ name ] = function( until, selector ) { + var ret = jQuery.map( this, fn, until ); + + if ( !runtil.test( name ) ) { + selector = until; + } + + if ( selector && typeof selector === "string" ) { + ret = jQuery.filter( selector, ret ); + } + + ret = this.length > 1 ? jQuery.unique( ret ) : ret; + + if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) { + ret = ret.reverse(); + } + + return this.pushStack( ret, name, slice.call(arguments).join(",") ); + }; +}); + +jQuery.extend({ + filter: function( expr, elems, not ) { + if ( not ) { + expr = ":not(" + expr + ")"; + } + + return jQuery.find.matches(expr, elems); + }, + + dir: function( elem, dir, until ) { + var matched = [], cur = elem[dir]; + while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { + if ( cur.nodeType === 1 ) { + matched.push( cur ); + } + cur = cur[dir]; + } + return matched; + }, + + nth: function( cur, result, dir, elem ) { + result = result || 1; + var num = 0; + + for ( ; cur; cur = cur[dir] ) { + if ( cur.nodeType === 1 && ++num === result ) { + break; + } + } + + return cur; + }, + + sibling: function( n, elem ) { + var r = []; + + for ( ; n; n = n.nextSibling ) { + if ( n.nodeType === 1 && n !== elem ) { + r.push( n ); + } + } + + return r; + } +}); +var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, + rleadingWhitespace = /^\s+/, + rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g, + rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i, + rtagName = /<([\w:]+)/, + rtbody = /"; + }, + wrapMap = { + option: [ 1, "" ], + legend: [ 1, "
      ", "
      " ], + thead: [ 1, "", "
      " ], + tr: [ 2, "", "
      " ], + td: [ 3, "", "
      " ], + col: [ 2, "", "
      " ], + area: [ 1, "", "" ], + _default: [ 0, "", "" ] + }; + +wrapMap.optgroup = wrapMap.option; +wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead; +wrapMap.th = wrapMap.td; + +// IE can't serialize and + +``` + +In the code above, the `button` is the event **target** (the button element that +will fire the event), `'click'` is the event **type** (when the button is +clicked, it fires this type of event), and the function + +```javascript +function(evt) { + alert('You clicked me!'); +} +``` + +is the **callback** (the code that runs when the event is triggered). + +## Exercises + +### Level 1 - Functions that Take Functions as Inputs + +* [Introduction to Callbacks](introduction_to_callbacks) +* [setTimeout and setInterval](settimeout_and_setinterval) diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/00_SUGGESTED_ORDER.md b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/00_SUGGESTED_ORDER.md new file mode 100644 index 0000000..cfedc41 --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/00_SUGGESTED_ORDER.md @@ -0,0 +1,4 @@ +1. `passing_functions_to_functions.js` +2. `introducing_predicate_functions.js` +3. `calling_a_callback_function_more_than_once.js` +4. `using_callback_functions_to_transform_arrays.js` diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/README.md b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/README.md new file mode 100644 index 0000000..4e8ec3f --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/README.md @@ -0,0 +1,24 @@ +# Introduction to Callbacks + +In JavaScript, we frequently encounter functions which take other functions as +arguments. This is a powerful technique known as a "callback function". Callback +functions, also referred to as simply "callbacks", are used all over the place +in JavaScript. + +Understanding how to use callback functions as well as how to write functions +that accept callback functions as arguments is critical. + +These exercises are intended to get a developer comfortable with callbacks in +JavaScript. + +## Let's Get Started + +1. Open the first JavaScript file as noted by [the suggested + order](00_SUGGESTED_ORDER.md). +2. Review the use cases at the bottom of the JavaScript file. +3. Uncomment the first use case and make the code work. (Frequently this means + replacing `____`s with code of your own.) +4. Verify it works. (Run the JavaScript file with `node`.) +5. Repeat steps 3 and 4 until all use cases are working. +6. Repeat steps 1 to 5 until you've worked through all the JavaScript files. +7. Submit a request for feedback on your commits! diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/calling_a_callback_function_more_than_once.js b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/calling_a_callback_function_more_than_once.js new file mode 100644 index 0000000..6d83520 --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/calling_a_callback_function_more_than_once.js @@ -0,0 +1,125 @@ +// 1. Scroll to the bottom to get to the use cases. +// 2. Uncomment the next use case and make it work. Frequently this means replacing +// `____`s with code of your own. +// 3. Verify it works by running the file with the `node` command line program. +// 4. Repeat until all use cases are functioning. + +// This function checks if the contents of an array are equal to one another. +// Why? Because `["hi"] === ["hi"]` is false in JavaScript. Le-sigh. +var bothArraysAreEqual = function(arrayA, arrayB) { + // If arrayA and arrayB are the same object then we don't have to compare + // their elements. + // ``` + // var a = ["hi", "there"]; + // var b = a; + // var c = ["hi", "there"]; + // a === b; // True :( + // c === a; // False + // ``` + if (arrayA === arrayB) { return true; } + + // If arrayA and arrayB aren't the same length + // they can't possibly be the same + if (arrayA.length !== arrayB.length) { + return false; + } + + for (var i = 0; i < arrayA.length; i++) { + // If any of the elements are different, BAIL OUT! + if (arrayA[i] !== arrayB[i]) { + return false; + } + } + return true; +} + +/* +// This function executes a callback function many times. +var doThisManyTimes = function(numberOfTimes, ____) { + for (var i = 0; i < numberOfTimes; i++) { + ____(); + } +}; +*/ + + +/* +// This function will go over every element in an array one by one, calling the +// callback with each item. Replace the `____`s to make it work. +var each = function(array, ____) { + for (var ____ = ____; ____ < array.length; ____++) { + var ____ = array[i]; + ____(____); + } +}; +*/ + + +// USE CASES ARE HERE. STOP SCROLLING! STOOOPPPPPPP. +// OK, now read on! +if (!module.parent) { + // Callback functions provide a powerful way to enable code re-use. + // Given the above functions, can you replace the `____`s with code to + // cause the expected result? + + // Uncomment each use case and get it working before moving to the next. + // You will know the code works because the word "true" will appear when + // you run `node calling_a_callback_function_more_than_once.js` + + /* + // Find, uncomment and implement the `doThisManyTimes` function above. Then + // update this code to use it. + var wows = []; + ____(5, function() { + wows.push("wow!"); + }); + + var expectedWows = ["wow!", "wow!", "wow!", "wow!", "wow!"]; + console.log(bothArraysAreEqual(wows, expectedWows)); + */ + + /* + // Find, uncomment and implement the `each` function above, then update this + // code to use it. + var doubles = []; + ____([4, 2, 9], ____(num) { + ____.____(num * 2); + }); + + var expectedDoubles = [8, 4, 18]; + + console.log(bothArraysAreEqual(doubles, expectedDoubles)); + */ + + + /* + // Let's re-use one of our functions to reverse a list of words! + reversedWords = []; + var ____ = ____(____) { + ____.____(____.split("").reverse().join("")); + }; + + each(["qatar", "zinfandel", "onomatopoeia"], reverseWords); + + expectedReversedWords = ["rataq", "lednafniz", "aieopotamono"]; + console.log(bothArraysAreEqual(reversedWords, expectedReversedWords)); + */ + + + /* + // Let's build a list of strings of Q's of varying lengths! + listOfQs = []; + var addQs = function(number) { + var q = ""; + while (number > 0) { + q = q + "Q"; + number = number - 1; + } + ____.____(q); + }; + + var expectedListOfQs = ["QQQ", "Q", "QQQQQQQ"]; + ____([3,1,7], addQs); + console.log(bothArraysAreEqual(listOfQs, expectedListOfQs)); + */ +} diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/introducing_predicate_functions.js b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/introducing_predicate_functions.js new file mode 100644 index 0000000..3e85d9c --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/introducing_predicate_functions.js @@ -0,0 +1,81 @@ +// 1. Scroll to the bottom to get to the use cases. +// 2. Uncomment the next use case and make it work. Frequently this means +// replacing `____`s with code of your own. +// 3. Verify it works by running the file with the `node` command line program. +// 4. Repeat until all use cases are functioning. + +var conservativeSpender = function(balance) { + return balance > 100; +}; + +var liberalSpender = function(balance) { + return balance > 10; +}; + +var horribleSaver = function(balance) { + return balance > 0; +}; + +var yourSpendingStrategy = function(balance) { + // Your code here +}; + +/* +var shouldIBuyThis = function(balance, ____) { + if ( ____(balance) ) { + return "Sure! I've got the money!"; + } else { + return "Nope! Gotta keep my savings up!"; + } +}; +*/ + +/* +function smarterShouldIBuyThis(cost, balance, strategy) { + var futureBalance = balance - cost; + if ( ____(futureBalance) ) { + return "Sure! I've got the money!"; + } else { + return "Nope! Gotta keep my savings up!"; + } +}; +*/ + + +// USE CASES ARE HERE. STOP SCROLLING! STOOOPPPPPPP. +// OK, now read on! +// +if (!module.parent) { + // Uncomment each use case and get it working before moving to the next. + // You will know the code works because the word "true" will appear when + // you run `node introducing_predicate_functions.js` + + // We're going to use a callback to decide whether we want to buy an item or + // not, based on our account balance. This kind of callback function (one + // which returns true or false) is known as a "predicate" function. + + // Find, uncomment and implement the `shouldIBuyThis` function above, then + // uncomment and implement each of the following use cases one by one. + + // console.log(shouldIBuyThis(20, ____) === "Nope! Gotta keep my savings up!"); + // console.log(shouldIBuyThis(20, ____) === "Sure! I've got the money!"); + // console.log(shouldIBuyThis(____, horribleSaver) === "Sure! I've got the money!"); + + // Find, uncomment, and implement the `smarterShouldIBuyThis` function above, + // then uncomment the following use cases + // console.log(smarterShouldIBuyThis(50, 45, horribleSaver) === "Nope! Gotta keep my savings up!"); + + // Can you define your own spending strategy? + // Scroll up to yourSpendingStrategy above + // console.log(smarterShouldIBuyThis(____, ____, yourSpendingStrategy) === ____); + + /* + // Like all callbacks, we may define a predicate function as an anonymous + // function (i.e. without storing it in a variable): + console.log(shouldIBuyThis(999, ____(____) { + return ____ > 1000; + }) === "Nope! Gotta keep my savings up!"); + */ +} + + diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/passing_functions_to_functions.js b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/passing_functions_to_functions.js new file mode 100644 index 0000000..039dc4b --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/passing_functions_to_functions.js @@ -0,0 +1,55 @@ +// 1. Scroll to the bottom to get to the use cases. +// 2. Uncomment the next use case and make it work. Frequently this means +// replacing `____`s with code of your own. +// 3. Verify it works by running the file with the `node` command line program. +// 4. Repeat until all use cases are functioning. + +var animalToCatTranslator = function(sentence) { + return sentence.replace("animal", "cat"); +}; + +var animalToDogTranslator = function(sentence) { + return sentence.replace("animal", "dog"); +}; + +/* +// Write your own translator for your preferred animal! +var ____ = ____(sentence) { + return ____._____(____, ____); +}; +*/ + +var favoriteAnimal = function(animalTranslator) { + return animalTranslator("animals are the best!"); +}; + +// !module.parent is true if the current file is being run directly in node +// and false when being required by another file. +if (!module.parent) { + // Uncomment each use case and get it working before moving to the next. + // You will know the code works because the word "true" will appear when + // you run `node passing_functions_to_functions.js` + + // Because functions are data in JavaScript you can pass them as arguments to + // other functions. Functions passed to another function to be called are + // known as "callback functions". + + // Choose a callback function from the animalToXTranslators above to make the + // following use cases pass. + // console.log(favoriteAnimal(____) === "cats are the best!"); + // console.log(favoriteAnimal(____) === "dogs are the best!"); + + // Define your own animal translator! Uncomment lines 15 to 20, then make the + // following use case pass. + // console.log(favoriteAnimal(____) === "____s are the best!"); + + // Have you tried to put ()'s after the callback function being passed as an + // argument? What happened? Why do you think that is? + + + // You can also define callback functions in-line: + // console.log(favoriteAnimal(function(sentence) { + // ____ ____.____("animal", "aardvark"); + // } === "aardvarks are the best!"); + // This is known as an "anonymous function". +} diff --git a/javascript-exercises/events_and_callbacks/introduction_to_callbacks/using_callback_functions_to_transform_arrays.js b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/using_callback_functions_to_transform_arrays.js new file mode 100644 index 0000000..30271ad --- /dev/null +++ b/javascript-exercises/events_and_callbacks/introduction_to_callbacks/using_callback_functions_to_transform_arrays.js @@ -0,0 +1,114 @@ +// 1. Scroll to the bottom to get to the use cases. +// 2. Uncomment the next use case and make it work. Frequently this means replacing +// `____`s with code of your own. +// 3. Verify it works by running the file with the `node` command line program. +// 4. Repeat until all use cases are functioning. + +var bothArraysAreEqual = function(arrayA, arrayB) { + if (arrayA === arrayB) { return true; } + + if (arrayA.length !== arrayB.length) { + return false; + } + + for (var i = 0; i < arrayA.length; i++) { + if (arrayA[i] !== arrayB[i]) { + return false; + } + } + return true; +}; + +/* +// This function will go over every element in an array one by one, calling the +// callback with each item, and building a new array with the callback's return +// values. Replace the `____`s to make it work. +var map = function(____, ____) { + var newArray = []; + for (var ____ = ____; ____ < ____.____; ____++) { + var ____ = ____[____]; + newArray[____] = ____(____); + } + return newArray; +}; +*/ + +/* +// This function will go over every element in an array one by one, calling the +// callback with each item, adding the element to a new array only if +// the callback returns true, and finally returning the new array. +var filter = function(____, ____) { + var filteredArray = []; + for (var ____ = ____; ____ < ____.____; ____++) { + var ____ = ____[____]; + if (____(____)) { + filteredArray.push(____); + } + } + return filteredArray; +}; +*/ + + +// USE CASES ARE HERE. STOP SCROLLING! STOOOPPPPPPP. +// OK, now read on! +// +if (!module.parent) { + // Using callbacks to transform arrays is a very common use case. Let's + // implement some functions that take an array and a callback and return a new + // array with the callback applied to it. + + // Uncomment each use case and get it working before moving to the next. + // You will know the code works because the word "true" will appear when + // you run `node using_callback_functions_to_transform_arrays.js` + + /* + // Defining an array, calling each on another array, and filling the new array + // is a common pattern known as "mapping". + // Find, uncomment, and implement the "map" function defined above and fill in + // the `____`s to make it work. + var square = function(number) { + return number * number; + }; + var squaredNumbers = map([2, 8, 3], square); + + var expectedSquaredNumbers = [4, 64, 9]; + console.log(bothArraysAreEqual(squaredNumbers, expectedSquaredNumbers)); + */ + + /* + // Let's shout some words: + var shoutedWords = ____(["one", "two", "three"], ____(____) { + ____ ____.toUpperCase(); + }); + + var expectedShoutedWords = ["ONE", "TWO", "THREE"]; + console.log(bothArraysAreEqual(shoutedWords, expectedShoutedWords)); + */ + + /* + // Sometimes you'll want to take an array and remove elements that don't + // match certain criteria. Uncomment and implement the "filter" function in + // the source code above. + // + // A callback that returns a boolean (a true or false value) is known as a + // "predicate". + + var onlyEvenNumbers = filter([2, 9, 6, 8, 4], ____(____) { + ____ ____ % 2 === 0; + }); + + console.log(bothArraysAreEqual(onlyEvenNumbers, [2, 6, 8, 4]); + */ + + /* + // Let's filter a list down to words that start with the best letter ever. + // Yes. That letter is Z. + + var bestWordPredicate = function(____) { + return ____.toUpperCase().charAt(0) === "Z"; + }; + var onlyAwesomeWords = ____(["zookeeper", "zelda", "misanthrope", "anarchy", "acrostic"], ____); + console.log(bothArraysAreEqual(onlyAwesomeWords, ["zookeeper", "zelda"])); + */ +} diff --git a/javascript-exercises/events_and_callbacks/settimeout_and_setinterval/README.md b/javascript-exercises/events_and_callbacks/settimeout_and_setinterval/README.md new file mode 100644 index 0000000..62c917a --- /dev/null +++ b/javascript-exercises/events_and_callbacks/settimeout_and_setinterval/README.md @@ -0,0 +1,220 @@ +# setTimeout and setInterval + +Both node.js and the browser implement methods that let us write _timed_ code: +`setInterval` and `setTimeout`. + +In other words, we can say things like "wait 5 minutes, then run this code" or +"execute this function every 20 seconds". + +The various applications for this kind of code are many. Anytime you have code +whose execution depends on time, you may reach for these methods. + +For the most part, these methods will work the same whether you run them in the +browser or in node.js. It is important to recognize that these are different +environments, so you should never expect timers to work _exactly_ the same. + +**Documentation references:** + +- [Documentation for Window Timers](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers) +- [Documentation for Node.js Timers](http://nodejs.org/docs/v0.6.1/api/timers.html) + +## Exercises + +### Timeouts fire functions after x milliseconds + +Can you use `setTimeout` to delay execution of a callback function? + +```javascript +console.log("Wait for it..."); +__(function() { console.log("NOW!") }, __); +// should print "NOW!" after waiting one second +``` + +Search suggestion: `delay execution node.js` + +### Delayed execution can be weird to understand + +Make a hypothesis about what the following code will do. Then run it. Did it do +what you expected? If not, why not? + +```javascript +console.log("Before the timeout"); +setTimeout(function() { console.log("In the timeout function"); }, 500); +console.log("After the timeout"); +``` + +What about this code? + +```javascript +console.log("Before the timeout"); +setTimeout(function() { console.log("In the timeout function"); }, 0); +console.log("After the timeout"); +``` + +Search suggestion: `how does setTimeout work javascript` + +### Sometimes you want to cancel the timer + +Can you write code to prevent the bomb from going off? + +```javascript +var bombTimeoutID = setTimeout(function() { console.log("KABOOM!!") }, 7000); +__(__); +``` + +Search suggestion: `cancel setTimeout function javascript` + +### Timers aren't perfect timers + +The following code may be confusing. Make a hypothesis about what you think it +will do, then test your hypothesis by running the code. Did it do what you +expected? Why or why not? + +```javascript +console.log("Before the timeout"); +setTimeout(function() { console.log("In the timeout"); }, 500); +console.log("After the timeout"); + +var now = Date.now(); +console.log("Starting the while loop"); +while (Date.now() < now + 3000) { + // waiting, waiting... +} +console.log("While loop finished"); +``` + +### But they can still be useful + +Can you complete the code below to create the desired behavior? + +```javascript +var timer = function(seconds, callback) { + var milliseconds = seconds * 1000; + console.log("Setting timer for " + seconds + " seconds") + __(__, __); +}; + +var breadReminder = function() { + console.log("Bread is ready!"); +} + +timer(3, breadReminder) +// should print "Bread is ready!" after a delay of 3 seconds +``` + +Search suggestion: `syntax setTimeout function javascript` + +### An example of a potential use case + +Can you complete the code below to configure a session timeout feature (like +what you might find in an online banking account, for example)? + +```javascript +var warnTimeout = function() { + console.log("Your online banking session will end in 5 seconds..."); +}; + +var sessionTimeout = function() { + console.log("Your online banking session has timed out"); +}; + +var sessionLength = 20 * 1000; // 20 seconds +__(__, sessionLength - 5 * 1000); // 5 seconds before +__(__, sessionLength); +// should print a warning in 15 seconds +// and a session timeout notice in 20 seconds +``` + +### Intervals do something over and over and over and... + +Can you make the following code print `Na-na-na-na` every half second? + +```javascript +console.log("Starting the counter"); +__(function() { + console.log("Na-na-na-na"); +}, __) +console.log("Counter finished. Maybe?") +// should print "Na-na-na-na" every half second +``` + +Search suggestion: `call function repeatedly at set times javascript` + +### Forever is a long time + +So it may be useful to be able to stop your perpetual motion machine after +starting it. Can you figure out how to make a stopwatch that counts the seconds +from 0 but actually **stops** after 5 seconds? + +```javascript +var time = 0; +var stopWatch = function() { + console.log(__); + time++; +}; + +console.log("Starting stopwatch"); +stopWatch(); +var __ = setInterval(stopWatch, 1000); + +setTimeout(function() { + __(__); +}, __); +``` + +Search suggestion: `stop function in setInterval javascript` + +### Working with timer IDs for maximum power + +You can have more than one interval going at once, you know. Can you reason +about the following code and see if you can understand what is going on? + +```javascript +var timesCounted = 0; +var counter = function() { + console.log(timesCounted); + timesCounted++; +}; +var counterID = setInterval(counter, 100); + +var monitorID; +var monitor = function() { + if (timesCounted > 20) { + clearInterval(counterID); + clearInterval(monitorID); + } +}; +monitorID = setInterval(monitor, 500); +``` + +### A full-fledged egg timer + +Can you put it all together now and complete the implementation for a working +timer that counts down from x seconds to zero? + +```javascript +var countDown = function(startTime) { + console.log(startTime); + + return setInterval(function() { + __--; + console.log(__); + }, __); +}; + +var setTimer = function(seconds) { + var milliseconds = __ * __; + console.log("Setting timer for " + __ + " seconds"); + + var counterID = countDown(seconds); + + __(function() { + console.log("Timer done!"); + clearInterval(__) + }, __); +}; + +setTimer(3) +// should set a timer for 3 seconds +// and print each second until it stops +``` diff --git a/javascript-exercises/interacting_with_the_user/README.md b/javascript-exercises/interacting_with_the_user/README.md new file mode 100644 index 0000000..ffe7ca1 --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/README.md @@ -0,0 +1,28 @@ +# Interacting with the User + +At the end of the day, software is a tool for people. Lots of programs talk to +each other, but at some point or another a real person is going to interact with +the software. + +The ways in which a user can interact with (give inputs to and read outputs +from) software is called the **user interface**, and it will differ depending on +the context. + +For web applications, the user interface is the browser, which renders HTML with +text, graphics, and media. Before the advent of the **Graphical User +Interface**, or GUI, the only way to interact with a computer was by typing +commands into a terminal and reading the output. + +We're going to explore the ways that JavaScript allows for user interaction. + +## Exercises + +### Level 1 - Collecting Input from the Command Line + +* [Command Line Arguments](command_line_arguments) +* [STDIN and STDOUT](stdin_and_stdout) + +### Level 2 - Finding and Changing Elements on a Web Page + +* [Querying the DOM](querying_the_dom) +* [Manipulating the DOM](manipulating_the_dom) diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/00_SUGGESTED_ORDER.md b/javascript-exercises/interacting_with_the_user/command_line_arguments/00_SUGGESTED_ORDER.md new file mode 100644 index 0000000..dc8b6cb --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/00_SUGGESTED_ORDER.md @@ -0,0 +1,4 @@ +1. `parsing_arguments.js` +2. `what_does_fox_say.js` +3. `calculator.js` +4. `using_an_argument_parser_library.js` diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/README.md b/javascript-exercises/interacting_with_the_user/command_line_arguments/README.md new file mode 100644 index 0000000..c3658d8 --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/README.md @@ -0,0 +1,21 @@ +# Command Line Arguments + +When executing a program from the command line, most languages (including +JavaScript) provide a mechanism by which you can access the **arguments** +provided to the command. + +The arguments to a command are the words provided after the command itself. In +the following example, `node` is the command, the first argument is `script.js` +and the second argument is `foo`. + +```shell +$ node script.js foo # the $ is the shell prompt. Don't type it! # is a comment. +``` + +Solve the exercises to learn how to work with command line arguments in node.js. + +## Let's Get Started + +1. Review the [suggested order](00_SUGGESTED_ORDER.md). +2. Work through the exercises in the JavaScript files. +3. Submit a request for feedback! diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/calculator.js b/javascript-exercises/interacting_with_the_user/command_line_arguments/calculator.js new file mode 100644 index 0000000..ae3b1cd --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/calculator.js @@ -0,0 +1,44 @@ +// Let's do something a bit more interesting and useful with the input: we will +// write a rudimentary calculator that can solve basic arithmetic expressions. +// +// Complete the code required to make this calculator function as expected. +// +// Search suggestion: "convert string to number javascript" + +var expression = ____.____[____].split(' '); + +var firstNumber = ____(expression[0]); +var operator = expression[1]; +var secondNumber = ____(expression[2]); + +switch (operator) { + case ____: + console.log(firstNumber ____ secondNumber); + break; + case ____: + console.log(firstNumber ____ secondNumber); + break; + case ____: + console.log(firstNumber ____ secondNumber); + break; + case ____: + console.log(firstNumber ____ secondNumber); + break; + default: + console.log("ERROR. DOES NOT COMPUTE."); + break; +} + +// Run the following commands to verify that your code produces the output shown. +// +// $ node calculator.js "2 + 7" +// 9 +// +// $ node calculator.js "10 - 3" +// 7 +// +// $ node calculator.js "3 * 9" +// 27 +// +// $ node calculator.js "1 / 5" +// 0.2 diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/parsing_arguments.js b/javascript-exercises/interacting_with_the_user/command_line_arguments/parsing_arguments.js new file mode 100644 index 0000000..beb63fc --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/parsing_arguments.js @@ -0,0 +1,39 @@ +/* +// First use case: accessing command line arguments in node +console.log(process.argv); + +// Run the following commands (lines starting with the command prompt `$`) in +// your terminal to verify that your code produces the output shown. +// +// (The $ is the command prompt. Don't type it!) +// +// $ node parsing_arguments.js +// ["node", "/path/to/parsing_arguments.js"] +// +// $ node parsing_arguments.js one 2 three +// ["node", "/path/to/parsing_arguments.js", "one", "2", "three"] +*/ + + +/* +// Second use case: narrow down the results + +var filename = ____.____[____]; +var arguments = ____.____.slice(____); +console.log("The filename is " + filename); +console.log("The arguments are " + arguments.join(", ")); + +// Search suggestion: "select part of an array javascript" + +// Run the following commands to verify that your code produces the output shown. +// +// $ node parsing_arguments.js +// ["node", "/path/to/parsing_arguments.js"] +// The filename is /path/to/parsing_arguments.js +// The arguments are +// +// $ node parsing_arguments.js one 2 three +// ["node", "/path/to/parsing_arguments.js", "one", "2", "three"] +// The filename is /path/to/parsing_arguments.js +// The arguments are one, 2, three +*/ diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/using_an_argument_parser_library.js b/javascript-exercises/interacting_with_the_user/command_line_arguments/using_an_argument_parser_library.js new file mode 100644 index 0000000..520a1ed --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/using_an_argument_parser_library.js @@ -0,0 +1,33 @@ +// Parsing command line arguments is a common task that there are libraries you +// can use to do it for you. +// +// Use the minimist package to finish the following exercise. +// +// Docs: https://www.npmjs.org/package/minimist +// +// Note: The script will figure out how to handle messages passed with _either_ +// the `-m` or the `--message` flag. + +var parseArgs = require("minimist"); + +var args = parseArgs(____.____.slice(2)); + +var from = ____[____]; +var to = ____[____]; +var message = ____[____] || ____[____]; + +console.log("Sending message from " + from + " to " + to); +console.log("==== body ===="); +console.log(message); + +// Run the following commands to verify that your code produces the output shown. +// +// $ node args.js -f Bugs -t Elmer --message "What's up doc?" +// Sending message from Bugs to Elmer +// ==== body ==== +// What's up doc? +// +// $ node args.js -f "Apollo 13" -t Houston -m "We have a problem." +// Sending message from Apollo 13 to Houston +// ==== body ==== +// We have a problem. diff --git a/javascript-exercises/interacting_with_the_user/command_line_arguments/what_does_fox_say.js b/javascript-exercises/interacting_with_the_user/command_line_arguments/what_does_fox_say.js new file mode 100644 index 0000000..c262711 --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/command_line_arguments/what_does_fox_say.js @@ -0,0 +1,30 @@ +// Often times command line arguments are used to determine which code to run. +// +// Complete the conditional so it responds to each command line argument +// appropriately. + +var animal = ____.____[____]; + +if (animal === ____) { + console.log(____); +} else if (animal === ____) { + console.log(____); +} else if (animal === ____) { + console.log(____); +} else if (animal === ____) { + console.log(____); +} + +// Run the following commands to verify that your code produces the output shown. +// +// $ node what_does_fox_say.js cow +// mooooooo +// +// $ node what_does_fox_say.js dog +// woof +// +// $ node what_does_fox_say.js fish +// >() >() >() +// +// $ node what_does_fox_say.js fox +// ??? diff --git a/javascript-exercises/interacting_with_the_user/manipulating_the_dom/README.md b/javascript-exercises/interacting_with_the_user/manipulating_the_dom/README.md new file mode 100644 index 0000000..a9504fc --- /dev/null +++ b/javascript-exercises/interacting_with_the_user/manipulating_the_dom/README.md @@ -0,0 +1,381 @@ +# Manipulating the DOM + +Once you've learned how to [query the DOM](../querying_the_dom) to select +elements from your web page, the next question is: what can you do with those +elements? + +When we write JavaScript, most of the time we will be writing programs that +affect the DOM, and thus, the user interface. There are many ways to do this. A +small sampling: + +- Show a chat window on the page when a user starts a new chat session +- Zoom in on the map when a user clicks on the "+" button +- When a user selects a preferred airline, highlight matching flights in yellow + +The examples are as numerous as the stars. Just know that anytime the content of +a web page changes, that is JavaScript doing its work. + +In these exercises, we are going to explore some of the common ways to +manipulate the DOM at the level of individual elements. + +As a reference, we will be using the documentation for the +[HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement) API +as well as some of the objects it inherits (i.e. shares) the properties and +methods of: [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) +and [Node](https://developer.mozilla.org/en-US/docs/Web/API/Node). + +## Exercises + +To complete these exercises, you will need two files: + +- `page.html` +- `my_script.js` + +Create a file called `page.html` and paste the following code into it: + +```html + + + +

      Smoothie Time

      + +

      + Everyone likes smoothies. My favorite is blueberry. +

      + +

      A list of fruits

      + + + +

      + Sign up for our email newsletter. +

      + +
      + + + +
      + + + + +``` + +**You do not need to change this page.** Your exercise code will go in +`my_script.js`. Make sure to create this file in the same directory as +`page.html`. + +Notice that ` where you are. +

      + + +``` + +Search suggestion: `javascript write text to document` + +> What's that `new Date().toString()` code? That is a how, in JavaScript, we can +> say "get me the current date and time, and then convert it to a String". + +### Think global, act local + +So that last example left us with a lot of JavaScript in one ` + + +

      What time is it?

      + +

      + The time is where you are. +

      + + +``` + +Search suggestion: `javascript store value in variable` + +> Even though the JavaScript in this example is split between two ` + + +

      What time is it?

      + +

      + The time is where you are. +

      + + +``` + +Search suggestion: `how to load external javascript file in html` + +### Importing scripts from anywhere + +If we can load our own scripts from other files, why not load scripts that other +people have written? For example, what about a third-party library like +`jQuery`? + +Can you figure out how to import the [jQuery library](http://jquery.com/) into +the page? Once you get it to work, you will see an alert appear. + +`page.html`: + +```html + + + + + +

      Waiting for jQuery...

      + + + +``` + +Search suggestion: `import jquery` + +> As you dig around, you may notice that there is more than one way to import +> jQuery (or any other library, for that matter). Because the `src` attribute of +> a ` + + + + +``` + +`my_script.js`: + +```javascript +// The jQuery variable is defined by the jQuery library. If the library is +// loaded, this variable will evaluate to a truthy value. +if (jQuery) { + alert("We have jQuery!"); +} +``` + +Search suggestion: `should script tags be in head or body` + +> Remember: the browser reads HTML from top to bottom. When it encounters a +> ` + + + + + + + + + + + + + + + + + + + + + + + diff --git a/javascript-koans/LICENSE b/javascript-koans/LICENSE new file mode 100644 index 0000000..199f991 --- /dev/null +++ b/javascript-koans/LICENSE @@ -0,0 +1,24 @@ +The MIT License (MIT) + +Copyright (c) 2010-2014 David Laing and Greg Malcolm + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/javascript-koans/README.markdown b/javascript-koans/README.markdown new file mode 100644 index 0000000..7364ef2 --- /dev/null +++ b/javascript-koans/README.markdown @@ -0,0 +1,40 @@ +# javascript-koans +Based on Edgecase's fantastic +[Ruby koans](http://github.com/edgecase/ruby_koans), the goal of the +Javascript koans is to teach you Javascript programming through +testing. + +When you first run the koans, you'll be presented with a runtime error and a +stack trace indicating where the error occurred. Your goal is to make the +error go away. As you fix each error, you should learn something about the +Javascript language and functional programming in general. + +Your journey towards Javascript enlightenment starts in the koans/AboutExpects.js file. These +koans will be very simple, so don't overthink them! As you progress through +more koans, more and more Javascript syntax will be introduced which will allow +you to solve more complicated problems and use more advanced techniques. + +## Running the Koans +Simply navigate to the Javascript Koans folder using a file browser, and +double click on KoansRunnner.html. + +Any browser will do, but for the best results Firefox or Chrome is +recommended. More stack trace information shows up for javascript on these +browsers. + +The first error will be in koans/AboutExpects.js. Fix the first test and +refresh the browser. Rinse and repeat until all tests turn green. + +The test runner used is [Jasmine](http://jasmine.github.io/) with a customized report viewer. + +### Changelog +* v3 - Nov 2010 - Moved out of branch of functional-koans project, into own top level project +* v2 - Sept 2010 - Second version based on jasmine (Thanks Greg Malcolm!) +* v1 - July 2010 - First version based on jsTestDriver + +### Acknowledgements +* Dick Wall (the Java posse) - for bringing the idea of koans to my attention +* Edgecase - for the great Ruby Koans +* Douglas Crockford - for Javascript; the good bits + +### [MIT Licensed](LICENSE) diff --git a/javascript-koans/jasmine/runner_specs/TestJSKoansRunner.html b/javascript-koans/jasmine/runner_specs/TestJSKoansRunner.html new file mode 100644 index 0000000..06885ad --- /dev/null +++ b/javascript-koans/jasmine/runner_specs/TestJSKoansRunner.html @@ -0,0 +1,24 @@ + + + + Jasmine Test Runner + + + + + + + + + + + + + + + + diff --git a/javascript-koans/jasmine/runner_specs/suites/KoansRunnerSpec.js b/javascript-koans/jasmine/runner_specs/suites/KoansRunnerSpec.js new file mode 100644 index 0000000..9a838c3 --- /dev/null +++ b/javascript-koans/jasmine/runner_specs/suites/KoansRunnerSpec.js @@ -0,0 +1,342 @@ +describe("KoansRunner", function() { + var env; + var reporter; + var body; + var fakeDocument; + + beforeEach(function() { + env = new jasmine.Env(); + env.updateInterval = 0; + + body = document.createElement("body"); + fakeDocument = { body: body, location: { search: "" } }; + reporter = new JsKoansReporter(fakeDocument); + }); + + function fakeSpec(name) { + return { + getFullName: function() { + return name; + } + }; + } + + function fakeSuite(desc) { + return { + parentSuite: null, + description: desc + }; + } + + function findElements(divs, withClass) { + var els = []; + for (var i = 0; i < divs.length; i++) { + if (divs[i].className == withClass) els.push(divs[i]); + } + return els; + } + + function findElement(divs, withClass) { + var els = findElements(divs, withClass); + if (els.length > 0) return els[0]; + throw new Error("couldn't find div with class " + withClass); + } + + it("should run only specs beginning with spec parameter", function() { + fakeDocument.location.search = "?spec=run%20this"; + expect(reporter.specFilter(fakeSpec("run this"))).toBeTruthy(); + expect(reporter.specFilter(fakeSpec("not the right spec"))).toBeFalsy(); + expect(reporter.specFilter(fakeSpec("not run this"))).toBeFalsy(); + }); + + it("should display empty divs for every suite when the runner is starting", function() { + reporter.reportRunnerStarting({ + env: env, + suites: function() { + return [ new jasmine.Suite({}, "suite 1", null, null) ]; + } + }); + + var divs = findElements(body.getElementsByTagName("div"), "suite"); + expect(divs.length).toEqual(1); + expect(divs[0].innerHTML).toContain("suite 1"); + }); + + describe('Matcher reporting', function () { + var getErrorMessageDiv = function (body) { + var divs = body.getElementsByTagName("div"); + for (var i = 0; i < divs.length; i++) { + if (divs[i].className.match(/errorMessage/)) { + return divs[i]; + } + } + }; + + var runner, spec, fakeTimer; + beforeEach(function () { + fakeTimer = new jasmine.FakeTimer(); + env.setTimeout = fakeTimer.setTimeout; + env.clearTimeout = fakeTimer.clearTimeout; + env.setInterval = fakeTimer.setInterval; + env.clearInterval = fakeTimer.clearInterval; + runner = env.currentRunner(); + var suite = new jasmine.Suite(env, 'some suite'); + runner.add(suite); + spec = new jasmine.Spec(env, suite, 'some spec'); + suite.add(spec); + fakeDocument.location.search = "?"; + env.addReporter(reporter); + }); + + describe('toContain', function () { + it('should show actual and expected', function () { + spec.runs(function () { + this.expect('foo').toContain('bar'); + }); + runner.execute(); + fakeTimer.tick(0); + + var resultEl = getErrorMessageDiv(body); + expect(resultEl.innerHTML).toMatch(/foo/); + expect(resultEl.innerHTML).toMatch(/bar/); + }); + }); + }); + + + describe("failure messages (integration)", function () { + var spec, results, expectationResult; + + beforeEach(function() { + results = { + passed: function() { + return false; + }, + getItems: function() { + }}; + + var suite1 = new jasmine.Suite(env, "suite 1", null, null); + + spec = { + suite: suite1, + getFullName: function() { + return "foo"; + }, + results: function() { + return results; + } + }; + + reporter.reportRunnerStarting({ + env: env, + suites: function() { + return [ suite1 ]; + } + }); + }); + + it("should add the failure message to the DOM (non-toEquals matchers)", function() { + expectationResult = new jasmine.ExpectationResult({ + matcherName: "toBeNull", passed: false, message: "Expected 'a' to be null, but it was not" + }); + + spyOn(results, 'getItems').andReturn([expectationResult]); + + reporter.reportSpecResults(spec); + + var divs = body.getElementsByTagName("div"); + var errorDiv = findElement(divs, 'errorMessage'); + expect(errorDiv.innerHTML).toEqual("Expected 'a' to be null, but it was not"); + }); + + it("should add the failure message to the DOM (non-toEquals matchers) html escaping", function() { + expectationResult = new jasmine.ExpectationResult({ + matcherName: "toBeNull", passed: false, message: "Expected '1 < 2' to e null, & it was not" + }); + + spyOn(results, 'getItems').andReturn([expectationResult]); + + reporter.reportSpecResults(spec); + + var divs = body.getElementsByTagName("div"); + var errorDiv = findElement(divs, 'errorMessage'); + expect(errorDiv.innerHTML).toEqual("Expected '1 < 2' to <b>e null, & it was not"); + }); + }); + + describe("duplicate example names", function() { + it("should report failures correctly", function() { + var suite1 = env.describe("suite", function() { + env.it("will have log messages", function() { + this.log("this one passes!"); + this.expect(true).toBeTruthy(); + }); + }); + + var suite2 = env.describe("suite", function() { + env.it("will have log messages", function() { + this.log("this one fails!"); + this.expect(true).toBeFalsy(); + }); + }); + + env.addReporter(reporter); + env.execute(); + + var divs = body.getElementsByTagName("div"); + var failedSpecDiv = findElement(divs, 'suite failed'); + expect(failedSpecDiv.className).toEqual('suite failed'); + expect(failedSpecDiv.innerHTML).toContain("damaging your karma"); + expect(failedSpecDiv.innerHTML).not.toContain("has expanded your awareness"); + + var passedSpecDiv = findElement(divs, 'suite passed'); + expect(passedSpecDiv.className).toEqual('suite passed'); + expect(passedSpecDiv.innerHTML).toContain("has expanded your awareness"); + expect(passedSpecDiv.innerHTML).not.toContain("damaging your karma"); + }); + }); + + describe('#reportSpecStarting', function() { + var spec1; + beforeEach(function () { + env.describe("suite 1", function() { + spec1 = env.it("spec 1", function() { + }); + }); + }); + + it('should not log running specs by default', function() { + spyOn(reporter, 'log'); + + reporter.reportSpecStarting(spec1); + + expect(reporter.log).not.toHaveBeenCalled(); + }); + }); + + describe('showing progress', function() { + beforeEach(function() { + env.describe("suite 1", function() { + env.it("spec A"); + env.it("spec B"); + }); + env.describe("suite 2", function() { + env.it("spec C"); + }); + }); + + describe('subjects', function() { + describe("with no failures", function() { + beforeEach(function() { + env.addReporter(reporter); + env.execute(); + }); + + it("should have 2 subjects", function() { + expect(reporter.noOfSubjects).toBe(2); + }); + + it("should not have any failed subjects", function() { + expect(reporter.failedSubjects).toBe(0); + }); + }); + + describe("with 1 failure", function() { + beforeEach(function() { + env.describe("suite with error", function() { + env.it("spec X", function() { + expect(true).toBeFalsey(); + }); + env.it("spec Y", function() { + expect(true).toBeFalsey(); + }); + }); + + env.addReporter(reporter); + env.execute(); + }); + + it("should have 3 subjects", function() { + expect(reporter.noOfSubjects).toBe(3); + }); + + it("should have a failure", function() { + expect(reporter.failedSubjects).toBe(1); + }); + }); + + describe("with 2 failures", function() { + beforeEach(function() { + env.describe("suite with error", function() { + env.it("spec X", function() { + expect(true).toBeFalsey(); + }); + }); + env.describe("suite with error too", function() { + env.it("spec Y", function() { + expect(true).toBeFalsey(); + }); + }); + + env.addReporter(reporter); + env.execute(); + }); + + it("should have 4 subjects", function() { + expect(reporter.noOfSubjects).toBe(4); + }); + + it("should have 2 failures", function() { + expect(reporter.failedSubjects).toBe(2); + }); + }); + + describe("with embedded suites only outer suites count as subjects", function() { + beforeEach(function() { + env.describe("outer suite", function() { + env.it("spec for outer suite", function() { + expect(true).toBeFalsey(); + }); + + env.describe("inner suite", function() { + env.it("spec for inner suite", function() { + expect(true).toBeFalsey(); + }); + }); + }); + + env.addReporter(reporter); + env.execute(); + }); + + it("should have 3 suites", function() { + expect(reporter.noOfSubjects).toBe(3); + }); + + it("should have 1 failure", function() { + expect(reporter.failedSubjects).toBe(1); + }); + + }); + }); + }); + + describe("presentation", function() { + describe("showing the suite description", function() { + it("should prefix outer suite descriptions with 'Thinking'", function() { + suite = fakeSuite("About Pies"); + description = reporter.getSuiteDescription(suite); + + expect(description).toEqual("Thinking About Pies"); + }); + + it("should prefix inner suite descriptions with 'Thinking'", function() { + suite = fakeSuite("cherries"); + suite.parentSuite = "Something"; + description = reporter.getSuiteDescription(suite); + + expect(description).toEqual("Considering cherries"); + }); + }); + }); +}); diff --git a/javascript-koans/koans/AboutApplyingWhatWeHaveLearnt.js b/javascript-koans/koans/AboutApplyingWhatWeHaveLearnt.js new file mode 100644 index 0000000..eccc937 --- /dev/null +++ b/javascript-koans/koans/AboutApplyingWhatWeHaveLearnt.js @@ -0,0 +1,113 @@ +var _; //globals + +describe("About Applying What We Have Learnt", function() { + + var products; + + beforeEach(function () { + products = [ + { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false }, + { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false }, + { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false }, + { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true }, + { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true } + ]; + }); + + /*********************************************************************************/ + + it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (imperative)", function () { + + var i,j,hasMushrooms, productsICanEat = []; + + for (i = 0; i < products.length; i+=1) { + if (products[i].containsNuts === false) { + hasMushrooms = false; + for (j = 0; j < products[i].ingredients.length; j+=1) { + if (products[i].ingredients[j] === "mushrooms") { + hasMushrooms = true; + } + } + if (!hasMushrooms) productsICanEat.push(products[i]); + } + } + + expect(productsICanEat.length).toBe(FILL_ME_IN); + }); + + it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () { + + var productsICanEat = []; + + /* solve using filter() & all() / any() */ + + expect(productsICanEat.length).toBe(FILL_ME_IN); + }); + + /*********************************************************************************/ + + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (imperative)", function () { + + var sum = 0; + for(var i=1; i<1000; i+=1) { + if (i % 3 === 0 || i % 5 === 0) { + sum += i; + } + } + + expect(sum).toBe(FILL_ME_IN); + }); + + it("should add all the natural numbers below 1000 that are multiples of 3 or 5 (functional)", function () { + + var sum = FILL_ME_IN; /* try chaining range() and reduce() */ + + expect(233168).toBe(FILL_ME_IN); + }); + + /*********************************************************************************/ + it("should count the ingredient occurrence (imperative)", function () { + var ingredientCount = { "{ingredient name}": 0 }; + + for (i = 0; i < products.length; i+=1) { + for (j = 0; j < products[i].ingredients.length; j+=1) { + ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1; + } + } + + expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); + }); + + it("should count the ingredient occurrence (functional)", function () { + var ingredientCount = { "{ingredient name}": 0 }; + + /* chain() together map(), flatten() and reduce() */ + + expect(ingredientCount['mushrooms']).toBe(FILL_ME_IN); + }); + + /*********************************************************************************/ + /* UNCOMMENT FOR EXTRA CREDIT */ + /* + it("should find the largest prime factor of a composite number", function () { + + }); + + it("should find the largest palindrome made from the product of two 3 digit numbers", function () { + + }); + + it("should find the smallest number divisible by each of the numbers 1 to 20", function () { + + + }); + + it("should find the difference between the sum of the squares and the square of the sums", function () { + + }); + + it("should find the 10001st prime", function () { + + }); + */ +}); diff --git a/javascript-koans/koans/AboutArrays.js b/javascript-koans/koans/AboutArrays.js new file mode 100644 index 0000000..543d7df --- /dev/null +++ b/javascript-koans/koans/AboutArrays.js @@ -0,0 +1,97 @@ +describe("About Arrays", function() { + + //We shall contemplate truth by testing reality, via spec expectations. + it("should create arrays", function() { + var emptyArray = []; + expect(typeof(emptyArray)).toBe("object"); //A mistake? - http://javascript.crockford.com/remedial.html + expect(emptyArray.length).toBe(0); + + var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]]; + expect(multiTypeArray[0]).toBe(0); + expect(multiTypeArray[2]).toBe("two"); + expect(multiTypeArray[3]()).toBe(3); + expect(multiTypeArray[4].value1).toBe(4); + expect(multiTypeArray[4]["value2"]).toBe(5); + expect(multiTypeArray[5][0]).toBe(6); + }); + + it("should understand array literals", function () { + var array = []; + expect(array).toEqual([]); + + array[0] = 1; + expect(array).toEqual([1]); + + array[1] = 2; + expect(array).toEqual([1,2]); + + array.push(3); + expect(array).toEqual([1,2,3]); + }); + + it("should understand array length", function () { + var fourNumberArray = [1, 2, 3, 4]; + + expect(fourNumberArray.length).toBe(4); + fourNumberArray.push(5, 6); + expect(fourNumberArray.length).toBe(6); + + var tenEmptyElementArray = new Array(10); + expect(tenEmptyElementArray.length).toBe(10); + + tenEmptyElementArray.length = 10; + expect(tenEmptyElementArray.length).toBe(10); + }); + + it("should slice arrays", function () { + var array = ["peanut", "butter", "and", "jelly"]; + + expect(array.slice(0, 1)).toEqual(['peanut']); + expect(array.slice(0, 2)).toEqual(['peanut' , 'butter']); + expect(array.slice(2, 2)).toEqual([]); + expect(array.slice(2, 20)).toEqual(['and' , 'jelly']); + expect(array.slice(3, 0)).toEqual([]); + expect(array.slice(3, 100)).toEqual(['jelly']); + expect(array.slice(5, 1)).toEqual([]); + }); + + it("should know array references", function () { + var array = [ "zero", "one", "two", "three", "four", "five" ]; + + function passedByReference(refArray) { + refArray[1] = "one"; + } + passedByReference(array); + expect(array[1]).toBe("one"); + + var assignedArray = array; + assignedArray[5] = "changed in assignedArray"; + expect(array[5]).toBe("changed in assignedArray"); + + var copyOfArray = array.slice(); + copyOfArray[3] = "changed in copyOfArray"; + expect(array[3]).toBe("three"); + }); + + it("should push and pop", function () { + var array = [1, 2]; + array.push(3); + + expect(array).toEqual([1,2,3]); + + var poppedValue = array.pop(); + expect(poppedValue).toBe(3); + expect(array).toEqual([1,2]); + }); + + it("should know about shifting arrays", function () { + var array = [1, 2]; + + array.unshift(3); + expect(array).toEqual([3,1,2]); + + var shiftedValue = array.shift(); + expect(shiftedValue).toEqual(3); + expect(array).toEqual([1,2]); + }); +}); diff --git a/javascript-koans/koans/AboutExpects.js b/javascript-koans/koans/AboutExpects.js new file mode 100644 index 0000000..2683255 --- /dev/null +++ b/javascript-koans/koans/AboutExpects.js @@ -0,0 +1,40 @@ +describe('About Expects', function() { + + // We shall contemplate truth by testing reality, via spec expectations. + it('should expect true', function() { + + // Your journey begins here: Replace the word false with true + expect(true).toBeTruthy(); + }); + + // To understand reality, we must compare our expectations against reality. + it('should expect equality', function() { + var expectedValue = 2; + var actualValue = 1 + 1; + + expect(actualValue === expectedValue).toBeTruthy(); + }); + + // Some ways of asserting equality are better than others. + it('should assert equality a better way', function() { + var expectedValue = 2; + var actualValue = 1 + 1; + + // toEqual() compares using common sense equality. + expect(actualValue).toEqual(expectedValue); + }); + + // Sometimes you need to be precise about what you "type." + it('should assert equality with ===', function() { + var expectedValue = 2; + var actualValue = (1 + 1); + + // toBe() will always use === to compare. + expect(actualValue).toBe(expectedValue); + }); + + // Sometimes we will ask you to fill in the values. + it('should have filled in values', function() { + expect(1 + 1).toEqual(2); + }); +}); diff --git a/javascript-koans/koans/AboutFunctions.js b/javascript-koans/koans/AboutFunctions.js new file mode 100644 index 0000000..0e78820 --- /dev/null +++ b/javascript-koans/koans/AboutFunctions.js @@ -0,0 +1,100 @@ +describe("About Functions", function() { + + it("should declare functions", function() { + + function add(a, b) { + return a + b; + } + + expect(add(1, 2)).toBe(3); + }); + + it("should know internal variables override outer variables", function () { + var message = "Outer"; + + function getMessage() { + return "Outer"; + } + + function overrideMessage() { + var message = "Inner"; + return "Inner"; + } + + expect(getMessage()).toBe("Outer"); + expect(overrideMessage()).toBe("Inner"); + expect(message).toBe("Outer"); + }); + + it("should have lexical scoping", function () { + var variable = "top-level"; + function parentfunction() { + var variable = "local"; + function childfunction() { + return variable; + } + return childfunction(); + } + expect(parentfunction()).toBe("local"); + }); + + it("should use lexical scoping to synthesise functions", function () { + + function makeMysteryFunction(makerValue) + { + var newFunction = function doMysteriousThing(param) + { + return makerValue + param; + }; + return newFunction; + } + + var mysteryFunction3 = makeMysteryFunction(3); + var mysteryFunction5 = makeMysteryFunction(5); + + expect(mysteryFunction3(10) + mysteryFunction5(5)).toBe(23); + }); + + it("should allow extra function arguments", function () { + + function returnFirstArg(firstArg) { + return firstArg; + } + + expect(returnFirstArg("first", "second", "third")).toBe("first"); + + function returnSecondArg(firstArg, secondArg) { + return secondArg; + } + + expect(returnSecondArg("only give first arg")).toBe("first"); + + function returnAllArgs() { + var argsArray = []; + for (var i = 0; i < arguments.length; i += 1) { + argsArray.push(arguments[i]); + } + return argsArray.join(","); + } + + expect(returnAllArgs("first", "second", "third")).toBe("first , second, third"); + }); + + it("should pass functions as values", function () { + + var appendRules = function (name) { + return name + " rules!"; + }; + + var appendDoubleRules = function (name) { + return name + " totally rules!"; + }; + + var praiseSinger = { givePraise: appendRules }; + expect(praiseSinger.givePraise("John")).toBe(praiseSinger + " rules!"); + + praiseSinger.givePraise = appendDoubleRules; + expect(praiseSinger.givePraise("Mary")).toBe(praiseSinger + " totally rules!"); + + }); +}); diff --git a/javascript-koans/koans/AboutHigherOrderFunctions.js b/javascript-koans/koans/AboutHigherOrderFunctions.js new file mode 100644 index 0000000..07c4167 --- /dev/null +++ b/javascript-koans/koans/AboutHigherOrderFunctions.js @@ -0,0 +1,90 @@ +var _; //globals + +/* This section uses a functional extension known as Underscore.js - http://documentcloud.github.com/underscore/ + "Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support + that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. + It's the tie to go along with jQuery's tux." + */ +describe("About Higher Order Functions", function () { + + it("should use filter to return array items that meet a criteria", function () { + var numbers = [1,2,3]; + var odd = _(numbers).filter(function (x) { return x % 2 !== 0 }); + + expect(odd).toEqual(FILL_ME_IN); + expect(odd.length).toBe(FILL_ME_IN); + expect(numbers.length).toBe(FILL_ME_IN); + }); + + it("should use 'map' to transform each element", function () { + var numbers = [1, 2, 3]; + var numbersPlus1 = _(numbers).map(function(x) { return x + 1 }); + + expect(numbersPlus1).toEqual(FILL_ME_IN); + expect(numbers).toEqual(FILL_ME_IN); + }); + + it("should use 'reduce' to update the same result on each iteration", function () { + var numbers = [1, 2, 3]; + var reduction = _(numbers).reduce( + function(/* result from last call */ memo, /* current */ x) { return memo + x }, /* initial */ 0); + + expect(reduction).toBe(FILL_ME_IN); + expect(numbers).toEqual(FILL_ME_IN); + }); + + it("should use 'forEach' for simple iteration", function () { + var numbers = [1,2,3]; + var msg = ""; + var isEven = function (item) { + msg += (item % 2) === 0; + }; + + _(numbers).forEach(isEven); + + expect(msg).toEqual(FILL_ME_IN); + expect(numbers).toEqual(FILL_ME_IN); + }); + + it("should use 'all' to test whether all items pass condition", function () { + var onlyEven = [2,4,6]; + var mixedBag = [2,4,5,6]; + + var isEven = function(x) { return x % 2 === 0 }; + + expect(_(onlyEven).all(isEven)).toBe(FILL_ME_IN); + expect(_(mixedBag).all(isEven)).toBe(FILL_ME_IN); + }); + + it("should use 'any' to test if any items passes condition" , function () { + var onlyEven = [2,4,6]; + var mixedBag = [2,4,5,6]; + + var isEven = function(x) { return x % 2 === 0 }; + + expect(_(onlyEven).any(isEven)).toBe(FILL_ME_IN); + expect(_(mixedBag).any(isEven)).toBe(FILL_ME_IN); + }); + + it("should use range to generate an array", function() { + expect(_.range(3)).toEqual(FILL_ME_IN); + expect(_.range(1, 4)).toEqual(FILL_ME_IN); + expect(_.range(0, -4, -1)).toEqual(FILL_ME_IN); + }); + + it("should use flatten to make nested arrays easy to work with", function() { + expect(_([ [1, 2], [3, 4] ]).flatten()).toEqual(FILL_ME_IN); + }); + + it("should use chain() ... .value() to use multiple higher order functions", function() { + var result = _([ [0, 1], 2 ]).chain() + .flatten() + .map(function(x) { return x+1 } ) + .reduce(function (sum, x) { return sum + x }) + .value(); + + expect(result).toEqual(FILL_ME_IN); + }); + +}); + diff --git a/javascript-koans/koans/AboutInheritance.js b/javascript-koans/koans/AboutInheritance.js new file mode 100644 index 0000000..5dba545 --- /dev/null +++ b/javascript-koans/koans/AboutInheritance.js @@ -0,0 +1,85 @@ +function Muppet(age, hobby) { + this.age = age; + this.hobby = hobby; + + this.answerNanny = function(){ + return "Everything's cool!"; + } +} + +function SwedishChef(age, hobby, mood) { + Muppet.call(this, age, hobby); + this.mood = mood; + + this.cook = function() { + return "Mmmm soup!"; + } +} + +SwedishChef.prototype = new Muppet(); + +describe("About inheritance", function() { + beforeEach(function(){ + this.muppet = new Muppet(2, "coding"); + this.swedishChef = new SwedishChef(2, "cooking", "chillin"); + }); + + it("should be able to call a method on the derived object", function() { + expect(this.swedishChef.cook()).toEqual(FILL_ME_IN); + }); + + it("should be able to call a method on the base object", function() { + expect(this.swedishChef.answerNanny()).toEqual(FILL_ME_IN); + }); + + it("should set constructor parameters on the base object", function() { + expect(this.swedishChef.age).toEqual(FILL_ME_IN); + expect(this.swedishChef.hobby).toEqual(FILL_ME_IN); + }); + + it("should set constructor parameters on the derived object", function() { + expect(this.swedishChef.mood).toEqual(FILL_ME_IN); + }); +}); + +// http://javascript.crockford.com/prototypal.html +Object.prototype.beget = function () { + function F() {} + F.prototype = this; + return new F(); +} + +function Gonzo(age, hobby, trick) { + Muppet.call(this, age, hobby); + this.trick = trick; + + this.doTrick = function() { + return this.trick; + } +} + +//no longer need to call the Muppet (base type) constructor +Gonzo.prototype = Muppet.prototype.beget(); + +describe("About Crockford's inheritance improvement", function() { + beforeEach(function(){ + this.gonzo = new Gonzo(3, "daredevil performer", "eat a tire"); + }); + + it("should be able to call a method on the derived object", function() { + expect(this.gonzo.doTrick()).toEqual(FILL_ME_IN); + }); + + it("should be able to call a method on the base object", function() { + expect(this.gonzo.answerNanny()).toEqual(FILL_ME_IN); + }); + + it("should set constructor parameters on the base object", function() { + expect(this.gonzo.age).toEqual(FILL_ME_IN); + expect(this.gonzo.hobby).toEqual(FILL_ME_IN); + }); + + it("should set constructor parameters on the derived object", function() { + expect(this.gonzo.trick).toEqual(FILL_ME_IN); + }); +}); diff --git a/javascript-koans/koans/AboutMutability.js b/javascript-koans/koans/AboutMutability.js new file mode 100644 index 0000000..fd9b69a --- /dev/null +++ b/javascript-koans/koans/AboutMutability.js @@ -0,0 +1,68 @@ +describe("About Mutability", function() { + + it("should expect object properties to be public and mutable", function () { + var aPerson = {firstname: "John", lastname: "Smith" }; + aPerson.firstname = "Alan"; + + expect(aPerson.firstname).toBe(FILL_ME_IN); + }); + + it("should understand that constructed properties are public and mutable", function () { + function Person(firstname, lastname) + { + this.firstname = firstname; + this.lastname = lastname; + } + var aPerson = new Person ("John", "Smith"); + aPerson.firstname = "Alan"; + + expect(aPerson.firstname).toBe(FILL_ME_IN); + }); + + it("should expect prototype properties to be public and mutable", function () { + function Person(firstname, lastname) + { + this.firstname = firstname; + this.lastname = lastname; + } + Person.prototype.getFullName = function () { + return this.firstname + " " + this.lastname; + }; + + var aPerson = new Person ("John", "Smith"); + expect(aPerson.getFullName()).toBe(FILL_ME_IN); + + aPerson.getFullName = function () { + return this.lastname + ", " + this.firstname; + }; + + expect(aPerson.getFullName()).toBe(FILL_ME_IN); + }); + + it("should know that variables inside a constructor and constructor args are private", function () { + function Person(firstname, lastname) + { + var fullName = firstname + " " + lastname; + + this.getFirstName = function () { return firstname; }; + this.getLastName = function () { return lastname; }; + this.getFullName = function () { return fullName; }; + } + var aPerson = new Person ("John", "Smith"); + + aPerson.firstname = "Penny"; + aPerson.lastname = "Andrews"; + aPerson.fullName = "Penny Andrews"; + + expect(aPerson.getFirstName()).toBe(FILL_ME_IN); + expect(aPerson.getLastName()).toBe(FILL_ME_IN); + expect(aPerson.getFullName()).toBe(FILL_ME_IN); + + aPerson.getFullName = function () { + return aPerson.lastname + ", " + aPerson.firstname; + }; + + expect(aPerson.getFullName()).toBe(FILL_ME_IN); + }); + +}); diff --git a/javascript-koans/koans/AboutObjects.js b/javascript-koans/koans/AboutObjects.js new file mode 100644 index 0000000..4d4b00c --- /dev/null +++ b/javascript-koans/koans/AboutObjects.js @@ -0,0 +1,109 @@ +describe("About Objects", function () { + + describe("Properties", function () { + var megalomaniac = {}; + + beforeEach(function () { + megalomaniac = { mastermind: "Joker", henchwoman: "Harley" }; + }); + + it("should confirm objects are collections of properties", function () { + expect(megalomaniac.mastermind).toBe("Joker"); + }); + + it("should confirm that properties are case sensitive", function () { + expect(megalomaniac.henchwoman).toBe("Harley"); + expect(megalomaniac.henchWoman).toBe("Harley"); + }); + }); + + + it("should know properties that are functions act like methods", function () { + var megalomaniac = { + mastermind : "Brain", + henchman: "Pinky", + battleCry: function (noOfBrains) { + return "They are " + this.henchman + " and the" + + Array(noOfBrains + 1).join(" " + this.mastermind); + } + }; + + var battleCry = megalomaniac.battleCry(4); + expect(FILL_ME_IN).toMatch(battleCry); + }); + + it("should confirm that when a function is attached to an object, 'this' refers to the object", function () { + var currentDate = new Date(); + var currentYear = (currentDate.getFullYear()); + var megalomaniac = { + mastermind: "James Wood", + henchman: "Adam West", + birthYear: 1970, + calculateAge: function () { + return currentYear - this.birthYear; + } + }; + + expect(currentYear).toBe(FILL_ME_IN); + expect(megalomaniac.calculateAge()).toBe(FILL_ME_IN); + }); + + describe("'in' keyword", function () { + var megalomaniac; + beforeEach(function () { + megalomaniac = { + mastermind: "The Monarch", + henchwoman: "Dr Girlfriend", + theBomb: true + }; + }); + + it("should have the bomb", function () { + + var hasBomb = "theBomb" in megalomaniac; + + expect(hasBomb).toBe(FILL_ME_IN); + }); + + it("should not have the detonator however", function () { + + var hasDetonator = "theDetonator" in megalomaniac; + + expect(hasDetonator).toBe(FILL_ME_IN); + }); + }); + + it("should know that properties can be added and deleted", function () { + var megalomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" }; + + expect("secretary" in megalomaniac).toBe(FILL_ME_IN); + + megalomaniac.secretary = "Agent Smith"; + expect("secretary" in megalomaniac).toBe(FILL_ME_IN); + + delete megalomaniac.henchman; + expect("henchman" in megalomaniac).toBe(FILL_ME_IN); + }); + + + it("should use prototype to add to all objects", function () { + function Circle(radius) + { + this.radius = radius; + } + + var simpleCircle = new Circle(10); + var colouredCircle = new Circle(5); + colouredCircle.colour = "red"; + + expect(simpleCircle.colour).toBe(FILL_ME_IN); + expect(colouredCircle.colour).toBe(FILL_ME_IN); + + Circle.prototype.describe = function () { + return "This circle has a radius of: " + this.radius; + }; + + expect(simpleCircle.describe()).toBe(FILL_ME_IN); + expect(colouredCircle.describe()).toBe(FILL_ME_IN); + }); +}); diff --git a/javascript-koans/lib/FILL_ME_IN.js b/javascript-koans/lib/FILL_ME_IN.js new file mode 100644 index 0000000..e2f25c2 --- /dev/null +++ b/javascript-koans/lib/FILL_ME_IN.js @@ -0,0 +1 @@ +var FILL_ME_IN = 23; diff --git a/javascript-koans/lib/jasmine/jasmine-html.js b/javascript-koans/lib/jasmine/jasmine-html.js new file mode 100644 index 0000000..b405821 --- /dev/null +++ b/javascript-koans/lib/jasmine/jasmine-html.js @@ -0,0 +1,182 @@ +jasmine.TrivialReporter = function(doc) { + this.document = doc || document; + this.suiteDivs = {}; + this.logRunningSpecs = false; +}; + +jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) { + var el = document.createElement(type); + + for (var i = 2; i < arguments.length; i++) { + var child = arguments[i]; + + if (typeof child === 'string') { + el.appendChild(document.createTextNode(child)); + } else { + if (child) { el.appendChild(child); } + } + } + + for (var attr in attrs) { + if (attr == "className") { + el[attr] = attrs[attr]; + } else { + el.setAttribute(attr, attrs[attr]); + } + } + + return el; +}; + +jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) { + var showPassed, showSkipped; + + this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' }, + this.createDom('div', { className: 'banner' }, + this.createDom('div', { className: 'logo' }, + "Jasmine", + this.createDom('span', { className: 'version' }, runner.env.versionString())), + this.createDom('div', { className: 'options' }, + "Show ", + showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }), + this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "), + showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }), + this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped") + ) + ), + + this.runnerDiv = this.createDom('div', { className: 'runner running' }, + this.createDom('a', { className: 'run_spec', href: '?' }, "run all"), + this.runnerMessageSpan = this.createDom('span', {}, "Running..."), + this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, "")) + ); + + this.document.body.appendChild(this.outerDiv); + + var suites = runner.suites(); + for (var i = 0; i < suites.length; i++) { + var suite = suites[i]; + var suiteDiv = this.createDom('div', { className: 'suite' }, + this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"), + this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description)); + this.suiteDivs[suite.id] = suiteDiv; + var parentDiv = this.outerDiv; + if (suite.parentSuite) { + parentDiv = this.suiteDivs[suite.parentSuite.id]; + } + parentDiv.appendChild(suiteDiv); + } + + this.startedAt = new Date(); + + var self = this; + showPassed.onchange = function(evt) { + if (evt.target.checked) { + self.outerDiv.className += ' show-passed'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, ''); + } + }; + + showSkipped.onchange = function(evt) { + if (evt.target.checked) { + self.outerDiv.className += ' show-skipped'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, ''); + } + }; +}; + +jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) { + var results = runner.results(); + var className = (results.failedCount > 0) ? "runner failed" : "runner passed"; + this.runnerDiv.setAttribute("class", className); + //do it twice for IE + this.runnerDiv.setAttribute("className", className); + var specs = runner.specs(); + var specCount = 0; + for (var i = 0; i < specs.length; i++) { + if (this.specFilter(specs[i])) { + specCount++; + } + } + var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s"); + message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s"; + this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild); + + this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString())); +}; + +jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) { + var results = suite.results(); + var status = results.passed() ? 'passed' : 'failed'; + if (results.totalCount == 0) { // todo: change this to check results.skipped + status = 'skipped'; + } + this.suiteDivs[suite.id].className += " " + status; +}; + +jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) { + if (this.logRunningSpecs) { + this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...'); + } +}; + +jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) { + var results = spec.results(); + var status = results.passed() ? 'passed' : 'failed'; + if (results.skipped) { + status = 'skipped'; + } + var specDiv = this.createDom('div', { className: 'spec ' + status }, + this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"), + this.createDom('a', { + className: 'description', + href: '?spec=' + encodeURIComponent(spec.getFullName()), + title: spec.getFullName() + }, spec.description)); + + + var resultItems = results.getItems(); + var messagesDiv = this.createDom('div', { className: 'messages' }); + for (var i = 0; i < resultItems.length; i++) { + var result = resultItems[i]; + + if (result.type == 'log') { + messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString())); + } else if (result.type == 'expect' && result.passed && !result.passed()) { + messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message)); + + if (result.trace.stack) { + messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); + } + } + } + + if (messagesDiv.childNodes.length > 0) { + specDiv.appendChild(messagesDiv); + } + + this.suiteDivs[spec.suite.id].appendChild(specDiv); +}; + +jasmine.TrivialReporter.prototype.log = function() { + var console = jasmine.getGlobal().console; + if (console && console.log) console.log.apply(console, arguments); +}; + +jasmine.TrivialReporter.prototype.getLocation = function() { + return this.document.location; +}; + +jasmine.TrivialReporter.prototype.specFilter = function(spec) { + var paramMap = {}; + var params = this.getLocation().search.substring(1).split('&'); + for (var i = 0; i < params.length; i++) { + var p = params[i].split('='); + paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); + } + + if (!paramMap["spec"]) return true; + return spec.getFullName().indexOf(paramMap["spec"]) == 0; +}; diff --git a/javascript-koans/lib/jasmine/jasmine.css b/javascript-koans/lib/jasmine/jasmine.css new file mode 100644 index 0000000..6583fe7 --- /dev/null +++ b/javascript-koans/lib/jasmine/jasmine.css @@ -0,0 +1,166 @@ +body { + font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; +} + + +.jasmine_reporter a:visited, .jasmine_reporter a { + color: #303; +} + +.jasmine_reporter a:hover, .jasmine_reporter a:active { + color: blue; +} + +.run_spec { + float:right; + padding-right: 5px; + font-size: .8em; + text-decoration: none; +} + +.jasmine_reporter { + margin: 0 5px; +} + +.banner { + color: #303; + background-color: #fef; + padding: 5px; +} + +.logo { + float: left; + font-size: 1.1em; + padding-left: 5px; +} + +.logo .version { + font-size: .6em; + padding-left: 1em; +} + +.runner.running { + background-color: yellow; +} + + +.options { + text-align: right; + font-size: .8em; +} + + + + +.suite { + border: 1px outset gray; + margin: 5px 0; + padding-left: 1em; +} + +.suite .suite { + margin: 5px; +} + +.suite.passed { + background-color: #dfd; +} + +.suite.failed { + background-color: #fdd; +} + +.spec { + margin: 5px; + padding-left: 1em; + clear: both; +} + +.spec.failed, .spec.passed, .spec.skipped { + padding-bottom: 5px; + border: 1px solid gray; +} + +.spec.failed { + background-color: #fbb; + border-color: red; +} + +.spec.passed { + background-color: #bfb; + border-color: green; +} + +.spec.skipped { + background-color: #bbb; +} + +.messages { + border-left: 1px dashed gray; + padding-left: 1em; + padding-right: 1em; +} + +.passed { + background-color: #cfc; + display: none; +} + +.failed { + background-color: #fbb; +} + +.skipped { + color: #777; + background-color: #eee; + display: none; +} + + +/*.resultMessage {*/ + /*white-space: pre;*/ +/*}*/ + +.resultMessage span.result { + display: block; + line-height: 2em; + color: black; +} + +.resultMessage .mismatch { + color: black; +} + +.stackTrace { + white-space: pre; + font-size: .8em; + margin-left: 10px; + max-height: 5em; + overflow: auto; + border: 1px inset red; + padding: 1em; + background: #eef; +} + +.finished-at { + padding-left: 1em; + font-size: .6em; +} + +.show-passed .passed, +.show-skipped .skipped { + display: block; +} + + +#jasmine_content { + position:fixed; + right: 100%; +} + +.runner { + border: 1px solid gray; + display: block; + margin: 5px 0; + padding: 2px 0 2px 10px; +} diff --git a/javascript-koans/lib/jasmine/jasmine.js b/javascript-koans/lib/jasmine/jasmine.js new file mode 100644 index 0000000..3ace3bc --- /dev/null +++ b/javascript-koans/lib/jasmine/jasmine.js @@ -0,0 +1,2421 @@ +/** + * Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework. + * + * @namespace + */ +var jasmine = {}; + +/** + * @private + */ +jasmine.unimplementedMethod_ = function() { + throw new Error("unimplemented method"); +}; + +/** + * Use jasmine.undefined instead of undefined, since undefined is just + * a plain old variable and may be redefined by somebody else. + * + * @private + */ +jasmine.undefined = jasmine.___undefined___; + +/** + * Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. + * + */ +jasmine.DEFAULT_UPDATE_INTERVAL = 250; + +/** + * Default timeout interval in milliseconds for waitsFor() blocks. + */ +jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; + +jasmine.getGlobal = function() { + function getGlobal() { + return this; + } + + return getGlobal(); +}; + +/** + * Allows for bound functions to be compared. Internal use only. + * + * @ignore + * @private + * @param base {Object} bound 'this' for the function + * @param name {Function} function to find + */ +jasmine.bindOriginal_ = function(base, name) { + var original = base[name]; + if (original.apply) { + return function() { + return original.apply(base, arguments); + }; + } else { + // IE support + return jasmine.getGlobal()[name]; + } +}; + +jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); +jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); +jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); +jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); + +jasmine.MessageResult = function(values) { + this.type = 'log'; + this.values = values; + this.trace = new Error(); // todo: test better +}; + +jasmine.MessageResult.prototype.toString = function() { + var text = ""; + for(var i = 0; i < this.values.length; i++) { + if (i > 0) text += " "; + if (jasmine.isString_(this.values[i])) { + text += this.values[i]; + } else { + text += jasmine.pp(this.values[i]); + } + } + return text; +}; + +jasmine.ExpectationResult = function(params) { + this.type = 'expect'; + this.matcherName = params.matcherName; + this.passed_ = params.passed; + this.expected = params.expected; + this.actual = params.actual; + + this.message = this.passed_ ? 'Passed.' : params.message; + this.trace = this.passed_ ? '' : new Error(this.message); +}; + +jasmine.ExpectationResult.prototype.toString = function () { + return this.message; +}; + +jasmine.ExpectationResult.prototype.passed = function () { + return this.passed_; +}; + +/** + * Getter for the Jasmine environment. Ensures one gets created + */ +jasmine.getEnv = function() { + return jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(); +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isArray_ = function(value) { + return jasmine.isA_("Array", value); +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isString_ = function(value) { + return jasmine.isA_("String", value); +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isNumber_ = function(value) { + return jasmine.isA_("Number", value); +}; + +/** + * @ignore + * @private + * @param {String} typeName + * @param value + * @returns {Boolean} + */ +jasmine.isA_ = function(typeName, value) { + return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; +}; + +/** + * Pretty printer for expecations. Takes any object and turns it into a human-readable string. + * + * @param value {Object} an object to be outputted + * @returns {String} + */ +jasmine.pp = function(value) { + var stringPrettyPrinter = new jasmine.StringPrettyPrinter(); + stringPrettyPrinter.format(value); + return stringPrettyPrinter.string; +}; + +/** + * Returns true if the object is a DOM Node. + * + * @param {Object} obj object to check + * @returns {Boolean} + */ +jasmine.isDomNode = function(obj) { + return obj['nodeType'] > 0; +}; + +/** + * Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter. + * + * @example + * // don't care about which function is passed in, as long as it's a function + * expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function)); + * + * @param {Class} clazz + * @returns matchable object of the type clazz + */ +jasmine.any = function(clazz) { + return new jasmine.Matchers.Any(clazz); +}; + +/** + * Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. + * + * Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine + * expectation syntax. Spies can be checked if they were called or not and what the calling params were. + * + * A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). + * + * Spies are torn down at the end of every spec. + * + * Note: Do not call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj. + * + * @example + * // a stub + * var myStub = jasmine.createSpy('myStub'); // can be used anywhere + * + * // spy example + * var foo = { + * not: function(bool) { return !bool; } + * } + * + * // actual foo.not will not be called, execution stops + * spyOn(foo, 'not'); + + // foo.not spied upon, execution will continue to implementation + * spyOn(foo, 'not').andCallThrough(); + * + * // fake example + * var foo = { + * not: function(bool) { return !bool; } + * } + * + * // foo.not(val) will return val + * spyOn(foo, 'not').andCallFake(function(value) {return value;}); + * + * // mock example + * foo.not(7 == 7); + * expect(foo.not).toHaveBeenCalled(); + * expect(foo.not).toHaveBeenCalledWith(true); + * + * @constructor + * @see spyOn, jasmine.createSpy, jasmine.createSpyObj + * @param {String} name + */ +jasmine.Spy = function(name) { + /** + * The name of the spy, if provided. + */ + this.identity = name || 'unknown'; + /** + * Is this Object a spy? + */ + this.isSpy = true; + /** + * The actual function this spy stubs. + */ + this.plan = function() { + }; + /** + * Tracking of the most recent call to the spy. + * @example + * var mySpy = jasmine.createSpy('foo'); + * mySpy(1, 2); + * mySpy.mostRecentCall.args = [1, 2]; + */ + this.mostRecentCall = {}; + + /** + * Holds arguments for each call to the spy, indexed by call count + * @example + * var mySpy = jasmine.createSpy('foo'); + * mySpy(1, 2); + * mySpy(7, 8); + * mySpy.mostRecentCall.args = [7, 8]; + * mySpy.argsForCall[0] = [1, 2]; + * mySpy.argsForCall[1] = [7, 8]; + */ + this.argsForCall = []; + this.calls = []; +}; + +/** + * Tells a spy to call through to the actual implemenatation. + * + * @example + * var foo = { + * bar: function() { // do some stuff } + * } + * + * // defining a spy on an existing property: foo.bar + * spyOn(foo, 'bar').andCallThrough(); + */ +jasmine.Spy.prototype.andCallThrough = function() { + this.plan = this.originalValue; + return this; +}; + +/** + * For setting the return value of a spy. + * + * @example + * // defining a spy from scratch: foo() returns 'baz' + * var foo = jasmine.createSpy('spy on foo').andReturn('baz'); + * + * // defining a spy on an existing property: foo.bar() returns 'baz' + * spyOn(foo, 'bar').andReturn('baz'); + * + * @param {Object} value + */ +jasmine.Spy.prototype.andReturn = function(value) { + this.plan = function() { + return value; + }; + return this; +}; + +/** + * For throwing an exception when a spy is called. + * + * @example + * // defining a spy from scratch: foo() throws an exception w/ message 'ouch' + * var foo = jasmine.createSpy('spy on foo').andThrow('baz'); + * + * // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch' + * spyOn(foo, 'bar').andThrow('baz'); + * + * @param {String} exceptionMsg + */ +jasmine.Spy.prototype.andThrow = function(exceptionMsg) { + this.plan = function() { + throw exceptionMsg; + }; + return this; +}; + +/** + * Calls an alternate implementation when a spy is called. + * + * @example + * var baz = function() { + * // do some stuff, return something + * } + * // defining a spy from scratch: foo() calls the function baz + * var foo = jasmine.createSpy('spy on foo').andCall(baz); + * + * // defining a spy on an existing property: foo.bar() calls an anonymnous function + * spyOn(foo, 'bar').andCall(function() { return 'baz';} ); + * + * @param {Function} fakeFunc + */ +jasmine.Spy.prototype.andCallFake = function(fakeFunc) { + this.plan = fakeFunc; + return this; +}; + +/** + * Resets all of a spy's the tracking variables so that it can be used again. + * + * @example + * spyOn(foo, 'bar'); + * + * foo.bar(); + * + * expect(foo.bar.callCount).toEqual(1); + * + * foo.bar.reset(); + * + * expect(foo.bar.callCount).toEqual(0); + */ +jasmine.Spy.prototype.reset = function() { + this.wasCalled = false; + this.callCount = 0; + this.argsForCall = []; + this.calls = []; + this.mostRecentCall = {}; +}; + +jasmine.createSpy = function(name) { + + var spyObj = function() { + spyObj.wasCalled = true; + spyObj.callCount++; + var args = jasmine.util.argsToArray(arguments); + spyObj.mostRecentCall.object = this; + spyObj.mostRecentCall.args = args; + spyObj.argsForCall.push(args); + spyObj.calls.push({object: this, args: args}); + return spyObj.plan.apply(this, arguments); + }; + + var spy = new jasmine.Spy(name); + + for (var prop in spy) { + spyObj[prop] = spy[prop]; + } + + spyObj.reset(); + + return spyObj; +}; + +/** + * Determines whether an object is a spy. + * + * @param {jasmine.Spy|Object} putativeSpy + * @returns {Boolean} + */ +jasmine.isSpy = function(putativeSpy) { + return putativeSpy && putativeSpy.isSpy; +}; + +/** + * Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something + * large in one call. + * + * @param {String} baseName name of spy class + * @param {Array} methodNames array of names of methods to make spies + */ +jasmine.createSpyObj = function(baseName, methodNames) { + if (!jasmine.isArray_(methodNames) || methodNames.length == 0) { + throw new Error('createSpyObj requires a non-empty array of method names to create spies for'); + } + var obj = {}; + for (var i = 0; i < methodNames.length; i++) { + obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]); + } + return obj; +}; + +/** + * All parameters are pretty-printed and concatenated together, then written to the current spec's output. + * + * Be careful not to leave calls to jasmine.log in production code. + */ +jasmine.log = function() { + var spec = jasmine.getEnv().currentSpec; + spec.log.apply(spec, arguments); +}; + +/** + * Function that installs a spy on an existing object's method name. Used within a Spec to create a spy. + * + * @example + * // spy example + * var foo = { + * not: function(bool) { return !bool; } + * } + * spyOn(foo, 'not'); // actual foo.not will not be called, execution stops + * + * @see jasmine.createSpy + * @param obj + * @param methodName + * @returns a Jasmine spy that can be chained with all spy methods + */ +var spyOn = function(obj, methodName) { + return jasmine.getEnv().currentSpec.spyOn(obj, methodName); +}; + +/** + * Creates a Jasmine spec that will be added to the current suite. + * + * // TODO: pending tests + * + * @example + * it('should be true', function() { + * expect(true).toEqual(true); + * }); + * + * @param {String} desc description of this specification + * @param {Function} func defines the preconditions and expectations of the spec + */ +var it = function(desc, func) { + return jasmine.getEnv().it(desc, func); +}; + +/** + * Creates a disabled Jasmine spec. + * + * A convenience method that allows existing specs to be disabled temporarily during development. + * + * @param {String} desc description of this specification + * @param {Function} func defines the preconditions and expectations of the spec + */ +var xit = function(desc, func) { + return jasmine.getEnv().xit(desc, func); +}; + +/** + * Starts a chain for a Jasmine expectation. + * + * It is passed an Object that is the actual value and should chain to one of the many + * jasmine.Matchers functions. + * + * @param {Object} actual Actual value to test against and expected value + */ +var expect = function(actual) { + return jasmine.getEnv().currentSpec.expect(actual); +}; + +/** + * Defines part of a jasmine spec. Used in cominbination with waits or waitsFor in asynchrnous specs. + * + * @param {Function} func Function that defines part of a jasmine spec. + */ +var runs = function(func) { + jasmine.getEnv().currentSpec.runs(func); +}; + +/** + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait + */ +var waits = function(timeout) { + jasmine.getEnv().currentSpec.waits(timeout); +}; + +/** + * Waits for the latchFunction to return true before proceeding to the next block. + * + * @param {Function} latchFunction + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout + */ +var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments); +}; + +/** + * A function that is called before each spec in a suite. + * + * Used for spec setup, including validating assumptions. + * + * @param {Function} beforeEachFunction + */ +var beforeEach = function(beforeEachFunction) { + jasmine.getEnv().beforeEach(beforeEachFunction); +}; + +/** + * A function that is called after each spec in a suite. + * + * Used for restoring any state that is hijacked during spec execution. + * + * @param {Function} afterEachFunction + */ +var afterEach = function(afterEachFunction) { + jasmine.getEnv().afterEach(afterEachFunction); +}; + +/** + * Defines a suite of specifications. + * + * Stores the description and all defined specs in the Jasmine environment as one suite of specs. Variables declared + * are accessible by calls to beforeEach, it, and afterEach. Describe blocks can be nested, allowing for specialization + * of setup in some tests. + * + * @example + * // TODO: a simple suite + * + * // TODO: a simple suite with a nested describe block + * + * @param {String} description A string, usually the class under test. + * @param {Function} specDefinitions function that defines several specs. + */ +var describe = function(description, specDefinitions) { + return jasmine.getEnv().describe(description, specDefinitions); +}; + +/** + * Disables a suite of specifications. Used to disable some suites in a file, or files, temporarily during development. + * + * @param {String} description A string, usually the class under test. + * @param {Function} specDefinitions function that defines several specs. + */ +var xdescribe = function(description, specDefinitions) { + return jasmine.getEnv().xdescribe(description, specDefinitions); +}; + + +// Provide the XMLHttpRequest class for IE 5.x-6.x: +jasmine.XmlHttpRequest = (typeof XMLHttpRequest == "undefined") ? function() { + try { + return new ActiveXObject("Msxml2.XMLHTTP.6.0"); + } catch(e) { + } + try { + return new ActiveXObject("Msxml2.XMLHTTP.3.0"); + } catch(e) { + } + try { + return new ActiveXObject("Msxml2.XMLHTTP"); + } catch(e) { + } + try { + return new ActiveXObject("Microsoft.XMLHTTP"); + } catch(e) { + } + throw new Error("This browser does not support XMLHttpRequest."); +} : XMLHttpRequest; +/** + * @namespace + */ +jasmine.util = {}; + +/** + * Declare that a child class inherit it's prototype from the parent class. + * + * @private + * @param {Function} childClass + * @param {Function} parentClass + */ +jasmine.util.inherit = function(childClass, parentClass) { + /** + * @private + */ + var subclass = function() { + }; + subclass.prototype = parentClass.prototype; + childClass.prototype = new subclass; +}; + +jasmine.util.formatException = function(e) { + var lineNumber; + if (e.line) { + lineNumber = e.line; + } + else if (e.lineNumber) { + lineNumber = e.lineNumber; + } + + var file; + + if (e.sourceURL) { + file = e.sourceURL; + } + else if (e.fileName) { + file = e.fileName; + } + + var message = (e.name && e.message) ? (e.name + ': ' + e.message) : e.toString(); + + if (file && lineNumber) { + message += ' in ' + file + ' (line ' + lineNumber + ')'; + } + + return message; +}; + +jasmine.util.htmlEscape = function(str) { + if (!str) return str; + return str.replace(/&/g, '&') + .replace(//g, '>'); +}; + +jasmine.util.argsToArray = function(args) { + var arrayOfArgs = []; + for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]); + return arrayOfArgs; +}; + +jasmine.util.extend = function(destination, source) { + for (var property in source) destination[property] = source[property]; + return destination; +}; + +/** + * Environment for Jasmine + * + * @constructor + */ +jasmine.Env = function() { + this.currentSpec = null; + this.currentSuite = null; + this.currentRunner_ = new jasmine.Runner(this); + + this.reporter = new jasmine.MultiReporter(); + + this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; + this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; + this.lastUpdate = 0; + this.specFilter = function() { + return true; + }; + + this.nextSpecId_ = 0; + this.nextSuiteId_ = 0; + this.equalityTesters_ = []; + + // wrap matchers + this.matchersClass = function() { + jasmine.Matchers.apply(this, arguments); + }; + jasmine.util.inherit(this.matchersClass, jasmine.Matchers); + + jasmine.Matchers.wrapInto_(jasmine.Matchers.prototype, this.matchersClass); +}; + + +jasmine.Env.prototype.setTimeout = jasmine.setTimeout; +jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout; +jasmine.Env.prototype.setInterval = jasmine.setInterval; +jasmine.Env.prototype.clearInterval = jasmine.clearInterval; + +/** + * @returns an object containing jasmine version build info, if set. + */ +jasmine.Env.prototype.version = function () { + if (jasmine.version_) { + return jasmine.version_; + } else { + throw new Error('Version not set'); + } +}; + +/** + * @returns string containing jasmine version build info, if set. + */ +jasmine.Env.prototype.versionString = function() { + if (jasmine.version_) { + var version = this.version(); + return version.major + "." + version.minor + "." + version.build + " revision " + version.revision; + } else { + return "version unknown"; + } +}; + +/** + * @returns a sequential integer starting at 0 + */ +jasmine.Env.prototype.nextSpecId = function () { + return this.nextSpecId_++; +}; + +/** + * @returns a sequential integer starting at 0 + */ +jasmine.Env.prototype.nextSuiteId = function () { + return this.nextSuiteId_++; +}; + +/** + * Register a reporter to receive status updates from Jasmine. + * @param {jasmine.Reporter} reporter An object which will receive status updates. + */ +jasmine.Env.prototype.addReporter = function(reporter) { + this.reporter.addReporter(reporter); +}; + +jasmine.Env.prototype.execute = function() { + this.currentRunner_.execute(); +}; + +jasmine.Env.prototype.describe = function(description, specDefinitions) { + var suite = new jasmine.Suite(this, description, specDefinitions, this.currentSuite); + + var parentSuite = this.currentSuite; + if (parentSuite) { + parentSuite.add(suite); + } else { + this.currentRunner_.add(suite); + } + + this.currentSuite = suite; + + var declarationError = null; + try { + specDefinitions.call(suite); + } catch(e) { + declarationError = e; + } + + this.currentSuite = parentSuite; + + if (declarationError) { + this.it("encountered a declaration exception", function() { + throw declarationError; + }); + } + + return suite; +}; + +jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { + if (this.currentSuite) { + this.currentSuite.beforeEach(beforeEachFunction); + } else { + this.currentRunner_.beforeEach(beforeEachFunction); + } +}; + +jasmine.Env.prototype.currentRunner = function () { + return this.currentRunner_; +}; + +jasmine.Env.prototype.afterEach = function(afterEachFunction) { + if (this.currentSuite) { + this.currentSuite.afterEach(afterEachFunction); + } else { + this.currentRunner_.afterEach(afterEachFunction); + } + +}; + +jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { + return { + execute: function() { + } + }; +}; + +jasmine.Env.prototype.it = function(description, func) { + var spec = new jasmine.Spec(this, this.currentSuite, description); + this.currentSuite.add(spec); + this.currentSpec = spec; + + if (func) { + spec.runs(func); + } + + return spec; +}; + +jasmine.Env.prototype.xit = function(desc, func) { + return { + id: this.nextSpecId(), + runs: function() { + } + }; +}; + +jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { + if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { + return true; + } + + a.__Jasmine_been_here_before__ = b; + b.__Jasmine_been_here_before__ = a; + + var hasKey = function(obj, keyName) { + return obj != null && obj[keyName] !== jasmine.undefined; + }; + + for (var property in b) { + if (!hasKey(a, property) && hasKey(b, property)) { + mismatchKeys.push("expected has key '" + property + "', but missing from actual."); + } + } + for (property in a) { + if (!hasKey(b, property) && hasKey(a, property)) { + mismatchKeys.push("expected missing key '" + property + "', but present in actual."); + } + } + for (property in b) { + if (property == '__Jasmine_been_here_before__') continue; + if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { + mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "' in actual."); + } + } + + if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) { + mismatchValues.push("arrays were not the same length"); + } + + delete a.__Jasmine_been_here_before__; + delete b.__Jasmine_been_here_before__; + return (mismatchKeys.length == 0 && mismatchValues.length == 0); +}; + +jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { + mismatchKeys = mismatchKeys || []; + mismatchValues = mismatchValues || []; + + for (var i = 0; i < this.equalityTesters_.length; i++) { + var equalityTester = this.equalityTesters_[i]; + var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); + if (result !== jasmine.undefined) return result; + } + + if (a === b) return true; + + if (a === jasmine.undefined || a === null || b === jasmine.undefined || b === null) { + return (a == jasmine.undefined && b == jasmine.undefined); + } + + if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) { + return a === b; + } + + if (a instanceof Date && b instanceof Date) { + return a.getTime() == b.getTime(); + } + + if (a instanceof jasmine.Matchers.Any) { + return a.matches(b); + } + + if (b instanceof jasmine.Matchers.Any) { + return b.matches(a); + } + + if (jasmine.isString_(a) && jasmine.isString_(b)) { + return (a == b); + } + + if (jasmine.isNumber_(a) && jasmine.isNumber_(b)) { + return (a == b); + } + + if (typeof a === "object" && typeof b === "object") { + return this.compareObjects_(a, b, mismatchKeys, mismatchValues); + } + + //Straight check + return (a === b); +}; + +jasmine.Env.prototype.contains_ = function(haystack, needle) { + if (jasmine.isArray_(haystack)) { + for (var i = 0; i < haystack.length; i++) { + if (this.equals_(haystack[i], needle)) return true; + } + return false; + } + return haystack.indexOf(needle) >= 0; +}; + +jasmine.Env.prototype.addEqualityTester = function(equalityTester) { + this.equalityTesters_.push(equalityTester); +}; +/** No-op base class for Jasmine reporters. + * + * @constructor + */ +jasmine.Reporter = function() { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportRunnerStarting = function(runner) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportRunnerResults = function(runner) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSuiteResults = function(suite) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSpecStarting = function(spec) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSpecResults = function(spec) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.log = function(str) { +}; + +/** + * Blocks are functions with executable code that make up a spec. + * + * @constructor + * @param {jasmine.Env} env + * @param {Function} func + * @param {jasmine.Spec} spec + */ +jasmine.Block = function(env, func, spec) { + this.env = env; + this.func = func; + this.spec = spec; +}; + +jasmine.Block.prototype.execute = function(onComplete) { + try { + this.func.apply(this.spec); + } catch (e) { + this.spec.fail(e); + } + onComplete(); +}; +/** JavaScript API reporter. + * + * @constructor + */ +jasmine.JsApiReporter = function() { + this.started = false; + this.finished = false; + this.suites_ = []; + this.results_ = {}; +}; + +jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) { + this.started = true; + var suites = runner.topLevelSuites(); + for (var i = 0; i < suites.length; i++) { + var suite = suites[i]; + this.suites_.push(this.summarize_(suite)); + } +}; + +jasmine.JsApiReporter.prototype.suites = function() { + return this.suites_; +}; + +jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) { + var isSuite = suiteOrSpec instanceof jasmine.Suite; + var summary = { + id: suiteOrSpec.id, + name: suiteOrSpec.description, + type: isSuite ? 'suite' : 'spec', + children: [] + }; + + if (isSuite) { + var children = suiteOrSpec.children(); + for (var i = 0; i < children.length; i++) { + summary.children.push(this.summarize_(children[i])); + } + } + return summary; +}; + +jasmine.JsApiReporter.prototype.results = function() { + return this.results_; +}; + +jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) { + return this.results_[specId]; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) { + this.finished = true; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) { + this.results_[spec.id] = { + messages: spec.results().getItems(), + result: spec.results().failedCount > 0 ? "failed" : "passed" + }; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.log = function(str) { +}; + +jasmine.JsApiReporter.prototype.resultsForSpecs = function(specIds){ + var results = {}; + for (var i = 0; i < specIds.length; i++) { + var specId = specIds[i]; + results[specId] = this.summarizeResult_(this.results_[specId]); + } + return results; +}; + +jasmine.JsApiReporter.prototype.summarizeResult_ = function(result){ + var summaryMessages = []; + var messagesLength = result.messages.length; + for (var messageIndex = 0; messageIndex < messagesLength; messageIndex++) { + var resultMessage = result.messages[messageIndex]; + summaryMessages.push({ + text: resultMessage.type == 'log' ? resultMessage.toString() : jasmine.undefined, + passed: resultMessage.passed ? resultMessage.passed() : true, + type: resultMessage.type, + message: resultMessage.message, + trace: { + stack: resultMessage.passed && !resultMessage.passed() ? resultMessage.trace.stack : jasmine.undefined + } + }); + } + + return { + result : result.result, + messages : summaryMessages + }; +}; + +/** + * @constructor + * @param {jasmine.Env} env + * @param actual + * @param {jasmine.Spec} spec + */ +jasmine.Matchers = function(env, actual, spec, opt_isNot) { + this.env = env; + this.actual = actual; + this.spec = spec; + this.isNot = opt_isNot || false; + this.reportWasCalled_ = false; +}; + +// todo: @deprecated as of Jasmine 0.11, remove soon [xw] +jasmine.Matchers.pp = function(str) { + throw new Error("jasmine.Matchers.pp() is no longer supported, please use jasmine.pp() instead!"); +}; + +// todo: @deprecated Deprecated as of Jasmine 0.10. Rewrite your custom matchers to return true or false. [xw] +jasmine.Matchers.prototype.report = function(result, failing_message, details) { + throw new Error("As of jasmine 0.11, custom matchers must be implemented differently -- please see jasmine docs"); +}; + +jasmine.Matchers.wrapInto_ = function(prototype, matchersClass) { + for (var methodName in prototype) { + if (methodName == 'report') continue; + var orig = prototype[methodName]; + matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig); + } +}; + +jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) { + return function() { + var matcherArgs = jasmine.util.argsToArray(arguments); + var result = matcherFunction.apply(this, arguments); + + if (this.isNot) { + result = !result; + } + + if (this.reportWasCalled_) return result; + + var message; + if (!result) { + if (this.message) { + message = this.message.apply(this, arguments); + if (jasmine.isArray_(message)) { + message = message[this.isNot ? 1 : 0]; + } + } else { + var englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); }); + message = "Expected " + jasmine.pp(this.actual) + (this.isNot ? " not " : " ") + englishyPredicate; + if (matcherArgs.length > 0) { + for (var i = 0; i < matcherArgs.length; i++) { + if (i > 0) message += ","; + message += " " + jasmine.pp(matcherArgs[i]); + } + } + message += "."; + } + } + var expectationResult = new jasmine.ExpectationResult({ + matcherName: matcherName, + passed: result, + expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0], + actual: this.actual, + message: message + }); + this.spec.addMatcherResult(expectationResult); + return jasmine.undefined; + }; +}; + + + + +/** + * toBe: compares the actual to the expected using === + * @param expected + */ +jasmine.Matchers.prototype.toBe = function(expected) { + return this.actual === expected; +}; + +/** + * toNotBe: compares the actual to the expected using !== + * @param expected + * @deprecated as of 1.0. Use not.toBe() instead. + */ +jasmine.Matchers.prototype.toNotBe = function(expected) { + return this.actual !== expected; +}; + +/** + * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. + * + * @param expected + */ +jasmine.Matchers.prototype.toEqual = function(expected) { + return this.env.equals_(this.actual, expected); +}; + +/** + * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual + * @param expected + * @deprecated as of 1.0. Use not.toNotEqual() instead. + */ +jasmine.Matchers.prototype.toNotEqual = function(expected) { + return !this.env.equals_(this.actual, expected); +}; + +/** + * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes + * a pattern or a String. + * + * @param expected + */ +jasmine.Matchers.prototype.toMatch = function(expected) { + return new RegExp(expected).test(this.actual); +}; + +/** + * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch + * @param expected + * @deprecated as of 1.0. Use not.toMatch() instead. + */ +jasmine.Matchers.prototype.toNotMatch = function(expected) { + return !(new RegExp(expected).test(this.actual)); +}; + +/** + * Matcher that compares the actual to jasmine.undefined. + */ +jasmine.Matchers.prototype.toBeDefined = function() { + return (this.actual !== jasmine.undefined); +}; + +/** + * Matcher that compares the actual to jasmine.undefined. + */ +jasmine.Matchers.prototype.toBeUndefined = function() { + return (this.actual === jasmine.undefined); +}; + +/** + * Matcher that compares the actual to null. + */ +jasmine.Matchers.prototype.toBeNull = function() { + return (this.actual === null); +}; + +/** + * Matcher that boolean not-nots the actual. + */ +jasmine.Matchers.prototype.toBeTruthy = function() { + return !!this.actual; +}; + + +/** + * Matcher that boolean nots the actual. + */ +jasmine.Matchers.prototype.toBeFalsy = function() { + return !this.actual; +}; + + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was called. + */ +jasmine.Matchers.prototype.toHaveBeenCalled = function() { + if (arguments.length > 0) { + throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith'); + } + + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy " + this.actual.identity + " to have been called.", + "Expected spy " + this.actual.identity + " not to have been called." + ]; + }; + + return this.actual.wasCalled; +}; + +/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */ +jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled; + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was not called. + * + * @deprecated Use expect(xxx).not.toHaveBeenCalled() instead + */ +jasmine.Matchers.prototype.wasNotCalled = function() { + if (arguments.length > 0) { + throw new Error('wasNotCalled does not take arguments'); + } + + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy " + this.actual.identity + " to not have been called.", + "Expected spy " + this.actual.identity + " to have been called." + ]; + }; + + return !this.actual.wasCalled; +}; + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters. + * + * @example + * + */ +jasmine.Matchers.prototype.toHaveBeenCalledWith = function() { + var expectedArgs = jasmine.util.argsToArray(arguments); + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + this.message = function() { + if (this.actual.callCount == 0) { + // todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw] + return [ + "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.", + "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was." + ]; + } else { + return [ + "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall), + "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall) + ]; + } + }; + + return this.env.contains_(this.actual.argsForCall, expectedArgs); +}; + +/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */ +jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith; + +/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */ +jasmine.Matchers.prototype.wasNotCalledWith = function() { + var expectedArgs = jasmine.util.argsToArray(arguments); + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was", + "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was" + ] + }; + + return !this.env.contains_(this.actual.argsForCall, expectedArgs); +}; + +/** + * Matcher that checks that the expected item is an element in the actual Array. + * + * @param {Object} expected + */ +jasmine.Matchers.prototype.toContain = function(expected) { + return this.env.contains_(this.actual, expected); +}; + +/** + * Matcher that checks that the expected item is NOT an element in the actual Array. + * + * @param {Object} expected + * @deprecated as of 1.0. Use not.toNotContain() instead. + */ +jasmine.Matchers.prototype.toNotContain = function(expected) { + return !this.env.contains_(this.actual, expected); +}; + +jasmine.Matchers.prototype.toBeLessThan = function(expected) { + return this.actual < expected; +}; + +jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { + return this.actual > expected; +}; + +/** + * Matcher that checks that the expected exception was thrown by the actual. + * + * @param {String} expected + */ +jasmine.Matchers.prototype.toThrow = function(expected) { + var result = false; + var exception; + if (typeof this.actual != 'function') { + throw new Error('Actual is not a function'); + } + try { + this.actual(); + } catch (e) { + exception = e; + } + if (exception) { + result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected)); + } + + var not = this.isNot ? "not " : ""; + + this.message = function() { + if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { + return ["Expected function " + not + "to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' '); + } else { + return "Expected function to throw an exception."; + } + }; + + return result; +}; + +jasmine.Matchers.Any = function(expectedClass) { + this.expectedClass = expectedClass; +}; + +jasmine.Matchers.Any.prototype.matches = function(other) { + if (this.expectedClass == String) { + return typeof other == 'string' || other instanceof String; + } + + if (this.expectedClass == Number) { + return typeof other == 'number' || other instanceof Number; + } + + if (this.expectedClass == Function) { + return typeof other == 'function' || other instanceof Function; + } + + if (this.expectedClass == Object) { + return typeof other == 'object'; + } + + return other instanceof this.expectedClass; +}; + +jasmine.Matchers.Any.prototype.toString = function() { + return ''; +}; + +/** + * @constructor + */ +jasmine.MultiReporter = function() { + this.subReporters_ = []; +}; +jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter); + +jasmine.MultiReporter.prototype.addReporter = function(reporter) { + this.subReporters_.push(reporter); +}; + +(function() { + var functionNames = [ + "reportRunnerStarting", + "reportRunnerResults", + "reportSuiteResults", + "reportSpecStarting", + "reportSpecResults", + "log" + ]; + for (var i = 0; i < functionNames.length; i++) { + var functionName = functionNames[i]; + jasmine.MultiReporter.prototype[functionName] = (function(functionName) { + return function() { + for (var j = 0; j < this.subReporters_.length; j++) { + var subReporter = this.subReporters_[j]; + if (subReporter[functionName]) { + subReporter[functionName].apply(subReporter, arguments); + } + } + }; + })(functionName); + } +})(); +/** + * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults + * + * @constructor + */ +jasmine.NestedResults = function() { + /** + * The total count of results + */ + this.totalCount = 0; + /** + * Number of passed results + */ + this.passedCount = 0; + /** + * Number of failed results + */ + this.failedCount = 0; + /** + * Was this suite/spec skipped? + */ + this.skipped = false; + /** + * @ignore + */ + this.items_ = []; +}; + +/** + * Roll up the result counts. + * + * @param result + */ +jasmine.NestedResults.prototype.rollupCounts = function(result) { + this.totalCount += result.totalCount; + this.passedCount += result.passedCount; + this.failedCount += result.failedCount; +}; + +/** + * Adds a log message. + * @param values Array of message parts which will be concatenated later. + */ +jasmine.NestedResults.prototype.log = function(values) { + this.items_.push(new jasmine.MessageResult(values)); +}; + +/** + * Getter for the results: message & results. + */ +jasmine.NestedResults.prototype.getItems = function() { + return this.items_; +}; + +/** + * Adds a result, tracking counts (total, passed, & failed) + * @param {jasmine.ExpectationResult|jasmine.NestedResults} result + */ +jasmine.NestedResults.prototype.addResult = function(result) { + if (result.type != 'log') { + if (result.items_) { + this.rollupCounts(result); + } else { + this.totalCount++; + if (result.passed()) { + this.passedCount++; + } else { + this.failedCount++; + } + } + } + this.items_.push(result); +}; + +/** + * @returns {Boolean} True if everything below passed + */ +jasmine.NestedResults.prototype.passed = function() { + return this.passedCount === this.totalCount; +}; +/** + * Base class for pretty printing for expectation results. + */ +jasmine.PrettyPrinter = function() { + this.ppNestLevel_ = 0; +}; + +/** + * Formats a value in a nice, human-readable string. + * + * @param value + */ +jasmine.PrettyPrinter.prototype.format = function(value) { + if (this.ppNestLevel_ > 40) { + throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); + } + + this.ppNestLevel_++; + try { + if (value === jasmine.undefined) { + this.emitScalar('undefined'); + } else if (value === null) { + this.emitScalar('null'); + } else if (value === jasmine.getGlobal()) { + this.emitScalar(''); + } else if (value instanceof jasmine.Matchers.Any) { + this.emitScalar(value.toString()); + } else if (typeof value === 'string') { + this.emitString(value); + } else if (jasmine.isSpy(value)) { + this.emitScalar("spy on " + value.identity); + } else if (value instanceof RegExp) { + this.emitScalar(value.toString()); + } else if (typeof value === 'function') { + this.emitScalar('Function'); + } else if (typeof value.nodeType === 'number') { + this.emitScalar('HTMLNode'); + } else if (value instanceof Date) { + this.emitScalar('Date(' + value + ')'); + } else if (value.__Jasmine_been_here_before__) { + this.emitScalar(''); + } else if (jasmine.isArray_(value) || typeof value == 'object') { + value.__Jasmine_been_here_before__ = true; + if (jasmine.isArray_(value)) { + this.emitArray(value); + } else { + this.emitObject(value); + } + delete value.__Jasmine_been_here_before__; + } else { + this.emitScalar(value.toString()); + } + } finally { + this.ppNestLevel_--; + } +}; + +jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { + for (var property in obj) { + if (property == '__Jasmine_been_here_before__') continue; + fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) != null) : false); + } +}; + +jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_; + +jasmine.StringPrettyPrinter = function() { + jasmine.PrettyPrinter.call(this); + + this.string = ''; +}; +jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter); + +jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) { + this.append(value); +}; + +jasmine.StringPrettyPrinter.prototype.emitString = function(value) { + this.append("'" + value + "'"); +}; + +jasmine.StringPrettyPrinter.prototype.emitArray = function(array) { + this.append('[ '); + for (var i = 0; i < array.length; i++) { + if (i > 0) { + this.append(', '); + } + this.format(array[i]); + } + this.append(' ]'); +}; + +jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { + var self = this; + this.append('{ '); + var first = true; + + this.iterateObject(obj, function(property, isGetter) { + if (first) { + first = false; + } else { + self.append(', '); + } + + self.append(property); + self.append(' : '); + if (isGetter) { + self.append(''); + } else { + self.format(obj[property]); + } + }); + + this.append(' }'); +}; + +jasmine.StringPrettyPrinter.prototype.append = function(value) { + this.string += value; +}; +jasmine.Queue = function(env) { + this.env = env; + this.blocks = []; + this.running = false; + this.index = 0; + this.offset = 0; + this.abort = false; +}; + +jasmine.Queue.prototype.addBefore = function(block) { + this.blocks.unshift(block); +}; + +jasmine.Queue.prototype.add = function(block) { + this.blocks.push(block); +}; + +jasmine.Queue.prototype.insertNext = function(block) { + this.blocks.splice((this.index + this.offset + 1), 0, block); + this.offset++; +}; + +jasmine.Queue.prototype.start = function(onComplete) { + this.running = true; + this.onComplete = onComplete; + this.next_(); +}; + +jasmine.Queue.prototype.isRunning = function() { + return this.running; +}; + +jasmine.Queue.LOOP_DONT_RECURSE = true; + +jasmine.Queue.prototype.next_ = function() { + var self = this; + var goAgain = true; + + while (goAgain) { + goAgain = false; + + if (self.index < self.blocks.length && !this.abort) { + var calledSynchronously = true; + var completedSynchronously = false; + + var onComplete = function () { + if (jasmine.Queue.LOOP_DONT_RECURSE && calledSynchronously) { + completedSynchronously = true; + return; + } + + if (self.blocks[self.index].abort) { + self.abort = true; + } + + self.offset = 0; + self.index++; + + var now = new Date().getTime(); + if (self.env.updateInterval && now - self.env.lastUpdate > self.env.updateInterval) { + self.env.lastUpdate = now; + self.env.setTimeout(function() { + self.next_(); + }, 0); + } else { + if (jasmine.Queue.LOOP_DONT_RECURSE && completedSynchronously) { + goAgain = true; + } else { + self.next_(); + } + } + }; + self.blocks[self.index].execute(onComplete); + + calledSynchronously = false; + if (completedSynchronously) { + onComplete(); + } + + } else { + self.running = false; + if (self.onComplete) { + self.onComplete(); + } + } + } +}; + +jasmine.Queue.prototype.results = function() { + var results = new jasmine.NestedResults(); + for (var i = 0; i < this.blocks.length; i++) { + if (this.blocks[i].results) { + results.addResult(this.blocks[i].results()); + } + } + return results; +}; + + +/** + * Runner + * + * @constructor + * @param {jasmine.Env} env + */ +jasmine.Runner = function(env) { + var self = this; + self.env = env; + self.queue = new jasmine.Queue(env); + self.before_ = []; + self.after_ = []; + self.suites_ = []; +}; + +jasmine.Runner.prototype.execute = function() { + var self = this; + if (self.env.reporter.reportRunnerStarting) { + self.env.reporter.reportRunnerStarting(this); + } + self.queue.start(function () { + self.finishCallback(); + }); +}; + +jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.splice(0,0,beforeEachFunction); +}; + +jasmine.Runner.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.splice(0,0,afterEachFunction); +}; + + +jasmine.Runner.prototype.finishCallback = function() { + this.env.reporter.reportRunnerResults(this); +}; + +jasmine.Runner.prototype.addSuite = function(suite) { + this.suites_.push(suite); +}; + +jasmine.Runner.prototype.add = function(block) { + if (block instanceof jasmine.Suite) { + this.addSuite(block); + } + this.queue.add(block); +}; + +jasmine.Runner.prototype.specs = function () { + var suites = this.suites(); + var specs = []; + for (var i = 0; i < suites.length; i++) { + specs = specs.concat(suites[i].specs()); + } + return specs; +}; + +jasmine.Runner.prototype.suites = function() { + return this.suites_; +}; + +jasmine.Runner.prototype.topLevelSuites = function() { + var topLevelSuites = []; + for (var i = 0; i < this.suites_.length; i++) { + if (!this.suites_[i].parentSuite) { + topLevelSuites.push(this.suites_[i]); + } + } + return topLevelSuites; +}; + +jasmine.Runner.prototype.results = function() { + return this.queue.results(); +}; +/** + * Internal representation of a Jasmine specification, or test. + * + * @constructor + * @param {jasmine.Env} env + * @param {jasmine.Suite} suite + * @param {String} description + */ +jasmine.Spec = function(env, suite, description) { + if (!env) { + throw new Error('jasmine.Env() required'); + } + if (!suite) { + throw new Error('jasmine.Suite() required'); + } + var spec = this; + spec.id = env.nextSpecId ? env.nextSpecId() : null; + spec.env = env; + spec.suite = suite; + spec.description = description; + spec.queue = new jasmine.Queue(env); + + spec.afterCallbacks = []; + spec.spies_ = []; + + spec.results_ = new jasmine.NestedResults(); + spec.results_.description = description; + spec.matchersClass = null; +}; + +jasmine.Spec.prototype.getFullName = function() { + return this.suite.getFullName() + ' ' + this.description + '.'; +}; + + +jasmine.Spec.prototype.results = function() { + return this.results_; +}; + +/** + * All parameters are pretty-printed and concatenated together, then written to the spec's output. + * + * Be careful not to leave calls to jasmine.log in production code. + */ +jasmine.Spec.prototype.log = function() { + return this.results_.log(arguments); +}; + +jasmine.Spec.prototype.runs = function (func) { + var block = new jasmine.Block(this.env, func, this); + this.addToQueue(block); + return this; +}; + +jasmine.Spec.prototype.addToQueue = function (block) { + if (this.queue.isRunning()) { + this.queue.insertNext(block); + } else { + this.queue.add(block); + } +}; + +/** + * @param {jasmine.ExpectationResult} result + */ +jasmine.Spec.prototype.addMatcherResult = function(result) { + this.results_.addResult(result); +}; + +jasmine.Spec.prototype.expect = function(actual) { + var positive = new (this.getMatchersClass_())(this.env, actual, this); + positive.not = new (this.getMatchersClass_())(this.env, actual, this, true); + return positive; +}; + +/** + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait + */ +jasmine.Spec.prototype.waits = function(timeout) { + var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); + this.addToQueue(waitsFunc); + return this; +}; + +/** + * Waits for the latchFunction to return true before proceeding to the next block. + * + * @param {Function} latchFunction + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout + */ +jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + var latchFunction_ = null; + var optional_timeoutMessage_ = null; + var optional_timeout_ = null; + + for (var i = 0; i < arguments.length; i++) { + var arg = arguments[i]; + switch (typeof arg) { + case 'function': + latchFunction_ = arg; + break; + case 'string': + optional_timeoutMessage_ = arg; + break; + case 'number': + optional_timeout_ = arg; + break; + } + } + + var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this); + this.addToQueue(waitsForFunc); + return this; +}; + +jasmine.Spec.prototype.fail = function (e) { + var expectationResult = new jasmine.ExpectationResult({ + passed: false, + message: e ? jasmine.util.formatException(e) : 'Exception' + }); + this.results_.addResult(expectationResult); +}; + +jasmine.Spec.prototype.getMatchersClass_ = function() { + return this.matchersClass || this.env.matchersClass; +}; + +jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { + var parent = this.getMatchersClass_(); + var newMatchersClass = function() { + parent.apply(this, arguments); + }; + jasmine.util.inherit(newMatchersClass, parent); + jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass); + this.matchersClass = newMatchersClass; +}; + +jasmine.Spec.prototype.finishCallback = function() { + this.env.reporter.reportSpecResults(this); +}; + +jasmine.Spec.prototype.finish = function(onComplete) { + this.removeAllSpies(); + this.finishCallback(); + if (onComplete) { + onComplete(); + } +}; + +jasmine.Spec.prototype.after = function(doAfter) { + if (this.queue.isRunning()) { + this.queue.add(new jasmine.Block(this.env, doAfter, this)); + } else { + this.afterCallbacks.unshift(doAfter); + } +}; + +jasmine.Spec.prototype.execute = function(onComplete) { + var spec = this; + if (!spec.env.specFilter(spec)) { + spec.results_.skipped = true; + spec.finish(onComplete); + return; + } + + this.env.reporter.reportSpecStarting(this); + + spec.env.currentSpec = spec; + + spec.addBeforesAndAftersToQueue(); + + spec.queue.start(function () { + spec.finish(onComplete); + }); +}; + +jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { + var runner = this.env.currentRunner(); + var i; + + for (var suite = this.suite; suite; suite = suite.parentSuite) { + for (i = 0; i < suite.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); + } + } + for (i = 0; i < runner.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); + } + for (i = 0; i < this.afterCallbacks.length; i++) { + this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); + } + for (suite = this.suite; suite; suite = suite.parentSuite) { + for (i = 0; i < suite.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); + } + } + for (i = 0; i < runner.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); + } +}; + +jasmine.Spec.prototype.explodes = function() { + throw 'explodes function should not have been called'; +}; + +jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { + if (obj == jasmine.undefined) { + throw "spyOn could not find an object to spy upon for " + methodName + "()"; + } + + if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) { + throw methodName + '() method does not exist'; + } + + if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { + throw new Error(methodName + ' has already been spied upon'); + } + + var spyObj = jasmine.createSpy(methodName); + + this.spies_.push(spyObj); + spyObj.baseObj = obj; + spyObj.methodName = methodName; + spyObj.originalValue = obj[methodName]; + + obj[methodName] = spyObj; + + return spyObj; +}; + +jasmine.Spec.prototype.removeAllSpies = function() { + for (var i = 0; i < this.spies_.length; i++) { + var spy = this.spies_[i]; + spy.baseObj[spy.methodName] = spy.originalValue; + } + this.spies_ = []; +}; + +/** + * Internal representation of a Jasmine suite. + * + * @constructor + * @param {jasmine.Env} env + * @param {String} description + * @param {Function} specDefinitions + * @param {jasmine.Suite} parentSuite + */ +jasmine.Suite = function(env, description, specDefinitions, parentSuite) { + var self = this; + self.id = env.nextSuiteId ? env.nextSuiteId() : null; + self.description = description; + self.queue = new jasmine.Queue(env); + self.parentSuite = parentSuite; + self.env = env; + self.before_ = []; + self.after_ = []; + self.children_ = []; + self.suites_ = []; + self.specs_ = []; +}; + +jasmine.Suite.prototype.getFullName = function() { + var fullName = this.description; + for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) { + fullName = parentSuite.description + ' ' + fullName; + } + return fullName; +}; + +jasmine.Suite.prototype.finish = function(onComplete) { + this.env.reporter.reportSuiteResults(this); + this.finished = true; + if (typeof(onComplete) == 'function') { + onComplete(); + } +}; + +jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.unshift(beforeEachFunction); +}; + +jasmine.Suite.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.unshift(afterEachFunction); +}; + +jasmine.Suite.prototype.results = function() { + return this.queue.results(); +}; + +jasmine.Suite.prototype.add = function(suiteOrSpec) { + this.children_.push(suiteOrSpec); + if (suiteOrSpec instanceof jasmine.Suite) { + this.suites_.push(suiteOrSpec); + this.env.currentRunner().addSuite(suiteOrSpec); + } else { + this.specs_.push(suiteOrSpec); + } + this.queue.add(suiteOrSpec); +}; + +jasmine.Suite.prototype.specs = function() { + return this.specs_; +}; + +jasmine.Suite.prototype.suites = function() { + return this.suites_; +}; + +jasmine.Suite.prototype.children = function() { + return this.children_; +}; + +jasmine.Suite.prototype.execute = function(onComplete) { + var self = this; + this.queue.start(function () { + self.finish(onComplete); + }); +}; +jasmine.WaitsBlock = function(env, timeout, spec) { + this.timeout = timeout; + jasmine.Block.call(this, env, null, spec); +}; + +jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block); + +jasmine.WaitsBlock.prototype.execute = function (onComplete) { + this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...'); + this.env.setTimeout(function () { + onComplete(); + }, this.timeout); +}; +/** + * A block which waits for some condition to become true, with timeout. + * + * @constructor + * @extends jasmine.Block + * @param {jasmine.Env} env The Jasmine environment. + * @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true. + * @param {Function} latchFunction A function which returns true when the desired condition has been met. + * @param {String} message The message to display if the desired condition hasn't been met within the given time period. + * @param {jasmine.Spec} spec The Jasmine spec. + */ +jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { + this.timeout = timeout || env.defaultTimeoutInterval; + this.latchFunction = latchFunction; + this.message = message; + this.totalTimeSpentWaitingForLatch = 0; + jasmine.Block.call(this, env, null, spec); +}; +jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); + +jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10; + +jasmine.WaitsForBlock.prototype.execute = function(onComplete) { + this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen')); + var latchFunctionResult; + try { + latchFunctionResult = this.latchFunction.apply(this.spec); + } catch (e) { + this.spec.fail(e); + onComplete(); + return; + } + + if (latchFunctionResult) { + onComplete(); + } else if (this.totalTimeSpentWaitingForLatch >= this.timeout) { + var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen'); + this.spec.fail({ + name: 'timeout', + message: message + }); + + this.abort = true; + onComplete(); + } else { + this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; + var self = this; + this.env.setTimeout(function() { + self.execute(onComplete); + }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); + } +}; +// Mock setTimeout, clearTimeout +// Contributed by Pivotal Computer Systems, www.pivotalsf.com + +jasmine.FakeTimer = function() { + this.reset(); + + var self = this; + self.setTimeout = function(funcToCall, millis) { + self.timeoutsMade++; + self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false); + return self.timeoutsMade; + }; + + self.setInterval = function(funcToCall, millis) { + self.timeoutsMade++; + self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true); + return self.timeoutsMade; + }; + + self.clearTimeout = function(timeoutKey) { + self.scheduledFunctions[timeoutKey] = jasmine.undefined; + }; + + self.clearInterval = function(timeoutKey) { + self.scheduledFunctions[timeoutKey] = jasmine.undefined; + }; + +}; + +jasmine.FakeTimer.prototype.reset = function() { + this.timeoutsMade = 0; + this.scheduledFunctions = {}; + this.nowMillis = 0; +}; + +jasmine.FakeTimer.prototype.tick = function(millis) { + var oldMillis = this.nowMillis; + var newMillis = oldMillis + millis; + this.runFunctionsWithinRange(oldMillis, newMillis); + this.nowMillis = newMillis; +}; + +jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) { + var scheduledFunc; + var funcsToRun = []; + for (var timeoutKey in this.scheduledFunctions) { + scheduledFunc = this.scheduledFunctions[timeoutKey]; + if (scheduledFunc != jasmine.undefined && + scheduledFunc.runAtMillis >= oldMillis && + scheduledFunc.runAtMillis <= nowMillis) { + funcsToRun.push(scheduledFunc); + this.scheduledFunctions[timeoutKey] = jasmine.undefined; + } + } + + if (funcsToRun.length > 0) { + funcsToRun.sort(function(a, b) { + return a.runAtMillis - b.runAtMillis; + }); + for (var i = 0; i < funcsToRun.length; ++i) { + try { + var funcToRun = funcsToRun[i]; + this.nowMillis = funcToRun.runAtMillis; + funcToRun.funcToCall(); + if (funcToRun.recurring) { + this.scheduleFunction(funcToRun.timeoutKey, + funcToRun.funcToCall, + funcToRun.millis, + true); + } + } catch(e) { + } + } + this.runFunctionsWithinRange(oldMillis, nowMillis); + } +}; + +jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) { + this.scheduledFunctions[timeoutKey] = { + runAtMillis: this.nowMillis + millis, + funcToCall: funcToCall, + recurring: recurring, + timeoutKey: timeoutKey, + millis: millis + }; +}; + +/** + * @namespace + */ +jasmine.Clock = { + defaultFakeTimer: new jasmine.FakeTimer(), + + reset: function() { + jasmine.Clock.assertInstalled(); + jasmine.Clock.defaultFakeTimer.reset(); + }, + + tick: function(millis) { + jasmine.Clock.assertInstalled(); + jasmine.Clock.defaultFakeTimer.tick(millis); + }, + + runFunctionsWithinRange: function(oldMillis, nowMillis) { + jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis); + }, + + scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) { + jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring); + }, + + useMock: function() { + if (!jasmine.Clock.isInstalled()) { + var spec = jasmine.getEnv().currentSpec; + spec.after(jasmine.Clock.uninstallMock); + + jasmine.Clock.installMock(); + } + }, + + installMock: function() { + jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer; + }, + + uninstallMock: function() { + jasmine.Clock.assertInstalled(); + jasmine.Clock.installed = jasmine.Clock.real; + }, + + real: { + setTimeout: jasmine.getGlobal().setTimeout, + clearTimeout: jasmine.getGlobal().clearTimeout, + setInterval: jasmine.getGlobal().setInterval, + clearInterval: jasmine.getGlobal().clearInterval + }, + + assertInstalled: function() { + if (!jasmine.Clock.isInstalled()) { + throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); + } + }, + + isInstalled: function() { + return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer; + }, + + installed: null +}; +jasmine.Clock.installed = jasmine.Clock.real; + +//else for IE support +jasmine.getGlobal().setTimeout = function(funcToCall, millis) { + if (jasmine.Clock.installed.setTimeout.apply) { + return jasmine.Clock.installed.setTimeout.apply(this, arguments); + } else { + return jasmine.Clock.installed.setTimeout(funcToCall, millis); + } +}; + +jasmine.getGlobal().setInterval = function(funcToCall, millis) { + if (jasmine.Clock.installed.setInterval.apply) { + return jasmine.Clock.installed.setInterval.apply(this, arguments); + } else { + return jasmine.Clock.installed.setInterval(funcToCall, millis); + } +}; + +jasmine.getGlobal().clearTimeout = function(timeoutKey) { + if (jasmine.Clock.installed.clearTimeout.apply) { + return jasmine.Clock.installed.clearTimeout.apply(this, arguments); + } else { + return jasmine.Clock.installed.clearTimeout(timeoutKey); + } +}; + +jasmine.getGlobal().clearInterval = function(timeoutKey) { + if (jasmine.Clock.installed.clearTimeout.apply) { + return jasmine.Clock.installed.clearInterval.apply(this, arguments); + } else { + return jasmine.Clock.installed.clearInterval(timeoutKey); + } +}; + + +jasmine.version_= { + "major": 1, + "minor": 0, + "build": "0.rc1", + "revision": 1282853377 +}; diff --git a/javascript-koans/lib/jasmine/jskoans-jasmine-html.js b/javascript-koans/lib/jasmine/jskoans-jasmine-html.js new file mode 100644 index 0000000..06faea0 --- /dev/null +++ b/javascript-koans/lib/jasmine/jskoans-jasmine-html.js @@ -0,0 +1,245 @@ +JsKoansReporter = function(doc) { + this.document = doc || document; + this.suiteDivs = {}; + this.failedSpecs = 0; + + this.noOfSubjects = 0; + this.failedSubjects = 0; +}; + +JsKoansReporter.prototype.createDom = function(type, attrs, childrenVarArgs) { + var el = document.createElement(type); + + for (var i = 2; i < arguments.length; i++) { + var child = arguments[i]; + + if (typeof child === 'string') { + el.appendChild(document.createTextNode(child)); + } else { + if (child) { el.appendChild(child); } + } + } + + for (var attr in attrs) { + if (attr == "className") { + el[attr] = attrs[attr]; + } else { + el.setAttribute(attr, attrs[attr]); + } + } + + return el; +}; + +JsKoansReporter.prototype.reportRunnerStarting = function(runner) { + this.outerDiv = this.createDom('div', { className: 'jasmine_reporter show-passed' }, + this.createDom('h1', { }, "Javascript Koans"), + this.runnerDiv = this.createDom('div', { className: 'runner running' }, + this.runnerMessageSpan = this.createDom('span', { classname: 'running' }, "Contemplating naval..."), + this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, "")) + ); + + this.document.body.appendChild(this.outerDiv); + + var suites = runner.suites(); + for (var i = 0; i < suites.length; i++) { + var suite = suites[i]; + var suiteDiv = this.createDom('div', { className: 'suite' }, + this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, + this.getSuiteDescription(suite))); + this.suiteDivs[suite.id] = suiteDiv; + var parentDiv = this.outerDiv; + if (suite.parentSuite) { + parentDiv = this.suiteDivs[suite.parentSuite.id]; + } + parentDiv.appendChild(suiteDiv); + } + + this.footerDiv = this.createDom('div', { className: 'banner' }, + this.createDom('div', { className: 'logo' }, + "Test runner: Jasmine", + this.createDom('span', { className: 'version' }, runner.env.versionString())) + ); + + this.outerDiv.appendChild(this.footerDiv); + + this.startedAt = new Date(); +}; + +JsKoansReporter.prototype.reportRunnerResults = function(runner) { + var results = runner.results(); + var className = "progress"; + this.runnerDiv.setAttribute("class", className); + //do it twice for IE + this.runnerDiv.setAttribute("className", className); + var specs = runner.specs(); + var specCount = 0; + for (var i = 0; i < specs.length; i++) { + if (this.specFilter(specs[i])) { + specCount++; + } + } + + var enlightenmentMessage; + if (this.failedSpecs === 0) { + status = 'passed'; + enlightenmentMessage = "Enlightenment!"; + } else { + status = 'failed'; + enlightenmentMessage = "You have not yet reached enlightenment..."; + } + + var suitesCount = runner.suites().length; + var specsCount = runner.specs().length; + var showPassed; + var showAllFailed; + + var progress = this.createDom('div', {}, + this.createDom('div', { className: 'enlightenment-' + status }, enlightenmentMessage), + this.createDom('div', { className: 'completion' }, + this.createDom('div', {}, + this.createDom('span', { className: 'key' }, "Subjects covered: "), + this.createDom('span', { className: 'value' }, this.noOfSubjects - this.failedSubjects + "/" + this.noOfSubjects) + ), + this.createDom('div', {}, + this.createDom('span', { className: 'key' }, "Koans learned: "), + this.createDom('span', { className: 'value' }, specsCount - this.failedSpecs + "/" + runner.specs().length) + ), + this.createDom('div', { className: 'options' }, + this.createDom('label', { "for": "__jsKoans_showPassed__" }, " Show passed koans"), + showPassed = this.createDom('input', { id: "__jsKoans_showPassed__", type: 'checkbox', checked: '' }), + this.createDom('label', { "for": "__jsKoans_showAllFailed__" }, " Look ahead"), + showAllFailed = this.createDom('input', { id: "__jsKoans_showAllFailed__", type: 'checkbox' }) + ) + ) + ); + this.runnerMessageSpan.replaceChild(this.createDom('div', { className: 'description', href: '?'}, progress), this.runnerMessageSpan.firstChild); + + var self = this; + showPassed.onchange = function(evt) { + if (evt.target.checked) { + self.outerDiv.className += ' show-passed'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, ''); + } + }; + showAllFailed.onchange = function(evt) { + if (evt.target.checked) { + self.outerDiv.className += ' show-skipped'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, ''); + } + }; +}; + +JsKoansReporter.prototype.reportSuiteResults = function(suite) { + var results = suite.results(); + var status = results.passed() ? 'passed' : 'failed'; + if (results.totalCount == 0 || this.failedSubjects > 0) { + status += '-skipped'; + } + + if (suite.parentSuite == null) { + this.noOfSubjects +=1; + if (this.failedSpecs > 0) { + this.failedSubjects += 1; + } + } + + this.suiteDivs[suite.id].className += " " + status; +}; + +JsKoansReporter.prototype.reportSpecStarting = function(spec) { +}; + +JsKoansReporter.prototype.reportSpecResults = function(spec) { + var results = spec.results(); + var status = results.passed() ? 'passed' : 'failed'; + var skipStatus = status; + + if (results.skipped || this.failedSpecs > 0) { + skipStatus += '-skipped'; + } + + var description; + if ( !results.passed() ) { + this.failedSpecs += 1; + + description = "It " + spec.description + ". It is damaging your karma." + } else { + description = "Knowing it " + spec.description + " has expanded your awareness." + } + + var specDiv = this.createDom('div', { className: 'spec ' + skipStatus }, + this.createDom('a', { className: 'run_spec_' + status, href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "meditate again"), + this.createDom('a', { + className: 'description', + href: '?spec=' + encodeURIComponent(spec.getFullName()), + title: spec.getFullName() + }, description)); + + var resultItems = results.getItems(); + var messagesDiv = this.createDom('div', { className: 'messages'}); + + for (var i = 0; i < resultItems.length; i++) { + var result = resultItems[i]; + + if (result.type == 'expect' && result.passed && !result.passed()) { + messagesDiv.appendChild( + this.createDom('div', { className: 'errorMessage' }, result.message) + ); + messagesDiv.appendChild( + this.createDom('div', { className: 'description' }, "Please meditate on the following code:") + ); + + if (result.trace.stack) { + var lines = result.trace.stack.split('\n'); + var stack = lines[0]; + for (var i = 1; i < lines.length; i++) { + if (lines[i].search('/koans/') != -1) { + stack += '\n' + lines[i] + } + } + messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, stack.trim())); + } + + break; + } + } + if (messagesDiv.childNodes.length > 0) { + specDiv.appendChild(messagesDiv); + } + + this.suiteDivs[spec.suite.id].appendChild(specDiv); + +}; + +JsKoansReporter.prototype.log = function() { + var console = jasmine.getGlobal().console; + if (console && console.log) console.log.apply(console, arguments); +}; + +JsKoansReporter.prototype.getLocation = function() { + return this.document.location; +}; + +JsKoansReporter.prototype.specFilter = function(spec) { + var paramMap = {}; + var params = this.getLocation().search.substring(1).split('&'); + for (var i = 0; i < params.length; i++) { + var p = params[i].split('='); + paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); + } + + if (!paramMap["spec"]) return true; + return spec.getFullName().indexOf(paramMap["spec"]) == 0; +}; + +JsKoansReporter.prototype.getSuiteDescription = function(suite) { + if (null === suite.parentSuite) { + return "Thinking " + suite.description; + } else { + return "Considering " + suite.description; + } +}; + diff --git a/javascript-koans/lib/jasmine/jskoans-jasmine.css b/javascript-koans/lib/jasmine/jskoans-jasmine.css new file mode 100644 index 0000000..edc79ce --- /dev/null +++ b/javascript-koans/lib/jasmine/jskoans-jasmine.css @@ -0,0 +1,210 @@ +body { + font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; +} + +h1 { + text-align: center; + font-weight: bold; + color: #78f; +} + +.jasmine_reporter a:visited, .jasmine_reporter a { + color: #303; +} + +.jasmine_reporter a:hover, .jasmine_reporter a:active { + color: blue; +} + +.run_spec_failed, .run_spec_skipped { + float:right; + padding-right: 5px; + font-size: .8em; + text-decoration: none; +} + +.run_spec_passed { + display: none; +} + +.jasmine_reporter { + margin: 0 5px; +} + +.banner { + color: #303; + background-color: #fef; + padding: 5px; + font-size: 0.6em; +} + +.logo { +} + +.logo .version { + font-size: 0.9em; + padding-left: 1em; +} + +.runner.running { + background-color: yellow; +} + +.options { + padding-top: 0.5px; +} + +.suite { + border: 1px outset gray; + margin: 5px 0; + padding-left: 1em; +} + +.suite .suite { + margin: 5px; +} + +.suite.passed, .suite.passed-skipped { + background-color: #dfd; +} + +.suite.failed, .suite.failed-skipped { + background-color: #fdd; +} + +.spec { + margin: 5px; + padding-left: 1em; + clear: both; +} + +.spec.failed, .spec.passed, .spec.passed-skipped, .spec.failed-skipped { + padding-bottom: 5px; + border: 1px solid gray; +} + +.spec.failed, .spec.failed-skipped { + background-color: #fbb; + border-color: red; +} + +.spec.passed, .spec.passed-skipped { + background-color: #bfb; + border-color: green; +} + +.messages { + border-left: 1px; +} + +.errorMessage { + font-family: "Menlo", "Monaco", "Andale Mono", "Courier New", "Courier", sans-serif; + color: #055; + white-space: pre; + padding: .2em 1em; + margin-left: 10px; + margin-right: 5px; + margin-bottom: 10px; + background: #eef; +} + +.meditate { + padding-top: 1em; +} + +.passed { + background-color: #cfc; + display: none; +} + +.failed { + background-color: #fbb; + display: block; +} + +.passed-skipped, .failed-skipped { + display: none; +} + +.resultMessage span.result { + display: block; + line-height: 2em; + color: black; +} + +.resultMessage .mismatch { + color: black; +} + +.stackTrace { + white-space: pre; + font-size: .8em; + margin-left: 10px; + margin-right: 5px; + max-height: 5em; + overflow: auto; + border: 1px inset red; + padding: 1em; + color: #eef; + background: #000; +} + +.finished-at { + padding-left: 1em; + font-size: .6em; +} + +.show-skipped .passed-skipped { + display: block; +} + +.show-skipped .failed-skipped { + display: block; +} + +.show-passed .passed { + display: block; +} +.show-failed .failed { + display: block; +} + +#jasmine_content { + position:fixed; + right: 100%; +} + +.runner { + border: 1px solid gray; + display: block; + margin: 5px 0; + padding: 2px 0 2px 10px; +} + +.progress { +} + +.completion { + font-size: .9em; + padding-top: 10px; +} + +.key { + color: #077; +} + +.value { + color: #444; +} + +.description { + text-decoration: none; +} + +.enlightenment-passed { + color: #990; + font-weight: bold; +} +.enlightenment-failed { + color: #550; +} diff --git a/javascript-koans/lib/jsTestDriver/JsTestDriver.jar b/javascript-koans/lib/jsTestDriver/JsTestDriver.jar new file mode 100644 index 0000000..8541aab Binary files /dev/null and b/javascript-koans/lib/jsTestDriver/JsTestDriver.jar differ diff --git a/javascript-koans/lib/jsTestDriver/capture_browser.sh b/javascript-koans/lib/jsTestDriver/capture_browser.sh new file mode 100755 index 0000000..e2cc1e4 --- /dev/null +++ b/javascript-koans/lib/jsTestDriver/capture_browser.sh @@ -0,0 +1 @@ +java -jar JsTestDriver.jar --port 42442 --runnerMode DEBUG --browser /Applications/Firefox-3.app/Contents/MacOS/firefox-bin \ No newline at end of file diff --git a/javascript-koans/lib/jsTestDriver/jsTestDriver.conf b/javascript-koans/lib/jsTestDriver/jsTestDriver.conf new file mode 100644 index 0000000..436f176 --- /dev/null +++ b/javascript-koans/lib/jsTestDriver/jsTestDriver.conf @@ -0,0 +1,16 @@ +server: http://localhost:42442 + +load: + - src/dojo-release-1.5.0/dojo/dojo.js + - src/dojo-release-1.5.0/dojox/lang/functional/*.js + - src/*.js + + - src-test/PathToEnlightenment.js + - src-test/AboutAsserts.js + - src-test/AboutArrays.js + - src-test/AboutFunctions.js + - src-test/AboutObjects.js + - src-test/AboutMutability.js + - src-test/AboutLambda.js + - src-test/AboutHigherOrderFunctions.js + - src-test/ApplyingWhatWeveLearnt.js diff --git a/javascript-koans/lib/jsTestDriver/run_all_koans.sh b/javascript-koans/lib/jsTestDriver/run_all_koans.sh new file mode 100755 index 0000000..98ff7a8 --- /dev/null +++ b/javascript-koans/lib/jsTestDriver/run_all_koans.sh @@ -0,0 +1 @@ +java -jar JsTestDriver.jar --reset --tests all \ No newline at end of file diff --git a/javascript-koans/lib/underscore-min.js b/javascript-koans/lib/underscore-min.js new file mode 100644 index 0000000..f502cf9 --- /dev/null +++ b/javascript-koans/lib/underscore-min.js @@ -0,0 +1,26 @@ +// Underscore.js 1.1.6 +// (c) 2011 Jeremy Ashkenas, DocumentCloud Inc. +// Underscore is freely distributable under the MIT license. +// Portions of Underscore are inspired or borrowed from Prototype, +// Oliver Steele's Functional, and John Resig's Micro-Templating. +// For all details and documentation: +// http://documentcloud.github.com/underscore +(function(){var p=this,C=p._,m={},i=Array.prototype,n=Object.prototype,f=i.slice,D=i.unshift,E=n.toString,l=n.hasOwnProperty,s=i.forEach,t=i.map,u=i.reduce,v=i.reduceRight,w=i.filter,x=i.every,y=i.some,o=i.indexOf,z=i.lastIndexOf;n=Array.isArray;var F=Object.keys,q=Function.prototype.bind,b=function(a){return new j(a)};typeof module!=="undefined"&&module.exports?(module.exports=b,b._=b):p._=b;b.VERSION="1.1.6";var h=b.each=b.forEach=function(a,c,d){if(a!=null)if(s&&a.forEach===s)a.forEach(c,d);else if(b.isNumber(a.length))for(var e= +0,k=a.length;e=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a, +c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};h(a,function(a,b,f){b=c?c.call(d,a,b,f):a;bd?1:0}),"value")};b.sortedIndex=function(a,c,d){d||(d=b.identity);for(var e=0,f=a.length;e>1;d(a[g])=0})})};b.zip=function(){for(var a=f.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c), +e=0;e=0;d--)b=[a[d].apply(this,b)];return b[0]}};b.after=function(a,b){return function(){if(--a<1)return b.apply(this,arguments)}};b.keys=F||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var b=[],d;for(d in a)l.call(a,d)&&(b[b.length]=d);return b};b.values=function(a){return b.map(a, +b.identity)};b.functions=b.methods=function(a){return b.filter(b.keys(a),function(c){return b.isFunction(a[c])}).sort()};b.extend=function(a){h(f.call(arguments,1),function(b){for(var d in b)b[d]!==void 0&&(a[d]=b[d])});return a};b.defaults=function(a){h(f.call(arguments,1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a};b.clone=function(a){return b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};b.isEqual=function(a,c){if(a===c)return!0;var d=typeof a;if(d!= +typeof c)return!1;if(a==c)return!0;if(!a&&c||a&&!c)return!1;if(a._chain)a=a._wrapped;if(c._chain)c=c._wrapped;if(a.isEqual)return a.isEqual(c);if(b.isDate(a)&&b.isDate(c))return a.getTime()===c.getTime();if(b.isNaN(a)&&b.isNaN(c))return!1;if(b.isRegExp(a)&&b.isRegExp(c))return a.source===c.source&&a.global===c.global&&a.ignoreCase===c.ignoreCase&&a.multiline===c.multiline;if(d!=="object")return!1;if(a.length&&a.length!==c.length)return!1;d=b.keys(a);var e=b.keys(c);if(d.length!=e.length)return!1; +for(var f in a)if(!(f in c)||!b.isEqual(a[f],c[f]))return!1;return!0};b.isEmpty=function(a){if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(l.call(a,c))return!1;return!0};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=n||function(a){return E.call(a)==="[object Array]"};b.isArguments=function(a){return!(!a||!l.call(a,"callee"))};b.isFunction=function(a){return!(!a||!a.constructor||!a.call||!a.apply)};b.isString=function(a){return!!(a===""||a&&a.charCodeAt&&a.substr)}; +b.isNumber=function(a){return!!(a===0||a&&a.toExponential&&a.toFixed)};b.isNaN=function(a){return a!==a};b.isBoolean=function(a){return a===!0||a===!1};b.isDate=function(a){return!(!a||!a.getTimezoneOffset||!a.setUTCFullYear)};b.isRegExp=function(a){return!(!a||!a.test||!a.exec||!(a.ignoreCase||a.ignoreCase===!1))};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.noConflict=function(){p._=C;return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e= +0;e/g,interpolate:/<%=([\s\S]+?)%>/g};b.template=function(a,c){var d=b.templateSettings;d="var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+a.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(d.interpolate,function(a,b){return"',"+b.replace(/\\'/g,"'")+",'"}).replace(d.evaluate|| +null,function(a,b){return"');"+b.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');";d=new Function("obj",d);return c?d(c):d};var j=function(a){this._wrapped=a};b.prototype=j.prototype;var r=function(a,c){return c?b(a).chain():a},H=function(a,c){j.prototype[a]=function(){var a=f.call(arguments);D.call(a,this._wrapped);return r(c.apply(b,a),this._chain)}};b.mixin(b);h(["pop","push","reverse","shift","sort", +"splice","unshift"],function(a){var b=i[a];j.prototype[a]=function(){b.apply(this._wrapped,arguments);return r(this._wrapped,this._chain)}});h(["concat","join","slice"],function(a){var b=i[a];j.prototype[a]=function(){return r(b.apply(this._wrapped,arguments),this._chain)}});j.prototype.chain=function(){this._chain=!0;return this};j.prototype.value=function(){return this._wrapped}})(); diff --git a/js-assessment/.DS_Store b/js-assessment/.DS_Store new file mode 100644 index 0000000..f062d05 Binary files /dev/null and b/js-assessment/.DS_Store differ diff --git a/js-assessment/.gitignore b/js-assessment/.gitignore new file mode 100644 index 0000000..71b051e --- /dev/null +++ b/js-assessment/.gitignore @@ -0,0 +1,2 @@ +npm-debug.log +node_modules/ diff --git a/js-assessment/.travis.yml b/js-assessment/.travis.yml new file mode 100644 index 0000000..48246e1 --- /dev/null +++ b/js-assessment/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: "4" +script: "npm run lint" diff --git a/js-assessment/CHANGELOG.md b/js-assessment/CHANGELOG.md new file mode 100644 index 0000000..80a2b4a --- /dev/null +++ b/js-assessment/CHANGELOG.md @@ -0,0 +1,18 @@ +# Change Log + +## 0.3.0 + +- Remove jquery, backbone, underscore, mocha, and chai as local libraries +- Remove RequireJS + - node deps use native `require()` + - browser deps are `script` tags + +## 0.2.0 + +- Convert app from Express to using serve-static +- Replace bin/server with a `grunt connect` task and `npm start` script +- Update dependencies + +## < 0.1.0 + +- Initial commits. Hello World! diff --git a/js-assessment/README.md b/js-assessment/README.md new file mode 100644 index 0000000..5cdcafe --- /dev/null +++ b/js-assessment/README.md @@ -0,0 +1,87 @@ +[![Build Status](https://travis-ci.org/rmurphey/js-assessment.svg?branch=master)](https://travis-ci.org/rmurphey/js-assessment) + +# A test-driven JS assessment + +This repo includes a set of tests that can be used to assess the skills of +a candidate for a JavaScript position, or to evaluate and improve one's own +skills. + +## I want to work on the tests; what do I do? +To use the tests, you will need to install [Node](https://nodejs.org/). Note +that on Windows, there are some reports that you will need to restart +after installing Node - see #12. + +You can clone or download this repo. Once you have done so, from the root +directory of the repo, run: + + npm install + npm start + +You can then view the tests in your browser at +[http://localhost:4444](http://localhost:4444). + +When you visit that page, all of the tests should be failing; your job is to +get the tests to pass. To do this, you'll need to refer to the tests in the +files in the `tests/app` directory, and edit the files in the `app/` directory. +Once you update a test, you can reload the test page in the browser to see +whether it worked. + +You can also run (most of) the tests on the command line: + + npm test + +The command line runner is a work in progress; contributions welcome :) + +### Available dependencies + +The repo includes jQuery, Backbone, and Underscore. You can use these +libraries when writing your solutions! + +## I want to contribute tests; what do I do? + +Submit a pull request! The tests are currently loosely organized by topic, so +you should do your best to add tests to the appropriate file in `tests/app`, or +create a new file there if you don't see an appropriate one. If you do create +a new file, make sure to add it to `tests/runner.js`, and to add a stub for the +solution to the corresponding file in `app/`. Finally, it would be great if you +could update the [answers](https://github.com/rmurphey/js-assessment-answers) +as well. + +If you're not sure how or where to add a test, please open an issue. + +### Data-driven tests + +If your tests need data that can be fetched via XHR, stick a `.json` file in +the `data` directory; you can access it at `/data/.json`. + +## I want to see the answers! + +First, bear in mind that looking up the answers is going to teach you a whole +lot less than you'll learn by working on the tests, even if you occasionally get +stuck. I'd recommend only looking at the answers once you have the tests +passing, to see if there's another way you could have approached the +problem. When you're ready to look at the answers, you can find them +[here](https://github.com/rmurphey/js-assessment-answers); I'll do my best to +keep them up to date. + +## I hate \ + +This repo uses [Mocha](https://github.com/mochajs/mocha) and +[Chai](http://chaijs.com/) for the tests themselves. It uses the BDD style for authoring tests. +If this doesn't suit you, please fork away, or, better, submit a pull request that lets +this be more flexible than it currently is. + +# Todos + +There are a number of things that would make this project better; check out the +[issues](https://github.com/rmurphey/js-assessment/issues) for details, pull +requests welcome! + +# License + +Copyright © 2012-2016 Rebecca Murphey with many thanks to several +[contributors](https://github.com/rmurphey/js-assessment/graphs/contributors). + +Creative Commons License + +This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. diff --git a/js-assessment/app/.eslintrc.js b/js-assessment/app/.eslintrc.js new file mode 100644 index 0000000..a4b2620 --- /dev/null +++ b/js-assessment/app/.eslintrc.js @@ -0,0 +1,194 @@ +module.exports = { + "env": { + "browser": true, + "node": true + }, + "globals": { + "exports": false, + }, + "extends": "eslint:recommended", + "rules": { + "accessor-pairs": "error", + "array-callback-return": "error", + "arrow-body-style": "error", + "arrow-parens": "error", + "arrow-spacing": "error", + "block-spacing": [ + "error", + "always" + ], + "callback-return": "error", + "camelcase": "error", + "comma-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "comma-style": [ + "error", + "last" + ], + "complexity": "error", + "computed-property-spacing": [ + "error", + "never" + ], + "consistent-return": "error", + "consistent-this": "error", + "curly": "error", + "default-case": "error", + "dot-location": "error", + "dot-notation": "error", + "eol-last": "error", + "eqeqeq": "error", + "generator-star-spacing": "error", + "guard-for-in": "error", + "handle-callback-err": "error", + "id-blacklist": "error", + "id-match": "error", + "indent": [ "error", 2 ], + "jsx-quotes": "error", + "key-spacing": [ + "error", { + "beforeColon": false, + "afterColon": true + } + ], + "keyword-spacing": "error", + "linebreak-style": [ + "error", + "unix" + ], + "lines-around-comment": "error", + "max-depth": "error", + "max-len": [ "error", 120 ], + "max-nested-callbacks": "error", + "max-params": "error", + "new-cap": "error", + "new-parens": "error", + "newline-per-chained-call": "error", + "no-alert": "error", + "no-array-constructor": "error", + "no-bitwise": "error", + "no-caller": "error", + "no-catch-shadow": "error", + "no-confusing-arrow": "error", + "no-continue": "error", + "no-div-regex": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-empty-function": "off", + "no-eq-null": "error", + "no-eval": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-label": "error", + "no-extra-parens": "error", + "no-floating-decimal": "error", + "no-implicit-coercion": "error", + "no-implicit-globals": "warn", + "no-implied-eval": "error", + "no-inner-declarations": [ + "error", + "functions" + ], + "no-invalid-this": "error", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "error", + "no-loop-func": "error", + "no-mixed-requires": "error", + "no-multi-spaces": "error", + "no-multi-str": "error", + "no-multiple-empty-lines": "error", + "no-negated-condition": "error", + "no-nested-ternary": "error", + "no-new": "error", + "no-new-func": "error", + "no-new-object": "error", + "no-new-require": "error", + "no-new-wrappers": "error", + "no-octal-escape": "error", + "no-param-reassign": "error", + "no-path-concat": "error", + "no-process-env": "error", + "no-process-exit": "error", + "no-proto": "error", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-syntax": "error", + "no-return-assign": "error", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-shadow": "error", + "no-shadow-restricted-names": "error", + "no-spaced-func": "error", + "no-sync": "error", + "no-throw-literal": "error", + "no-trailing-spaces": "error", + "no-undef-init": "error", + "no-undefined": "error", + "no-underscore-dangle": "error", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "error", + "no-unused-vars": "off", + "no-use-before-define": "error", + "no-useless-call": "error", + "no-useless-concat": "error", + "no-useless-constructor": "error", + "no-useless-escape": "error", + "no-void": "error", + "no-whitespace-before-property": "error", + "no-with": "error", + "object-curly-spacing": "error", + "one-var-declaration-per-line": "error", + "operator-assignment": "error", + "operator-linebreak": "error", + "prefer-const": "error", + "prefer-reflect": "error", + "prefer-spread": "error", + "quotes": [ + "error", + "single" + ], + "radix": "error", + "semi": "error", + "semi-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "sort-imports": "error", + "sort-vars": "error", + "space-before-blocks": "off", + "space-before-function-paren": "off", + "space-in-parens": "off", + "space-infix-ops": "error", + "space-unary-ops": "error", + "spaced-comment": [ + "error", + "always" + ], + "strict": [ + "error", + "never" + ], + "template-curly-spacing": "error", + "valid-jsdoc": "error", + "wrap-iife": "error", + "wrap-regex": "error", + "yield-star-spacing": "error", + "yoda": [ + "error", + "never" + ] + } +}; diff --git a/js-assessment/app/arrays.js b/js-assessment/app/arrays.js new file mode 100644 index 0000000..0e1d7f9 --- /dev/null +++ b/js-assessment/app/arrays.js @@ -0,0 +1,59 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.arraysAnswers = { + indexOf: function(arr, item) { + + }, + + sum: function(arr) { + + }, + + remove: function(arr, item) { + + }, + + removeWithoutCopy: function(arr, item) { + + }, + + append: function(arr, item) { + + }, + + truncate: function(arr) { + + }, + + prepend: function(arr, item) { + + }, + + curtail: function(arr) { + + }, + + concat: function(arr1, arr2) { + + }, + + insert: function(arr, item, index) { + + }, + + count: function(arr, item) { + + }, + + duplicates: function(arr) { + + }, + + square: function(arr) { + + }, + + findAllOccurrences: function(arr, target) { + + } +}; diff --git a/js-assessment/app/async.js b/js-assessment/app/async.js new file mode 100644 index 0000000..e01759c --- /dev/null +++ b/js-assessment/app/async.js @@ -0,0 +1,11 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.asyncAnswers = { + async: function(value) { + + }, + + manipulateRemoteData: function(url) { + + } +}; diff --git a/js-assessment/app/bestPractices.js b/js-assessment/app/bestPractices.js new file mode 100644 index 0000000..a7195b3 --- /dev/null +++ b/js-assessment/app/bestPractices.js @@ -0,0 +1,26 @@ +/* eslint-disable */ +exports = typeof window === 'undefined' ? global : window; + +/** + * This file defines an object with some methods. Some of these methods are + * populated incorrectly; your job is to fix them. Other methods are not + * populated at all; your job is to fill them out. + */ + +exports.bestPracticesAnswers = { + globals: function() { + myObject = { + name: 'Jory' + }; + + return myObject; + }, + + parseInt: function(num) { + return parseInt(num); + }, + + identity: function(val1, val2) { + + } +}; diff --git a/js-assessment/app/count.js b/js-assessment/app/count.js new file mode 100644 index 0000000..10056bf --- /dev/null +++ b/js-assessment/app/count.js @@ -0,0 +1,7 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.countAnswers = { + count: function (start, end) { + + } +}; diff --git a/js-assessment/app/flowControl.js b/js-assessment/app/flowControl.js new file mode 100644 index 0000000..89cd2ea --- /dev/null +++ b/js-assessment/app/flowControl.js @@ -0,0 +1,15 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.flowControlAnswers = { + fizzBuzz: function(num) { + // write a function that receives a number as its argument; + // if the number is divisible by 3, the function should return 'fizz'; + // if the number is divisible by 5, the function should return 'buzz'; + // if the number is divisible by 3 and 5, the function should return + // 'fizzbuzz'; + // + // otherwise the function should return the number, or false if no number + // was provided or the value provided is not a number + + } +}; diff --git a/js-assessment/app/functions.js b/js-assessment/app/functions.js new file mode 100644 index 0000000..07fa745 --- /dev/null +++ b/js-assessment/app/functions.js @@ -0,0 +1,39 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.functionsAnswers = { + argsAsArray: function(fn, arr) { + + }, + + speak: function(fn, obj) { + + }, + + functionFunction: function(str) { + + }, + + makeClosures: function(arr, fn) { + + }, + + partial: function(fn, str1, str2) { + + }, + + useArguments: function() { + + }, + + callIt: function(fn) { + + }, + + partialUsingArguments: function(fn) { + + }, + + curryIt: function(fn) { + + } +}; diff --git a/js-assessment/app/logicalOperators.js b/js-assessment/app/logicalOperators.js new file mode 100644 index 0000000..e266ea2 --- /dev/null +++ b/js-assessment/app/logicalOperators.js @@ -0,0 +1,11 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.logicalOperatorsAnswers = { + or: function(a, b) { + + }, + + and: function(a, b) { + + } +}; diff --git a/js-assessment/app/modules.js b/js-assessment/app/modules.js new file mode 100644 index 0000000..9f09589 --- /dev/null +++ b/js-assessment/app/modules.js @@ -0,0 +1,7 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.modulesAnswers = { + createModule: function(str1, str2) { + + } +}; diff --git a/js-assessment/app/numbers.js b/js-assessment/app/numbers.js new file mode 100644 index 0000000..823da34 --- /dev/null +++ b/js-assessment/app/numbers.js @@ -0,0 +1,19 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.numbersAnswers = { + valueAtBit: function(num, bit) { + + }, + + base10: function(str) { + + }, + + convertToBinary: function(num) { + + }, + + multiply: function(a, b) { + + } +}; diff --git a/js-assessment/app/objects.js b/js-assessment/app/objects.js new file mode 100644 index 0000000..f549d53 --- /dev/null +++ b/js-assessment/app/objects.js @@ -0,0 +1,15 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.objectsAnswers = { + alterContext: function(fn, obj) { + + }, + + alterObjects: function(constructor, greeting) { + + }, + + iterate: function(obj) { + + } +}; diff --git a/js-assessment/app/recursion.js b/js-assessment/app/recursion.js new file mode 100644 index 0000000..13b69c2 --- /dev/null +++ b/js-assessment/app/recursion.js @@ -0,0 +1,19 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.recursionAnswers = { + listFiles: function(data, dirName) { + + }, + + permute: function(arr) { + + }, + + fibonacci: function(n) { + + }, + + validParentheses: function(n) { + + } +}; diff --git a/js-assessment/app/regex.js b/js-assessment/app/regex.js new file mode 100644 index 0000000..ddae5de --- /dev/null +++ b/js-assessment/app/regex.js @@ -0,0 +1,27 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.regexAnswers = { + containsNumber: function(str) { + + }, + + containsRepeatingLetter: function(str) { + + }, + + endsWithVowel: function(str) { + + }, + + captureThreeNumbers: function(str) { + + }, + + matchesPattern: function(str) { + + }, + + isUSD: function(str) { + + } +}; diff --git a/js-assessment/app/strings.js b/js-assessment/app/strings.js new file mode 100644 index 0000000..a874122 --- /dev/null +++ b/js-assessment/app/strings.js @@ -0,0 +1,15 @@ +exports = typeof window === 'undefined' ? global : window; + +exports.stringsAnswers = { + reduceString: function(str, amount) { + + }, + + wordWrap: function(str, cols) { + + }, + + reverseString: function(str) { + + } +}; diff --git a/js-assessment/data/testdata.json b/js-assessment/data/testdata.json new file mode 100644 index 0000000..f86fce5 --- /dev/null +++ b/js-assessment/data/testdata.json @@ -0,0 +1,9 @@ +{ + "people" : [ + { "name" : "Matt" }, + { "name" : "Rebecca" }, + { "name" : "Paul" }, + { "name" : "Alex" }, + { "name" : "Adam" } + ] +} diff --git a/js-assessment/help.txt b/js-assessment/help.txt new file mode 100644 index 0000000..4713622 --- /dev/null +++ b/js-assessment/help.txt @@ -0,0 +1,8 @@ +To work on the tests, open a browser and visit http://{{host}}:{{port}}. + +The tests are in files in the tests/app directory; you will need to fill out +the answers in the corresponding files in the app directory, your browser will +reload when you save the files. + +For some tests, the instructions will be more involved; in those cases, the +inline comments should give you the details you need. diff --git a/js-assessment/index.js b/js-assessment/index.js new file mode 100644 index 0000000..b13242e --- /dev/null +++ b/js-assessment/index.js @@ -0,0 +1,24 @@ +const fs = require('fs'); +const browserSync = require('browser-sync').create(); + +const port = process.env.PORT || '4444'; +const host = process.env.HOST || '127.0.0.1'; + +browserSync.init({ + server: { + baseDir: __dirname, + index: 'tests/runner.html' + }, + files: ['app/**/*.js'], + host: host, + port: port, + open: false, + notify: false, + ui: false, + ghostMode: false, + logLevel: 'silent' +}); + +f = fs.readFileSync(__dirname + '/help.txt', 'utf8'); +console.log(f.replace('{{host}}', host).replace('{{port}}', port)); +console.log('Server running http://%s:%d', host, port); diff --git a/js-assessment/lib/plugins/text.js b/js-assessment/lib/plugins/text.js new file mode 100644 index 0000000..6f75417 --- /dev/null +++ b/js-assessment/lib/plugins/text.js @@ -0,0 +1,11 @@ +/* + RequireJS text 1.0.7 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved. + Available via the MIT or new BSD license. + see: http://github.com/jrburke/requirejs for details +*/ +(function(){var k=["Msxml2.XMLHTTP","Microsoft.XMLHTTP","Msxml2.XMLHTTP.4.0"],n=/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,o=/]*>\s*([\s\S]+)\s*<\/body>/im,i=typeof location!=="undefined"&&location.href,p=i&&location.protocol&&location.protocol.replace(/\:/,""),q=i&&location.hostname,r=i&&(location.port||void 0),j=[];define(function(){var g,h,l;typeof window!=="undefined"&&window.navigator&&window.document?h=function(a,c){var b=g.createXhr();b.open("GET",a,!0);b.onreadystatechange= +function(){b.readyState===4&&c(b.responseText)};b.send(null)}:typeof process!=="undefined"&&process.versions&&process.versions.node?(l=require.nodeRequire("fs"),h=function(a,c){var b=l.readFileSync(a,"utf8");b.indexOf("\ufeff")===0&&(b=b.substring(1));c(b)}):typeof Packages!=="undefined"&&(h=function(a,c){var b=new java.io.File(a),e=java.lang.System.getProperty("line.separator"),b=new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(b),"utf-8")),d,f,g="";try{d=new java.lang.StringBuffer; +(f=b.readLine())&&f.length()&&f.charAt(0)===65279&&(f=f.substring(1));for(d.append(f);(f=b.readLine())!==null;)d.append(e),d.append(f);g=String(d.toString())}finally{b.close()}c(g)});return g={version:"1.0.7",strip:function(a){if(a){var a=a.replace(n,""),c=a.match(o);c&&(a=c[1])}else a="";return a},jsEscape:function(a){return a.replace(/(['\\])/g,"\\$1").replace(/[\f]/g,"\\f").replace(/[\b]/g,"\\b").replace(/[\n]/g,"\\n").replace(/[\t]/g,"\\t").replace(/[\r]/g,"\\r")},createXhr:function(){var a,c, +b;if(typeof XMLHttpRequest!=="undefined")return new XMLHttpRequest;else for(c=0;c<3;c++){b=k[c];try{a=new ActiveXObject(b)}catch(e){}if(a){k=[b];break}}if(!a)throw Error("createXhr(): XMLHttpRequest not available");return a},get:h,parseName:function(a){var c=!1,b=a.indexOf("."),e=a.substring(0,b),a=a.substring(b+1,a.length),b=a.indexOf("!");b!==-1&&(c=a.substring(b+1,a.length),c=c==="strip",a=a.substring(0,b));return{moduleName:e,ext:a,strip:c}},xdRegExp:/^((\w+)\:)?\/\/([^\/\\]+)/,useXhr:function(a, +c,b,e){var d=g.xdRegExp.exec(a),f;if(!d)return!0;a=d[2];d=d[3];d=d.split(":");f=d[1];d=d[0];return(!a||a===c)&&(!d||d===b)&&(!f&&!d||f===e)},finishLoad:function(a,c,b,e,d){b=c?g.strip(b):b;d.isBuild&&(j[a]=b);e(b)},load:function(a,c,b,e){if(e.isBuild&&!e.inlineText)b();else{var d=g.parseName(a),f=d.moduleName+"."+d.ext,m=c.toUrl(f),h=e&&e.text&&e.text.useXhr||g.useXhr;!i||h(m,p,q,r)?g.get(m,function(c){g.finishLoad(a,d.strip,c,b,e)}):c([f],function(a){g.finishLoad(d.moduleName+"."+d.ext,d.strip,a, +b,e)})}},write:function(a,c,b){if(c in j){var e=g.jsEscape(j[c]);b.asModule(a+"!"+c,"define(function () { return '"+e+"';});\n")}},writeFile:function(a,c,b,e,d){var c=g.parseName(c),f=c.moduleName+"."+c.ext,h=b.toUrl(c.moduleName+"."+c.ext)+".js";g.load(f,b,function(){var b=function(a){return e(h,a)};b.asModule=function(a,b){return e.asModule(a,h,b)};g.write(a,f,b,d)},d)}}})})(); \ No newline at end of file diff --git a/js-assessment/lib/plugins/use.js b/js-assessment/lib/plugins/use.js new file mode 100644 index 0000000..fa47392 --- /dev/null +++ b/js-assessment/lib/plugins/use.js @@ -0,0 +1,95 @@ +if (typeof define !== 'function') { var define = require('amdefine')(module); } + +/* RequireJS Use Plugin v0.2.0 + * Copyright 2012, Tim Branyen (@tbranyen) + * use.js may be freely distributed under the MIT license. + */ +(function() { + +// Cache used to map configuration options between load and write. +var buildMap = {}; + +define({ + version: "0.2.0", + + // Invoked by the AMD builder, passed the path to resolve, the require + // function, done callback, and the configuration options. + load: function(name, req, load, config) { + // Dojo provides access to the config object through the req function. + if (!config) { + config = require.rawConfig; + } + + var module = config.use && config.use[name]; + + // No module to load, throw. + if (!module) { + throw new TypeError("Module '" + name + "' is undefined or does not" + + " have a `use` config. Make sure it exists, add a `use` config, or" + + " don't use use! on it"); + } + + // Attach to the build map for use in the write method below. + buildMap[name] = { deps: module.deps || [], attach: module.attach }; + + // Read the current module configuration for any dependencies that are + // required to run this particular non-AMD module. + req(module.deps || [], function() { + var depArgs = arguments; + // Require this module + req([name], function() { + // Attach property + var attach = module.attach; + + // If doing a build don't care about loading + if (config.isBuild) { + return load(); + } + + // Return the correct attached object + if (typeof attach === "function") { + return load(attach.apply(this, depArgs)); + } + + // Use window for now (maybe this?) + return load(this[attach]); + }); + }); + }, + + // Also invoked by the AMD builder, this writes out a compatible define + // call that will work with loaders such as almond.js that cannot read + // the configuration data. + write: function(pluginName, moduleName, write) { + var module = buildMap[moduleName]; + var deps = module.deps; + var normalize = { attach: null, deps: "" }; + + // Normalize the attach to window[name] or function() { } + if (typeof module.attach === "function") { + normalize.attach = module.attach.toString(); + } else { + normalize.attach = [ + "function() {", + "return typeof ", module.attach, + " !== \"undefined\" ? ", module.attach, " : void 0;", + "}" + ].join(""); + } + + // Normalize the dependencies to have proper string characters + if (deps.length) { + normalize.deps = "'" + deps.toString().split(",").join("','") + "'"; + } + + // Write out the actual definition + write([ + "define('", pluginName, "!", moduleName, "', ", + "[", normalize.deps, "], ", normalize.attach, + ");\n" + ].join("")); + } +}); + +})(); + diff --git a/js-assessment/package.json b/js-assessment/package.json new file mode 100644 index 0000000..16b8641 --- /dev/null +++ b/js-assessment/package.json @@ -0,0 +1,37 @@ +{ + "author": "Rebecca Murphey (http://rmurphey.com)", + "name": "js-assessment", + "description": "A test-driven assessment of JavaScript skills", + "version": "0.3.0", + "homepage": "http://rmurphey.com", + "repository": { + "type": "git", + "url": "git://github.com/rmurphey/js-assessment.git" + }, + "engines": { + "node": "0.12.x" + }, + "dependencies": { + "backbone": "^1.1.2", + "browser-sync": "^2.8.2", + "chai": "^2.3.0", + "expect.js": "0.1.2", + "jquery": "^1.11.0", + "mocha": "^2.2.4", + "sinon": "^1.14.1", + "underscore": "1.8.x" + }, + "scripts": { + "start": "node index.js", + "test": "mocha -R spec 'tests/app'", + "lint": "eslint --quiet tests/app app", + "lint-warn": "eslint tests/app app" + }, + "bugs": { + "url": "https://github.com/rmurphey/js-assessment/issues" + }, + "private": true, + "devDependencies": { + "eslint": "^2.8.0" + } +} diff --git a/js-assessment/tests/app/.eslintrc.js b/js-assessment/tests/app/.eslintrc.js new file mode 100644 index 0000000..6bfcf9b --- /dev/null +++ b/js-assessment/tests/app/.eslintrc.js @@ -0,0 +1,213 @@ +module.exports = { + "env": { + "browser": true + }, + "globals": { + "arraysAnswers": false, + "asyncAnswers": false, + "bestPracticesAnswers": false, + "countAnswers": false, + "flowControlAnswers": false, + "functionsAnswers": false, + "logicalOperatorsAnswers": false, + "modulesAnswers": false, + "numbersAnswers": false, + "objectsAnswers": false, + "recursionAnswers": false, + "regexAnswers": false, + "stringsAnswers": false, + + "it": false, + "require": false, + "describe": false, + "beforeEach": false, + "afterEach": false, + "before": false, + "after": false + }, + "extends": "eslint:recommended", + "rules": { + "accessor-pairs": "error", + "array-callback-return": "error", + "arrow-body-style": "error", + "arrow-parens": "error", + "arrow-spacing": "error", + "block-spacing": [ + "error", + "always" + ], + "callback-return": "error", + "camelcase": "error", + "comma-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "comma-style": [ + "error", + "last" + ], + "complexity": "error", + "computed-property-spacing": [ + "error", + "never" + ], + "consistent-return": "error", + "consistent-this": "error", + "curly": "error", + "default-case": "error", + "dot-location": "error", + "dot-notation": "error", + "eol-last": "error", + "eqeqeq": "error", + "generator-star-spacing": "error", + "guard-for-in": "error", + "handle-callback-err": "error", + "id-blacklist": "error", + "id-match": "error", + "indent": [ "error", 2 ], + "jsx-quotes": "error", + "key-spacing": [ + "error", { + "beforeColon": false, + "afterColon": true + } + ], + "keyword-spacing": "error", + "linebreak-style": [ + "error", + "unix" + ], + "lines-around-comment": "error", + "max-depth": "error", + "max-len": [ "error", 120 ], + "max-nested-callbacks": "error", + "max-params": "error", + "new-cap": "error", + "new-parens": "error", + "newline-per-chained-call": "error", + "no-alert": "error", + "no-array-constructor": "error", + "no-bitwise": "error", + "no-caller": "error", + "no-catch-shadow": "error", + "no-confusing-arrow": "error", + "no-continue": "error", + "no-div-regex": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-empty-function": "error", + "no-eq-null": "error", + "no-eval": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-label": "error", + "no-extra-parens": "error", + "no-floating-decimal": "error", + "no-implicit-coercion": "error", + "no-implicit-globals": "warn", + "no-implied-eval": "error", + "no-inner-declarations": [ + "error", + "functions" + ], + "no-invalid-this": "error", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "error", + "no-loop-func": "error", + "no-mixed-requires": "error", + "no-multi-spaces": "error", + "no-multi-str": "error", + "no-multiple-empty-lines": "error", + "no-negated-condition": "error", + "no-nested-ternary": "error", + "no-new": "error", + "no-new-func": "error", + "no-new-object": "error", + "no-new-require": "error", + "no-new-wrappers": "error", + "no-octal-escape": "error", + "no-param-reassign": "error", + "no-path-concat": "error", + "no-process-env": "error", + "no-process-exit": "error", + "no-proto": "error", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-syntax": "error", + "no-return-assign": "error", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-shadow": "error", + "no-shadow-restricted-names": "error", + "no-spaced-func": "error", + "no-sync": "error", + "no-ternary": "error", + "no-throw-literal": "error", + "no-trailing-spaces": "error", + "no-undef-init": "error", + "no-undefined": "error", + "no-underscore-dangle": "error", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "error", + "no-use-before-define": "error", + "no-useless-call": "error", + "no-useless-concat": "error", + "no-useless-constructor": "error", + "no-useless-escape": "error", + "no-void": "error", + "no-whitespace-before-property": "error", + "no-with": "error", + "object-curly-spacing": "error", + "one-var-declaration-per-line": "error", + "operator-assignment": "error", + "operator-linebreak": "error", + "prefer-const": "error", + "prefer-reflect": "error", + "prefer-spread": "error", + "quotes": [ + "error", + "single" + ], + "radix": "error", + "semi": "error", + "semi-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "sort-imports": "error", + "sort-vars": "error", + "space-before-blocks": "off", + "space-before-function-paren": "off", + "space-in-parens": "off", + "space-infix-ops": "error", + "space-unary-ops": "error", + "spaced-comment": [ + "error", + "always" + ], + "strict": [ + "error", + "never" + ], + "template-curly-spacing": "error", + "valid-jsdoc": "error", + "wrap-iife": "error", + "wrap-regex": "error", + "yield-star-spacing": "error", + "yoda": [ + "error", + "never" + ] + } +}; diff --git a/js-assessment/tests/app/arrays.js b/js-assessment/tests/app/arrays.js new file mode 100644 index 0000000..83efc3f --- /dev/null +++ b/js-assessment/tests/app/arrays.js @@ -0,0 +1,113 @@ +if ( typeof window === 'undefined' ) { + require('../../app/arrays'); + var expect = require('chai').expect; +} + +describe('arrays', function() { + var a; + + beforeEach(function() { + a = [ 1, 2, 3, 4 ]; + }); + + it('you should be able to determine the location of an item in an array', function() { + expect(arraysAnswers.indexOf(a, 3)).to.eql(2); + expect(arraysAnswers.indexOf(a, 5)).to.eql(-1); + }); + + it('you should be able to sum the items of an array', function() { + expect(arraysAnswers.sum(a)).to.eql(10); + }); + + it('you should be able to remove all instances of a value from an array', function() { + a.push(2); // Make sure the value appears more than one time + a.push(2); // Make sure the value appears more than one time in a row + var result = arraysAnswers.remove(a, 2); + + expect(result).to.have.length(3); + expect(result.join(' ')).to.eql('1 3 4'); + }); + + it('you should be able to remove all instances of a value from an array, returning the original array', function() { + a.splice( 1, 0, 2 ); + a.push( 2 ); + a.push( 2 ); + + var result = arraysAnswers.removeWithoutCopy(a, 2); + + expect(result).to.have.length(3); + expect(result.join(' ')).to.eql('1 3 4'); + + // make sure that you return the same array instance + expect(result).equal(a); + }); + + it('you should be able to add an item to the end of an array', function() { + var result = arraysAnswers.append(a, 10); + + expect(result).to.have.length(5); + expect(result[result.length - 1]).to.eql(10); + }); + + it('you should be able to remove the last item of an array', function() { + var result = arraysAnswers.truncate(a); + + expect(result).to.have.length(3); + expect(result.join(' ')).to.eql('1 2 3'); + }); + + it('you should be able to add an item to the beginning of an array', function () { + var result = arraysAnswers.prepend(a, 10); + + expect(result).to.have.length(5); + expect(result[0]).to.eql(10); + }); + + it('you should be able to remove the first item of an array', function () { + var result = arraysAnswers.curtail(a); + + expect(result).to.have.length(3); + expect(result.join(' ')).to.eql('2 3 4'); + }); + + it('you should be able to join together two arrays', function() { + var c = [ 'a', 'b', 'c', 1 ]; + var result = arraysAnswers.concat(a, c); + + expect(result).to.have.length(8); + expect(result.join(' ')).to.eql('1 2 3 4 a b c 1'); + }); + + it('you should be able to add an item anywhere in an array', function() { + var result = arraysAnswers.insert(a, 'z', 2); + + expect(result).to.have.length(5); + expect(result.join(' ')).to.eql('1 2 z 3 4'); + }); + + it('you should be able to count the occurences of an item in an array', function() { + var result = arraysAnswers.count([ 1, 2, 4, 4, 3, 4, 3 ], 4); + + expect(result).to.eql(3); + }); + + it('you should be able to find duplicates in an array', function() { + var result = arraysAnswers.duplicates([ 1, 2, 4, 4, 3, 3, 1, 5, 3 ]); + + expect(result.sort()).to.eql([1, 3, 4]); + }); + + it('you should be able to square each number in an array', function() { + var result = arraysAnswers.square(a); + + expect(result).to.have.length(4); + expect(result.join(' ')).to.eql('1 4 9 16'); + }); + + it('you should be able to find all occurrences of an item in an array', function() { + var result = arraysAnswers.findAllOccurrences([ 1, 2, 3, 4, 5, 6, 1, 7], 1); + + expect(result.sort().join(' ')).to.eql('0 6'); + }); + +}); diff --git a/js-assessment/tests/app/async.js b/js-assessment/tests/app/async.js new file mode 100644 index 0000000..0e04e1c --- /dev/null +++ b/js-assessment/tests/app/async.js @@ -0,0 +1,40 @@ +if ( typeof window === 'undefined' ) { + require('../../app/async'); + var expect = require('chai').expect; +} + +describe('async behavior', function() { + it('you should understand how to use promises to handle asynchronicity', function(done) { + var flag = false; + var finished = 0; + var total = 2; + + function finish(_done) { + if (++finished === total) { _done(); } + } + + asyncAnswers.async(true).then(function(result) { + flag = result; + expect(flag).to.eql(true); + finish(done); + }); + + asyncAnswers.async('success').then(function(result) { + flag = result; + expect(flag).to.eql('success'); + finish(done); + }); + + expect(flag).to.eql(false); + }); + + it('you should be able to retrieve data from the server and return a sorted array of names', function(done) { + var url = '/data/testdata.json'; + + asyncAnswers.manipulateRemoteData(url).then(function(result) { + expect(result).to.have.length(5); + expect(result.join(' ')).to.eql('Adam Alex Matt Paul Rebecca'); + done(); + }); + }); +}); diff --git a/js-assessment/tests/app/bestPractices.js b/js-assessment/tests/app/bestPractices.js new file mode 100644 index 0000000..de2269b --- /dev/null +++ b/js-assessment/tests/app/bestPractices.js @@ -0,0 +1,23 @@ +if ( typeof window === 'undefined' ) { + require('../../app/bestPractices'); + var expect = require('chai').expect; +} + +describe('best practices', function(){ + it('you should avoid global variables', function() { + bestPracticesAnswers.globals(); + expect(window.myObject).not.to.be.ok; + }); + + it('you should use parseInt correctly', function() { + expect(bestPracticesAnswers.parseInt('12')).to.eql(12); + expect(bestPracticesAnswers.parseInt('12px')).to.eql(12); + expect(bestPracticesAnswers.parseInt('0x12')).to.eql(0); + }); + + it('you should understand strict comparison', function() { + expect(bestPracticesAnswers.identity(1, '1')).to.eql(false); + expect(bestPracticesAnswers.identity(1, 1)).to.eql(true); + expect(bestPracticesAnswers.identity(0, false)).to.eql(false); + }); +}); diff --git a/js-assessment/tests/app/count.js b/js-assessment/tests/app/count.js new file mode 100644 index 0000000..c1973ef --- /dev/null +++ b/js-assessment/tests/app/count.js @@ -0,0 +1,66 @@ +/* eslint-disable no-console */ +if ( typeof window === 'undefined' ) { + require('../../app/count'); + var expect = require('chai').expect; + var sinon = require('sinon'); +} + +/** + * This test describes a function, count, that takes two arguments: a starting number, + * and an ending number. The function should console.log each number from the start + * number to the end number, one number per 1/10th of a second. The function should + * return an object with a cancel method, which should cancel the counting. +*/ + +describe('counter', function () { + var nums; + var origConsoleLog; + + beforeEach(function () { + nums = []; + + if (typeof console === 'undefined') { + console = { + log: null + }; + } + origConsoleLog = console.log; + console.log = function (val) { + nums.push(val); + }; + + this.clock = sinon.useFakeTimers(); + }); + + afterEach(function () { + console.log = origConsoleLog; + + this.clock.restore(); + }); + + it('should count from start number to end number, one per 1/10th of a second', function () { + this.timeout(600); + countAnswers.count(1, 5); + + for (var i = 1; i <= 5; i++) { + expect(nums.length).to.eql(i); + + this.clock.tick(100); + } + + expect(nums.length).to.eql(5); + expect(nums[0]).to.eql(1); + expect(nums[4]).to.eql(5); + }); + + it('should provide a method to cancel the counting', function () { + this.timeout(600); + + var counter = countAnswers.count(1, 5); + counter.cancel(); + + this.clock.tick(550); + + expect(nums.length < 5).to.be.ok; + }); +}); diff --git a/js-assessment/tests/app/flowControl.js b/js-assessment/tests/app/flowControl.js new file mode 100644 index 0000000..b914476 --- /dev/null +++ b/js-assessment/tests/app/flowControl.js @@ -0,0 +1,30 @@ +if ( typeof window === 'undefined' ) { + require('../../app/flowControl'); + var expect = require('chai').expect; +} + +describe('flow control', function() { + it('you should be able to conditionally branch your code', function() { + var num = 0; + + while (num % 3 === 0 || num % 5 === 0) { + num = Math.floor(Math.random() * 10) + 1; + } + + expect(flowControlAnswers.fizzBuzz()).not.to.be.ok; + expect(flowControlAnswers.fizzBuzz('foo')).not.to.be.ok; + expect(flowControlAnswers.fizzBuzz(2)).to.eql(2); + expect(flowControlAnswers.fizzBuzz(101)).to.eql(101); + + expect(flowControlAnswers.fizzBuzz(3)).to.eql('fizz'); + expect(flowControlAnswers.fizzBuzz(6)).to.eql('fizz'); + expect(flowControlAnswers.fizzBuzz(num * 3)).to.eql('fizz'); + + expect(flowControlAnswers.fizzBuzz(5)).to.eql('buzz'); + expect(flowControlAnswers.fizzBuzz(10)).to.eql('buzz'); + expect(flowControlAnswers.fizzBuzz(num * 5)).to.eql('buzz'); + + expect(flowControlAnswers.fizzBuzz(15)).to.eql('fizzbuzz'); + expect(flowControlAnswers.fizzBuzz(num * 3 * 5)).to.eql('fizzbuzz'); + }); +}); diff --git a/js-assessment/tests/app/functions.js b/js-assessment/tests/app/functions.js new file mode 100644 index 0000000..ee5b9bd --- /dev/null +++ b/js-assessment/tests/app/functions.js @@ -0,0 +1,145 @@ +if ( typeof window === 'undefined' ) { + require('../../app/functions'); + var expect = require('chai').expect; +} + +describe('functions', function() { + var sayItCalled = false; + var sayIt = function(greeting, name, punctuation) { + sayItCalled = true; + return greeting + ', ' + name + (punctuation || '!'); + }; + + beforeEach(function () { + sayItCalled = false; + }); + + it('you should be able to use an array as arguments when calling a function', function() { + var result = functionsAnswers.argsAsArray(sayIt, [ 'Hello', 'Ellie', '!' ]); + expect(result).to.eql('Hello, Ellie!'); + expect(sayItCalled).to.be.ok; + }); + + it('you should be able to change the context in which a function is called', function() { + var speak = function() { + return sayIt(this.greeting, this.name, '!!!'); + }; + var obj = { + greeting: 'Hello', + name: 'Rebecca' + }; + + var result = functionsAnswers.speak(speak, obj); + expect(result).to.eql('Hello, Rebecca!!!'); + expect(sayItCalled).to.be.ok; + }); + + it('you should be able to return a function from a function', function() { + expect(functionsAnswers.functionFunction('Hello')('world')).to.eql('Hello, world'); + expect(functionsAnswers.functionFunction('Hai')('can i haz funxtion?')).to.eql('Hai, can i haz funxtion?'); + }); + + it('you should be able to use closures', function () { + var arr = [ Math.random(), Math.random(), Math.random(), Math.random() ]; + var square = function (x) { return x * x; }; + + var funcs = functionsAnswers.makeClosures(arr, square); + expect(funcs).to.have.length(arr.length); + + for (var i = 0; i < arr.length; i++) { + expect(funcs[i]()).to.eql(square(arr[i])); + } + }); + + it('you should be able to create a "partial" function', function() { + var partial = functionsAnswers.partial(sayIt, 'Hello', 'Ellie'); + expect(partial('!!!')).to.eql('Hello, Ellie!!!'); + expect(sayItCalled).to.be.ok; + }); + + it('you should be able to use arguments', function () { + var a = Math.random(); + var b = Math.random(); + var c = Math.random(); + var d = Math.random(); + + expect(functionsAnswers.useArguments(a)).to.eql(a); + expect(functionsAnswers.useArguments(a, b)).to.eql(a + b); + expect(functionsAnswers.useArguments(a, b, c)).to.eql(a + b + c); + expect(functionsAnswers.useArguments(a, b, c, d)).to.eql(a + b + c + d); + }); + + it('you should be able to apply functions with arbitrary numbers of arguments', function () { + (function () { + var a = Math.random(); + var b = Math.random(); + var c = Math.random(); + + var wasITake2ArgumentsCalled = false; + var iTake2Arguments = function (firstArgument, secondArgument) { + expect(arguments.length).to.eql(2); + expect(firstArgument).to.eql(a); + expect(secondArgument).to.eql(b); + + wasITake2ArgumentsCalled = true; + }; + + var wasITake3ArgumentsCalled = false; + var iTake3Arguments = function (firstArgument, secondArgument, thirdArgument) { + expect(arguments.length).to.eql(3); + expect(firstArgument).to.eql(a); + expect(secondArgument).to.eql(b); + expect(thirdArgument).to.eql(c); + + wasITake3ArgumentsCalled = true; + }; + + functionsAnswers.callIt(iTake2Arguments, a, b); + functionsAnswers.callIt(iTake3Arguments, a, b, c); + + expect(wasITake2ArgumentsCalled).to.be.ok; + expect(wasITake3ArgumentsCalled).to.be.ok; + }()); + }); + + it('you should be able to create a "partial" function for variable number of applied arguments', function () { + var partialMe = function (x, y, z) { + return x / y * z; + }; + + var a = Math.random(); + var b = Math.random(); + var c = Math.random(); + expect(functionsAnswers.partialUsingArguments(partialMe)(a, b, c)).to.eql(partialMe(a, b, c)); + expect(functionsAnswers.partialUsingArguments(partialMe, a)(b, c)).to.eql(partialMe(a, b, c)); + expect(functionsAnswers.partialUsingArguments(partialMe, a, b)(c)).to.eql(partialMe(a, b, c)); + expect(functionsAnswers.partialUsingArguments(partialMe, a, b, c)()).to.eql(partialMe(a, b, c)); + }); + + it('you should be able to curry existing functions', function () { + var curryMe = function (x, y, z) { + return x / y * z; + }; + + var a = Math.random(); + var b = Math.random(); + var c = Math.random(); + var result; + + result = functionsAnswers.curryIt(curryMe); + expect(typeof result).to.eql('function'); + expect(result.length).to.eql(1); + + result = functionsAnswers.curryIt(curryMe)(a); + expect(typeof result).to.eql('function'); + expect(result.length).to.eql(1); + + result = functionsAnswers.curryIt(curryMe)(a)(b); + expect(typeof result).to.eql('function'); + expect(result.length).to.eql(1); + + result = functionsAnswers.curryIt(curryMe)(a)(b)(c); + expect(typeof result).to.eql('number'); + expect(result).to.eql(curryMe(a, b, c)); + }); +}); diff --git a/js-assessment/tests/app/logicalOperators.js b/js-assessment/tests/app/logicalOperators.js new file mode 100644 index 0000000..6767d7e --- /dev/null +++ b/js-assessment/tests/app/logicalOperators.js @@ -0,0 +1,22 @@ +if ( typeof window === 'undefined' ) { + require('../../app/logicalOperators'); + var expect = require('chai').expect; +} + +describe('logical operators', function(){ + it('you should be able to work with logical or', function() { + expect(logicalOperatorsAnswers.or(false, true)).to.be.ok; + expect(logicalOperatorsAnswers.or(true, false)).to.be.ok; + expect(logicalOperatorsAnswers.or(true, true)).to.be.ok; + expect(logicalOperatorsAnswers.or(false, false)).not.to.be.ok; + expect(logicalOperatorsAnswers.or(3, 4)).to.not.eq(7); + }); + + it('you should be able to work with logical and', function() { + expect(logicalOperatorsAnswers.and(false, true)).not.to.be.ok; + expect(logicalOperatorsAnswers.and(false, false)).not.to.be.ok; + expect(logicalOperatorsAnswers.and(true, false)).not.to.be.ok; + expect(logicalOperatorsAnswers.and(true, true)).to.be.ok; + expect(logicalOperatorsAnswers.and(3, 4)).to.be.ok; + }); +}); diff --git a/js-assessment/tests/app/modules.js b/js-assessment/tests/app/modules.js new file mode 100644 index 0000000..cc0c44a --- /dev/null +++ b/js-assessment/tests/app/modules.js @@ -0,0 +1,21 @@ +if ( typeof window === 'undefined' ) { + require('../../app/modules'); + var expect = require('chai').expect; +} + +describe('the module pattern', function() { + it('you should be able to create a function that returns a module', function() { + var module = modulesAnswers.createModule('hello', 'matt'); + + expect(module.sayIt).to.be.a('function'); + expect(module.name).to.eql('matt'); + expect(module.greeting).to.eql('hello'); + expect(module.sayIt()).to.eql('hello, matt'); + + module.name = 'katniss'; + module.greeting = 'hi'; + expect(module.name).to.eql('katniss'); + expect(module.greeting).to.eql('hi'); + expect(module.sayIt()).to.eql('hi, katniss'); + }); +}); diff --git a/js-assessment/tests/app/numbers.js b/js-assessment/tests/app/numbers.js new file mode 100644 index 0000000..6f1a333 --- /dev/null +++ b/js-assessment/tests/app/numbers.js @@ -0,0 +1,32 @@ +if ( typeof window === 'undefined' ) { + require('../../app/numbers'); + var expect = require('chai').expect; +} + +describe('numbers', function() { + describe('binary operations', function() { + it('you should be able to find the value of a given bit', function() { + expect(numbersAnswers.valueAtBit(128, 8)).to.eql(1); + expect(numbersAnswers.valueAtBit(65, 1)).to.eql(1); + expect(numbersAnswers.valueAtBit(65, 7)).to.eql(1); + expect(numbersAnswers.valueAtBit(128, 1)).to.eql(0); + }); + + it('you should be able to return the base10 representation of a binary string', function() { + expect(numbersAnswers.base10('11000000')).to.eql(192); + }); + + it('you should be able to convert an eight-bit number to a binary string', function() { + expect(numbersAnswers.convertToBinary(128)).to.eql('10000000'); + expect(numbersAnswers.convertToBinary(65)).to.eql('01000001'); + }); + }); + + describe('decimals', function() { + it('you should be able to multiply with precision', function() { + expect(numbersAnswers.multiply(3, 0.1)).to.eql(0.3); + expect(numbersAnswers.multiply(3, 0.2)).to.eql(0.6); + expect(numbersAnswers.multiply(3, 0.0001)).to.eql(0.0003); + }); + }); +}); diff --git a/js-assessment/tests/app/objects.js b/js-assessment/tests/app/objects.js new file mode 100644 index 0000000..7e24306 --- /dev/null +++ b/js-assessment/tests/app/objects.js @@ -0,0 +1,63 @@ +if ( typeof window === 'undefined' ) { + require('../../app/objects'); + var expect = require('chai').expect; +} + +describe('objects and context', function() { + var a; + var b; + var C; + + beforeEach(function() { + a = { + name: 'Matt', + greeting: 'Hello', + sayIt: function() { + return this.greeting + ', ' + + this.name + '!'; + } + }; + + b = { + name: 'Rebecca', + greeting: 'Yo' + }; + + C = function(name) { + this.name = name; + return this; + }; + }); + + it('you should be able to alter the context in which a method runs', function() { + // define a function for fn so that the following will pass + expect(objectsAnswers.alterContext(a.sayIt, b)).to.eql('Yo, Rebecca!'); + }); + + it('you should be able to alter multiple objects at once', function() { + // define a function for fn so that the following will pass + var obj1 = new C('Rebecca'); + var obj2 = new C('Melissa'); + var greeting = 'What\'s up'; + + objectsAnswers.alterObjects(C, greeting); + + expect(obj1.greeting).to.eql(greeting); + expect(obj2.greeting).to.eql(greeting); + expect(new C('Ellie').greeting).to.eql(greeting); + }); + + it('you should be able to iterate over an object\'s "own" properties', function() { + // define a function for fn so that the following will pass + C = function() { + this.foo = 'bar'; + this.baz = 'bim'; + }; + + C.prototype.bop = 'bip'; + + var obj = new C(); + + expect(objectsAnswers.iterate(obj)).to.eql([ 'foo: bar', 'baz: bim' ]); + }); +}); diff --git a/js-assessment/tests/app/recursion.js b/js-assessment/tests/app/recursion.js new file mode 100644 index 0000000..424a986 --- /dev/null +++ b/js-assessment/tests/app/recursion.js @@ -0,0 +1,107 @@ +if ( typeof window === 'undefined' ) { + require('../../app/recursion'); + var expect = require('chai').expect; + var _ = require('underscore'); +} + +describe('recursion', function() { + var fileData = { + dir: 'app', + files: [ + 'index.html', + { + dir: 'js', + files: [ + 'main.js', + 'app.js', + 'misc.js', + { + dir: 'vendor', + files: [ + 'jquery.js', + 'underscore.js' + ] + } + ] + }, + { + dir: 'css', + files: [ + 'reset.css', + 'main.css' + ] + } + ] + }; + + it('you should be able to return a list of files from the data', function() { + var result = recursionAnswers.listFiles(fileData); + expect(result.length).to.eql(8); + expect(result.indexOf('index.html') > -1).to.be.ok; + expect(result.indexOf('main.js') > -1).to.be.ok; + expect(result.indexOf('underscore.js') > -1).to.be.ok; + }); + + it('you should be able to return a list of files in a subdir', function() { + var result = recursionAnswers.listFiles(fileData, 'js'); + expect(result.length).to.eql(5); + expect(result.indexOf('main.js') > -1).to.be.ok; + expect(result.indexOf('underscore.js') > -1).to.be.ok; + }); +}); + +describe('permutation', function() { + var arr = [ 1, 2, 3, 4 ]; + var answer = [ + [1, 2, 3, 4], + [1, 2, 4, 3], + [1, 3, 2, 4], + [1, 3, 4, 2], + [1, 4, 2, 3], + [1, 4, 3, 2], + [2, 1, 3, 4], + [2, 1, 4, 3], + [2, 3, 1, 4], + [2, 3, 4, 1], + [2, 4, 1, 3], + [2, 4, 3, 1], + [3, 1, 2, 4], + [3, 1, 4, 2], + [3, 2, 1, 4], + [3, 2, 4, 1], + [3, 4, 1, 2], + [3, 4, 2, 1], + [4, 1, 2, 3], + [4, 1, 3, 2], + [4, 2, 1, 3], + [4, 2, 3, 1], + [4, 3, 1, 2], + [4, 3, 2, 1] + ]; + + it('you should be able to return the permutations of an array', function() { + var result = recursionAnswers.permute(arr); + var resultStrings = _.map(result, function(r) { return r.join(''); }); + + expect(result.length).to.eql(answer.length); + + _.each(answer, function(a) { + expect(resultStrings.indexOf(a.join('')) > -1).to.be.ok; + }); + }); + + it('you should be able to return the nth number in a fibonacci sequence', function() { + expect(recursionAnswers.fibonacci(2)).to.eql(1); + expect(recursionAnswers.fibonacci(6)).to.eql(8); + }); + + it('you should be able to return the set of all valid combinations of n pairs of parentheses.', function() { + var expected = [ '((()))', '(()())', '(())()', '()(())', '()()()']; + var result = recursionAnswers.validParentheses(3); + + expect(result.length).to.eql(5); + _.each(expected, function(c) { + expect(result).to.contain(c); + }); + }); +}); diff --git a/js-assessment/tests/app/regex.js b/js-assessment/tests/app/regex.js new file mode 100644 index 0000000..cad9a11 --- /dev/null +++ b/js-assessment/tests/app/regex.js @@ -0,0 +1,63 @@ +if ( typeof window === 'undefined' ) { + require('../../app/regex'); + var expect = require('chai').expect; +} + +describe('regular expressions', function() { + it('you should be able to detect a number in a string', function() { + expect(regexAnswers.containsNumber('abc123')).to.eql(true); + expect(regexAnswers.containsNumber('abc')).to.eql(false); + }); + + it('you should be able to detect a repeating letter in a string', function() { + expect(regexAnswers.containsRepeatingLetter('bookkeeping')).to.eql(true); + expect(regexAnswers.containsRepeatingLetter('rattler')).to.eql(true); + expect(regexAnswers.containsRepeatingLetter('ZEPPELIN')).to.eql(true); + expect(regexAnswers.containsRepeatingLetter('cats')).to.eql(false); + expect(regexAnswers.containsRepeatingLetter('l33t')).to.eql(false); + }); + + it('you should be able to determine whether a string ends with a vowel (aeiou)', function() { + expect(regexAnswers.endsWithVowel('cats')).to.eql(false); + expect(regexAnswers.endsWithVowel('gorilla')).to.eql(true); + expect(regexAnswers.endsWithVowel('I KNOW KUNG FU')).to.eql(true); + }); + + it('you should be able to capture the first series of three numbers', function() { + expect(regexAnswers.captureThreeNumbers('abc123')).to.eql('123'); + expect(regexAnswers.captureThreeNumbers('9876543')).to.eql('987'); + expect(regexAnswers.captureThreeNumbers('abcdef')).to.eql(false); + expect(regexAnswers.captureThreeNumbers('12ab12ab')).to.eql(false); + }); + + it('you should be able to determine whether a string matches a pattern', function() { + // the pattern is XXX-XXX-XXXX where all X's are digits + expect(regexAnswers.matchesPattern('800-555-1212')).to.eql(true); + expect(regexAnswers.matchesPattern('451-933-7899')).to.eql(true); + expect(regexAnswers.matchesPattern('33-444-5555')).to.eql(false); + expect(regexAnswers.matchesPattern('abc-def-hijk')).to.eql(false); + expect(regexAnswers.matchesPattern('1800-555-1212')).to.eql(false); + expect(regexAnswers.matchesPattern('800-555-12121')).to.eql(false); + expect(regexAnswers.matchesPattern('800-5555-1212')).to.eql(false); + expect(regexAnswers.matchesPattern('800-55-1212')).to.eql(false); + }); + + it('you should be able to detect correctly-formatted monetary amounts in USD', function() { + expect(regexAnswers.isUSD('$132.03')).to.eql(true); + expect(regexAnswers.isUSD('$32.03')).to.eql(true); + expect(regexAnswers.isUSD('$2.03')).to.eql(true); + expect(regexAnswers.isUSD('$1,023,032.03')).to.eql(true); + expect(regexAnswers.isUSD('$20,933,209.93')).to.eql(true); + expect(regexAnswers.isUSD('$20,933,209')).to.eql(true); + expect(regexAnswers.isUSD('$459,049,393.21')).to.eql(true); + expect(regexAnswers.isUSD('34,344.34')).to.eql(false); + expect(regexAnswers.isUSD('$,344.34')).to.eql(false); + expect(regexAnswers.isUSD('$34,344.3')).to.eql(false); + expect(regexAnswers.isUSD('$34,344.344')).to.eql(false); + expect(regexAnswers.isUSD('$34,344_34')).to.eql(false); + expect(regexAnswers.isUSD('$3,432,12.12')).to.eql(false); + expect(regexAnswers.isUSD('$3,432,1,034.12')).to.eql(false); + expect(regexAnswers.isUSD('4$3,432,034.12')).to.eql(false); + expect(regexAnswers.isUSD('$2.03.45')).to.eql(false); + }); +}); diff --git a/js-assessment/tests/app/strings.js b/js-assessment/tests/app/strings.js new file mode 100644 index 0000000..52370fe --- /dev/null +++ b/js-assessment/tests/app/strings.js @@ -0,0 +1,51 @@ +if ( typeof window === 'undefined' ) { + require('../../app/strings'); + var expect = require('chai').expect; +} + +describe('strings', function() { + it('you should be able to reduce duplicate characters to a desired minimum', function() { + expect(stringsAnswers.reduceString('aaaabbbb', 2)).to.eql('aabb'); + expect(stringsAnswers.reduceString('xaaabbbb', 2)).to.eql('xaabb'); + expect(stringsAnswers.reduceString('aaaabbbb', 1)).to.eql('ab'); + expect(stringsAnswers.reduceString('aaxxxaabbbb', 2)).to.eql('aaxxaabb'); + }); + + it('you should be able to wrap lines at a given number of columns, without breaking words', function() { + var wrapCol = 5; + var inputStrings = [ + 'abcdef abcde abc def', + 'abc abc abc', + 'a b c def' + ]; + var outputStrings = [ + 'abcdef\nabcde\nabc\ndef', + 'abc\nabc\nabc', + 'a b c\ndef' + ]; + var formattedStr; + + inputStrings.forEach(function(str, index) { + formattedStr = stringsAnswers.wordWrap(str, wrapCol); + expect(formattedStr).to.eql(outputStrings[index]); + }); + }); + + it('you should be able to reverse a string', function() { + var inputStrings = [ + 'abc', + 'i am a string of characters', + 'A man, a plan, a canal: Panama' + ]; + var outputStrings = [ + 'cba', + 'sretcarahc fo gnirts a ma i', + 'amanaP :lanac a ,nalp a ,nam A' + ]; + + inputStrings.forEach(function(str, index) { + var result = stringsAnswers.reverseString(str); + expect(result).to.eql(outputStrings[index]); + }); + }); +}); diff --git a/js-assessment/tests/runner.html b/js-assessment/tests/runner.html new file mode 100644 index 0000000..5b8706d --- /dev/null +++ b/js-assessment/tests/runner.html @@ -0,0 +1,61 @@ + + + + + Mocha Tests + + + + + +
      +
      + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/solo_study.md b/solo_study.md new file mode 100644 index 0000000..357078d --- /dev/null +++ b/solo_study.md @@ -0,0 +1,188 @@ +# Core Vanilla JavaScript - Solo Study + +A day-by-day structured deep dive into the basics JavaScript. Read articles, watch videos, complete lessons and exercises, and implement a final project. + +## Day 1: Variables, Flow control, and Functions + +Your objectives for the day is to achieve: + ++ Basic understanding of variables ++ Basic string and array manipulation ++ Understand 'truthy' and 'falsey' ++ Understand literal notation ++ Understand dot bracket notation ++ Basic understanding of JSON ++ Understand Operator precedence ++ Practice Flow control: iteration, if, switch, while, for loops ++ Understand and practice Functions: arguments, arity, return, scope + +### Plan + ++ [ ] Signup for [Udacity Javascript Basics][udacity] ++ [ ] Complete [Getting up and running & Problem Set 0][udacity] ++ [ ] Complete [Data Types and Problem Set 1][udacity] ++ [ ] Complete [Flow Control and Problem Set 2][udacity] +- [ ] Complete [Quiz: Relationships][udacity] +- [ ] Complete [Quiz: Astronaut Arrays][udacity] +- [ ] Complete [Quiz: Pagespeed Insights][udacity] + + +### Stretch + +- [ ] Skim [You don't know JS](https://github.com/getify/You-Dont-Know-JS/blob/master/up%20&%20going/ch1.md#practice) and complete the final challenge +- [ ] Complete [Final Project][udacity] +- [ ] Watch and complete the [Front End Masters Javascript Introduction](https://frontendmasters.com/courses/javascript-basics/) (2 hours) +- [ ] Watch the bootcamp videos in the [Core Vanilla Javascript](https://shereef.wistia.com/projects/fwy60ilf20) section +- [ ] Read Chapters 1,2,3 of [Eloquent Javascript][el] +- [ ] Complete [CodeCademy's Learn Javasript](https://www.codecademy.com/learn/javascript) + + +## Day 2: Arrays, Objects, and lots of Practice + + +Today you objectives are to put in practice the concepts you learned yesterday. It's a good opportunity to practice researching, asking for help, basic debugging, and problem solving. You will + +- Complete other beginner javascript tutorials +- Dive deeper into Arrays and Objects +- Practice lots of small javascript coding drills + +*Note: Refer to this handy [Javascript Cheat sheet](http://overapi.com/javascript) if you need it* + +### Plan + +- [ ] Complete the last 5 tasks of [Objects Basics Tutorial](https://learn.javascript.info/object) + - [ ] Link to your solution here (jsbin, gist, or a js file in this repo) +- [ ] Complete the last 5 tasks of [Arrays Tutorial](https://learn.javascript.info/array) + - [ ] Link to your solution here +- [ ] Complete the last 5 tasks of [Array Methods](https://learn.javascript.info/array) + - [ ] Link to your solution here +- [ ] Complete the [Learn JS Basics tutorial](http://www.learn-js.org/en/Welcome) +- [ ] Complete the [Learn JS Objects tutorial](http://www.learn-js.org/en/Object_Oriented_JavaScript) + +*These next 3 steps are part of [Front End Masters JS Course](https://frontendmasters.com/courses/js-fundamentals-to-functional/). You might need to watch the videos and part of the course to make it through. Reach out to staff for an invite code if you don't already have an account.* + +- [ ] Complete the [Array Exercises](https://github.com/bgando/array-exercises) + - [ ] Link to your solution here +- [ ] Complete the [Function Exercises](https://github.com/bgando/function-exercises) + - [ ] Link to your solution here +- [ ] Complete the [Object Exercises](https://github.com/bgando/object-exercises) + - [ ] Link to your solution here + +- Browse in the terminal to the `js-assessment` folder in this repo. +- Run `npm install` and `npm start`, then goto `http://127.0.0.1:4444/` in your browser. +- You should see a bunch of failing tests +- [ ] Update `js-asessment/app/flowControl.js` to make the section's tests pass +- [ ] Update `js-asessment/app/logicalOperators.js` to make the section's tests pass +- [ ] Update `js-asessment/app/logicalNumbers.js` to make the section's tests pass + + +### Stretch + +- [ ] Complete [Free Code Camp Basic Javascript](https://www.freecodecamp.com/map-aside#nested-collapseBasicJavaScript) +- [ ] Read [Javascript Objects in Detail](http://javascriptissexy.com/javascript-objects-in-detail/) +- [ ] Watch the first 11 [Fundamentals Videos](https://channel9.msdn.com/Series/JavaScript-Fundamentals-Development-for-Absolute-Beginners) +- [ ] Read pages 173 to 184 in [Professional JS for Web Developers][projs] +- [ ] Signup for a free Team Treehouse trial and complete their [Object Oriented Javascript Course] (https://teamtreehouse.com/library/objectoriented-javascript) +- [ ] Skim this [cheat sheet][cheat] + +## Day 3: Koans and Drills + +Let's test our understanding by working on some Koans. + +*Note: It's pretty easy to make these test pass. What isn't easy is to make sure you undertand WHY they are passing, and don't move on to the next one until you do* + +*Remember: Update this file, and commit your repo, and push to github after every step* + +- In your forked repo, find the `javascript-koans` sub directory. +- Find `KoansRunner.html` and open it in a browser +- [ ] Edit AboutObjects.js to make all tests pass +- [ ] Edit AboutFunctions.js to make all tests pass +- [ ] Edit AboutArrays.js to make all tests pass +- [ ] Edit AboutApplyingWhatWeHaveLearnt.js to make all tests pass +- [ ] Write all 10 functions described in [/challenges/10exercises.js](/challenges/10exercises.js) + +Ok, on for some more drills. + +- [ ] Sign up for an [edabit](http://edabit.com) account. +- [ ] Solve all 10 array challenges linked in [/challenges/edabit-arrays.js](/challenges/edabit-arrays.js) and commit your solutions to the file + +- Browse in the terminal to the `js-assessment` folder in this repo. +- Run `npm start`, then goto `http://127.0.0.1:4444/` in your browser. +- You should see a bunch of failing tests +- [ ] Update `js-asessment/app/arrays.js` to make the section's tests pass +- [ ] Update `js-asessment/app/strings.js` to make the section's tests pass +- [ ] Update `js-asessment/app/objects.js` to make the section's tests pass +- [ ] Update `js-asessment/app/count.js` to make the section's tests pass +- [ ] Update `js-asessment/app/bestPractices.js` to make the section's tests pass +- [ ] Update `js-asessment/app/functions.js` to make the section's tests pass + +### Stretch + +- [ ] Read chapter 3 in [Professional JS for Web Developers][projs] +- [ ] Install and go through the [javascripting](https://github.com/workshopper/javascripting) workshop + +## Day 4: Some Recursion and Bigger Challenges + + +### Plan +Let's do more Koans to get our day started + +- In your forked repo, find the `Koans` sub directory. +- Find `jskoansbasics.html` and open it in a browser + +- [ ] Find & edit `about_asserts.js` to make the tests pass +- [ ] Find & edit `about_operators.js` to make the tests pass +- [ ] Find & edit `about_equality.js` to make the tests pass +- [ ] Find & edit `about_truthyness.js` to make the tests pass +- [ ] Find & edit `about_assignment.js` to make the tests pass +- [ ] Find & edit `about_control_structures.js` to make the tests pass +- [ ] Find & edit `about_strings.js` to make the tests pass +- [ ] Find & edit `about_numbers.js` to make the tests pass +- [ ] Find & edit `about_objects.js` to make the tests pass +- [ ] Find & edit `about_arrays.js` to make the tests pass + +- Browse in the terminal to the `js-assessment` folder in this repo. +- Run `npm start`, then goto `http://127.0.0.1:4444/` in your browser. +- You should see a bunch of failing tests +- [ ] Update `js-asessment/app/recursion.js` to make the section's tests pass + +### Exercism + +Exercism provides a number of practice problems along with unit tests to ensure the accuracy of your answer to the test problems. We will be using exercism challenges for the rest of this goal. + +- Read the [Exercism setup](/exercism/setup.md) instructions + +- [ ] Solve `/exercism/saddle-points` +- [ ] Solve `/exercism/meetup` +- [ ] Solve `/exercism/matrix` + +## Day 5: More challenges + +Today is going to be a tough one. These challenges are tricky, and will need you to be focussed 100% to make it through: + +- [ ] Solve `/exercism/run-length-encoding` +- [ ] Solve `/exercism/crypto-square` +- [ ] Solve `/exercism/pig-latin` +- [ ] Solve `/exercism/series` +- [ ] Solve `/exercism/flatten-array` +- [ ] Solve `/exercism/difference-of-squares` + + +#### Stretch + +- [ ] Solve `/exercism/sieve` +- [ ] Solve `/exercism/simple-cipher` +- [ ] Solve `/exercism/pascals-triangle` +- [ ] Solve `/exercism/sum-of-multiples` +- [ ] Research about regular expressions and update `js-asessment/app/regex.js` to make the section's tests pass +- [ ] Update `js-asessment/app/modules.js` to make the section's tests pass +- [ ] Update `js-asessment/app/async.js` to make the section's tests pass +- [ ] Complete section 1 of [these exercises](http://ynonperek.com/javascript-exer.html) + + +[el]:http://eloquentjavascript.net/ +[udacity]:https://www.udacity.com/course/javascript-basics--ud804 +[projs]:ftp://ftp.micronet-rostov.ru/linux-support/books/programming/JavaScript/Wrox.Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012.pdf +[ninja]:https://github.com/GuildCrafts/core-object-oriented-javascript/raw/master/Books/Secrets%20of%20the%20JavaScript%20Ninja%20-%20John%20Resig%20and%20Bear%20Bibeault%20-%20December%202012.pdf +[cheat]:https://github.com/GuildCrafts/core-object-oriented-javascript/raw/master/Books/Objects-Cheat-Sheet.pdf +[oojs]:ftp://ftp.micronet-rostov.ru/linux-support/books/programming/JavaScript/[Packt]%20-%20Object-Oriented%20JavaScript%20-%20[Stefanov].pdf diff --git a/team_practice.md b/team_practice.md new file mode 100644 index 0000000..d97200c --- /dev/null +++ b/team_practice.md @@ -0,0 +1,84 @@ +# Core Vanilla JavaScript - Team Practice - 1 + +A marathon of pair exercises in pure javascript. Capped with a final project. +It's a good idea to cut your teeth on the [solo_study](solo_study.md) first before attempting these challenges. + +### Exercism + +Exercism provides a number of practice problems along with unit tests to ensure the accuracy of your answer to the test problems. We will be using exercism challenges for the first three days of this goal. + +- Read the [Exercism setup](/exercism/setup.md) instructions + +### Day 1 - Exercism + +- [ ] Solve `/exercism/hamming` +- [ ] Solve `/exercism/diamond` +- [ ] Solve `/exercism/bracket-push` +- [ ] Solve `/exercism/ocr-numbers` +- [ ] Solve `/exercism/bowling` + +#### Stretch + +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` + +- [ ] Solve `/exercism/phone-number` +- [ ] Solve `/exercism/word-count` +- [ ] Solve `/exercism/gigasecond` +- [ ] Solve `/exercism/leap` + +### Day 2 - Exercism + +- [ ] Solve `/exercism/secret-handshake` +- [ ] Solve `/exercism/wordy` +- [ ] Solve `/exercism/largest-series-product` +- [ ] Solve `/exercism/robot-simulator` +- [ ] Solve `/exercism/rna-transcription` +- [ ] Solve `/exercism/bob` + + +#### Stretch + +- [ ] Solve `/exercism/isogram` +- [ ] Solve `/exercism/trinary` +- [ ] Solve `/exercism/octal` +- [ ] Solve `/exercism/hexadecimal` +- [ ] Solve `/exercism/two-bucket` +- [ ] Solve `/exercism/all-your-base` + +### Day 3 - Exercism + +- [ ] Solve `/exercism/kindergarten-garden` +- [ ] Solve `/exercism/nth-prime` +- [ ] Solve `/exercism/palindrome-products` +- [ ] Solve `/exercism/say` +- [ ] Solve `/exercism/queen-attack` + +#### Stretch + +- [ ] Solve `/exercism/luhn` +- [ ] Solve `/exercism/pythagorean-triplet` +- [ ] Solve `/exercism/beer-song` +- [ ] Solve `/exercism/grade-school` + + + +# Day 4 - Sudoku + +Time for a longer challenge. We'll spend the next two days building a sudoku solver + +- [ ] Ship Step 1 of [Sudoku Challenge](/Sudoku/README.md) +- [ ] Ship Step 2 of [Sudoku Challenge](/Sudoku/README.md) + +# Day 5 - More Sudoku + +- [ ] Ship Step 3 of [Sudoku Challenge](/Sudoku/README.md) + +#### Stretch + +- [ ] Ship Step 4 of [Sudoku Challenge](/Sudoku/README.md) diff --git a/team_practice_2.md b/team_practice_2.md new file mode 100644 index 0000000..3bd9d0c --- /dev/null +++ b/team_practice_2.md @@ -0,0 +1,57 @@ +# Core Vanilla JavaScript - Team Practice - 2 + +A marathon of pair exercises in pure javascript that's slightly more challenging than [Team Practice 1](team_practice.md) +Capped with a final project. +It's a good idea to cut your teeth on the [solo_study](solo_study.md) and [Team Practice 1](team_practice.md) first before attempting these challenges. + +### Exercism + +Exercism provides a number of practice problems along with unit tests to ensure the accuracy of your answer to the test problems. We will be using exercism challenges for the first three days of this goal. + +- Read the [Exercism setup](/exercism/setup.md) instructions + +### Day 1 - Exercism + +- [ ] Solve `/exercism/binary-search` +- [ ] Solve `/exercism/binary-search-tree` +- [ ] Solve `/exercism/pangram` +- [ ] Solve `/exercism/anagram` +- [ ] Solve `/exercism/food-chain` +- [ ] Solve `/exercism/robot-name` + +#### Stretch + +- [ ] Solve `/exercism/` +- [ ] Solve `/exercism/` + +### Day 2 - Exercism + +- [ ] Solve `/exercism/etl` +- [ ] Solve `/exercism/linked-list` +- [ ] Solve `/exercism/triangle` +- [ ] Solve `/exercism/clock` +- [ ] Solve `/exercism/scrabble-score` +- [ ] Solve `/exercism/roman-numerals` + + +#### Stretch + + + +### Day 3 - Exercism + +- [ ] Solve `/exercism/strain` +- [ ] Solve `/exercism/circular-buffer` +- [ ] Solve `/exercism/binary` +- [ ] Solve `/exercism/prime-factors` +- [ ] Solve `/exercism/raindrops` +- [ ] Solve `/exercism/allergies` +- [ ] Solve `/exercism/atbash-cipher` +- [ ] Solve `/exercism/accumulate` + +#### Stretch + + +# Day 4 - + +# Day 5 -