From 7cba91ad64b650176757c5263dbc953f0ff9f1a6 Mon Sep 17 00:00:00 2001 From: mdunisch Date: Thu, 21 Aug 2014 22:47:24 +0200 Subject: [PATCH] Adding .jshintrc + refactory files --- .jshintrc | 118 ++++++++++++++++++ exercises/1_getting_started/exercise.js | 2 +- .../1_getting_started/solution/solution.js | 4 +- exercises/2_sort_me/exercise.js | 14 +-- exercises/2_sort_me/solution/solution.js | 6 +- exercises/3_in_every_case/exercise.js | 19 +-- .../3_in_every_case/solution/solution.js | 11 +- exercises/4_everyone_is_min/exercise.js | 26 ++-- .../4_everyone_is_min/solution/solution.js | 13 +- exercises/5_count_the_comments/exercise.js | 11 +- .../5_count_the_comments/solution/solution.js | 8 +- exercises/6_give_me_an_overview/exercise.js | 8 +- .../solution/solution.js | 16 +-- exercises/7_analyze/exercise.js | 17 +-- exercises/7_analyze/solution/solution.js | 12 +- exercises/8_start_templating/exercise.js | 7 +- .../8_start_templating/solution/solution.js | 6 +- exercises/9_todo_template/exercise.js | 8 +- .../9_todo_template/solution/solution.js | 28 ++--- lib/verify.js | 94 +++++++------- 20 files changed, 284 insertions(+), 144 deletions(-) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..889ff8f --- /dev/null +++ b/.jshintrc @@ -0,0 +1,118 @@ +{ + // -------------------------------------------------------------------- + // JSHint Configuration, Strict Edition + // -------------------------------------------------------------------- + // + // This is a options template for [JSHint][1], using [JSHint example][2] + // and [Ory Band's example][3] as basis and setting config values to + // be most strict: + // + // * set all enforcing options to true + // * set all relaxing options to false + // * set all environment options to false, except the browser value + // * set all JSLint legacy options to false + // + // [1]: http://www.jshint.com/ + // [2]: https://github.com/jshint/node-jshint/blob/master/example/config.json + // [3]: https://github.com/oryband/dotfiles/blob/master/jshintrc + // + // @author http://michael.haschke.biz/ + // @license http://unlicense.org/ + + // == Enforcing Options =============================================== + // + // These options tell JSHint to be more strict towards your code. Use + // them if you want to allow only a safe subset of JavaScript, very + // useful when your codebase is shared with a big number of developers + // with different skill levels. + + "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). + "curly" : true, // Require {} for every new block or scope. + "eqeqeq" : true, // Require triple equals i.e. `===`. + "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`. + "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef" : true, // Prohibit variable use before definition. + "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. + "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "noempty" : true, // Prohibit use of empty blocks. + "nonew" : true, // Prohibit use of constructors for side-effects. + "plusplus" : true, // Prohibit use of `++` & `--`. + "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions. + "undef" : true, // Require all non-global variables be declared before they are used. + "strict" : true, // Require `use strict` pragma in every file. + "trailing" : true, // Prohibit trailing whitespaces. + + // == Relaxing Options ================================================ + // + // These options allow you to suppress certain types of warnings. Use + // them only if you are absolutely positive that you know what you are + // doing. + + "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). + "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. + "debug" : false, // Allow debugger statements e.g. browser breakpoints. + "eqnull" : false, // Tolerate use of `== null`. + "esnext" : false, // Allow ES.next specific features such as `const` and `let`. + "evil" : false, // Tolerate use of `eval`. + "expr" : false, // Tolerate `ExpressionStatement` as Programs. + "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside. + "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). + "iterator" : false, // Allow usage of __iterator__ property. + "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block. + "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. + "laxcomma" : false, // Suppress warnings about comma-first coding style. + "loopfunc" : false, // Allow functions to be defined within loops. + "multistr" : false, // Tolerate multi-line strings. + "onecase" : false, // Tolerate switches with just one case. + "proto" : false, // Tolerate __proto__ property. This property is deprecated. + "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. + "scripturl" : false, // Tolerate script-targeted URLs. + "smarttabs" : false, // Tolerate mixed tabs and spaces when the latter are used for alignmnent only. + "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. + "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. + "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. + "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function. + + // == Environments ==================================================== + // + // These options pre-define global variables that are exposed by + // popular JavaScript libraries and runtime environments—such as + // browser or node.js. + + "browser" : true, // Standard browser globals e.g. `window`, `document`. + "couch" : false, // Enable globals exposed by CouchDB. + "devel" : false, // Allow development statements e.g. `console.log();`. + "dojo" : false, // Enable globals exposed by Dojo Toolkit. + "jquery" : false, // Enable globals exposed by jQuery JavaScript library. + "mootools" : false, // Enable globals exposed by MooTools JavaScript framework. + "node" : true, // Enable globals available when code is running inside of the NodeJS runtime environment. + "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape. + "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework. + "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment. + "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host. + + // == JSLint Legacy =================================================== + // + // These options are legacy from JSLint. Aside from bug fixes they will + // not be improved in any way and might be removed at any point. + + "nomen" : false, // Prohibit use of initial or trailing underbars in names. + "onevar" : false, // Allow only one `var` statement per function. + "passfail" : false, // Stop on first error. + "white" : true, // Check against strict whitespace and indentation rules. + + // == Undocumented Options ============================================ + // + // While I've found these options in [example1][2] and [example2][3] + // they are not described in the [JSHint Options documentation][4]. + // + // [4]: http://www.jshint.com/options/ + + "maxerr" : 100, // Maximum errors before stopping. + "predef" : [ // Extra globals. + //"exampleVar", + //"anotherCoolGlobal", + //"iLoveDouglas" + ], + "indent" : 4 // Specify indentation spacing +} \ No newline at end of file diff --git a/exercises/1_getting_started/exercise.js b/exercises/1_getting_started/exercise.js index c93f62d..023701a 100644 --- a/exercises/1_getting_started/exercise.js +++ b/exercises/1_getting_started/exercise.js @@ -42,4 +42,4 @@ var testing = { } }; -module.exports = verify(testing,run); \ No newline at end of file +module.exports = verify(testing, run); \ No newline at end of file diff --git a/exercises/1_getting_started/solution/solution.js b/exercises/1_getting_started/solution/solution.js index bdc5743..25478b7 100644 --- a/exercises/1_getting_started/solution/solution.js +++ b/exercises/1_getting_started/solution/solution.js @@ -1,6 +1,8 @@ +'use strict'; + var _ = require("lodash"); -var filterwhere = function(item){ +var filterwhere = function (item) { return _.where(item, {active: true}); }; diff --git a/exercises/2_sort_me/exercise.js b/exercises/2_sort_me/exercise.js index b8e9d12..baec918 100644 --- a/exercises/2_sort_me/exercise.js +++ b/exercises/2_sort_me/exercise.js @@ -1,10 +1,10 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { - json:[ + json: [ { article: 2323, quantity: 2 @@ -19,9 +19,9 @@ var run = { } ], expect: [ - { article: 41, quantity: 24 }, - { article: 655, quantity: 23 }, - { article: 2323, quantity: 2 } + { article: 41, quantity: 24 }, + { article: 655, quantity: 23 }, + { article: 2323, quantity: 2 } ] }; @@ -43,4 +43,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/2_sort_me/solution/solution.js b/exercises/2_sort_me/solution/solution.js index f7009bc..b389b64 100644 --- a/exercises/2_sort_me/solution/solution.js +++ b/exercises/2_sort_me/solution/solution.js @@ -1,7 +1,9 @@ +'use strict'; + var _ = require("lodash"); -var sorting = function(item){ - return _.sortBy(item,function(item){ +var sorting = function (item) { + return _.sortBy(item, function (item) { return -item.quantity; }); diff --git a/exercises/3_in_every_case/exercise.js b/exercises/3_in_every_case/exercise.js index 2833c37..c83032a 100644 --- a/exercises/3_in_every_case/exercise.js +++ b/exercises/3_in_every_case/exercise.js @@ -1,7 +1,7 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { json: { @@ -22,6 +22,7 @@ var testing = { input: run.json, shouldbe: run.expect }, + /* 'Checking with an Array...': { input: [ {Hamburg: { population: 1.698 }}, @@ -34,31 +35,31 @@ var testing = { "population": 1.698 }, "size": "small" - },{ + }, { "Strasbourg": { "population": 0.272 }, "size": "small" - },{ + }, { "Rom": { "population": 2.753 }, "size": "small" - },{ + }, { "Dublin": { "population": 0.528 }, "size": "small" }] - }, + }, */ 'Checking with empty Object...': { - input:{}, + input: {}, shouldbe: {} }, 'Checking with empty Array...': { - input:[], + input: [], shouldbe: [] } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/3_in_every_case/solution/solution.js b/exercises/3_in_every_case/solution/solution.js index 5075a86..81404f3 100644 --- a/exercises/3_in_every_case/solution/solution.js +++ b/exercises/3_in_every_case/solution/solution.js @@ -1,13 +1,14 @@ -var _ = require("lodash"); +'use strict'; +var _ = require("lodash"); -var addsizetyp = function(item){ +var addsizetyp = function (item) { - _.forEach(item,function(value,key){ + _.forEach(item, function (value, key) { - if(value.population > 1){ + if (value.population > 1) { item[key].size = "big"; - } else if (value.population > 0.5){ + } else if (value.population > 0.5) { item[key].size = "med"; } else { item[key].size = "small"; diff --git a/exercises/4_everyone_is_min/exercise.js b/exercises/4_everyone_is_min/exercise.js index 6c1fc8c..21468e0 100644 --- a/exercises/4_everyone_is_min/exercise.js +++ b/exercises/4_everyone_is_min/exercise.js @@ -1,15 +1,15 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { json: { - Hamburg: [14,15,16,14,18,17,20,22,21,18,19,23], - Munic: [16,17,19,20,21,23,22,21,20,19,24,23], - Madrid: [24,23,20,24,24,23,21,22,24,20,24,22], - Stockholm: [16,14,12,15,13,14,14,12,11,14,15,14], - Warsaw: [17,15,16,18,20,20,21,18,19,18,17,20] + Hamburg: [14, 15, 16, 14, 18, 17, 20, 22, 21, 18, 19, 23], + Munic: [16, 17, 19, 20, 21, 23, 22, 21, 20, 19, 24, 23], + Madrid: [24, 23, 20, 24, 24, 23, 21, 22, 24, 20, 24, 22], + Stockholm: [16, 14, 12, 15, 13, 14, 14, 12, 11, 14, 15 ,14], + Warsaw: [17, 15, 16, 18, 20, 20, 21, 18, 19, 18, 17, 20] }, expect: { "hot": [ "Madrid"], @@ -25,7 +25,7 @@ var testing = { }, 'Checking with only cold...': { input: [ - {Foo: [0,0,0,0,0,2,4,2,1,4,5,2]} + {Foo: [0, 0, 0, 0, 0, 2, 4, 2, 1, 4, 5, 2]} ], shouldbe: { "hot": [], @@ -33,8 +33,10 @@ var testing = { } }, 'Checking with one temp each...': { - input:{ - "foo": [2],"bar": [16],"tool": [24] + input: { + "foo": [2], + "bar": [16], + "tool": [24] }, shouldbe: { "hot": ["tool"], @@ -42,7 +44,7 @@ var testing = { } }, 'Checking with empty Object...': { - input:{}, + input: {}, shouldbe: { "hot": [], "warm": [] @@ -50,4 +52,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/4_everyone_is_min/solution/solution.js b/exercises/4_everyone_is_min/solution/solution.js index e3d5219..abdf27b 100644 --- a/exercises/4_everyone_is_min/solution/solution.js +++ b/exercises/4_everyone_is_min/solution/solution.js @@ -1,21 +1,22 @@ -var _ = require("lodash"); +'use strict'; +var _ = require("lodash"); -var tempsort = function(item){ +var tempsort = function (item) { var result = { hot: [], warm: [] }; - function check_temp(item){ + function check_temp (item) { return item > 19; } - _.forEach(item,function(town,townname){ + _.forEach(item, function (town, townname) { - if(_.every(town,check_temp)){ + if (_.every(town, check_temp)) { result.hot.push(townname); - }else if(_.some(town,check_temp)){ + } else if (_.some(town, check_temp)) { result.warm.push(townname); } diff --git a/exercises/5_count_the_comments/exercise.js b/exercises/5_count_the_comments/exercise.js index a9efc7d..2f9c053 100644 --- a/exercises/5_count_the_comments/exercise.js +++ b/exercises/5_count_the_comments/exercise.js @@ -1,7 +1,7 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { @@ -39,7 +39,8 @@ var testing = { { username: "foo", comment: "lalalala" - }], + } + ], shouldbe: [ { "username": "foo", @@ -48,7 +49,7 @@ var testing = { ] }, 'Testing counting right...': { - input:[ + input: [ { username: "foo", comment: "lalalala" @@ -75,4 +76,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/5_count_the_comments/solution/solution.js b/exercises/5_count_the_comments/solution/solution.js index 06d4243..a11b2dd 100644 --- a/exercises/5_count_the_comments/solution/solution.js +++ b/exercises/5_count_the_comments/solution/solution.js @@ -1,13 +1,15 @@ +'use strict'; + var _ = require("lodash"); -var commentcount = function(comments){ +var commentcount = function (comments) { var counted = []; // Group by article - var comments = _.groupBy(comments, 'username'); + comments = _.groupBy(comments, 'username'); - _.forEach(comments, function(item, name){ + _.forEach(comments, function (item, name) { counted.push({ username: name, diff --git a/exercises/6_give_me_an_overview/exercise.js b/exercises/6_give_me_an_overview/exercise.js index bffa66b..5f0e7fd 100644 --- a/exercises/6_give_me_an_overview/exercise.js +++ b/exercises/6_give_me_an_overview/exercise.js @@ -1,7 +1,7 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { @@ -72,7 +72,7 @@ var testing = { ] }, 'Checking 0 quantitys...': { - input:[ + input: [ { article: 1, quantity: 0 @@ -91,4 +91,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/6_give_me_an_overview/solution/solution.js b/exercises/6_give_me_an_overview/solution/solution.js index 5cda6d4..3a72d98 100644 --- a/exercises/6_give_me_an_overview/solution/solution.js +++ b/exercises/6_give_me_an_overview/solution/solution.js @@ -1,8 +1,11 @@ +'use strict'; + var _ = require("lodash"); -var overview = function(orders) { +var overview = function (orders) { - var overviewarray = []; + var overviewarray = [], + total = 0; // Group by article orders = _.groupBy(orders, 'article'); @@ -10,18 +13,17 @@ var overview = function(orders) { _.forEach(orders, function (item, key) { key = parseInt(key); - var total = 0; + total = 0; // If only one article - if(item.length === 1){ + if (item.length === 1) { total = item[0].quantity; // Else make sum of all orders - }else{ - + } else { total = _.reduce(item, function(sum, item) { return sum + item.quantity; - },0); + }, 0); } overviewarray.push({ diff --git a/exercises/7_analyze/exercise.js b/exercises/7_analyze/exercise.js index 90a9752..493c59f 100644 --- a/exercises/7_analyze/exercise.js +++ b/exercises/7_analyze/exercise.js @@ -1,7 +1,7 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { json: [ @@ -58,19 +58,20 @@ var testing = { shouldbe: { "average": 2563, "underperform": [{ - "name": "mike", - "income": 2563 - }], + "name": "mike", + "income": 2563 + }], "overperform": [] } }, 'Checking with different sortings': { - input:[ + input: [ { name: "mike", income: 100 }, { name: "kim", income: 99 }, { name: "foo", income: 101 } ], - shouldbe: {"average": 100, + shouldbe: { + "average": 100, "underperform": [ { "name": "kim", "income": 99 }, { "name": "mike", "income": 100 } @@ -82,4 +83,4 @@ var testing = { } }; -module.exports = verify(testing,run); \ No newline at end of file +module.exports = verify(testing, run); \ No newline at end of file diff --git a/exercises/7_analyze/solution/solution.js b/exercises/7_analyze/solution/solution.js index 320ac02..67a46cb 100644 --- a/exercises/7_analyze/solution/solution.js +++ b/exercises/7_analyze/solution/solution.js @@ -1,6 +1,8 @@ +'use strict'; + var _ = require("lodash"); -var analyze = function(item){ +var analyze = function (item) { var average, underperform, @@ -12,18 +14,18 @@ var analyze = function(item){ // Sum of all incomes average = _.reduce(item, function(sum, num) { return sum + num.income; - },0); + }, 0); + // calculate average average = average / item.length; - // filter underperformer - underperform = _.filter(item, function(num){ + underperform = _.filter(item, function (num) { return num.income <= average; }); // filter overperformer - overperform = _.filter(item, function(num){ + overperform = _.filter(item, function (num) { return num.income > average; }); diff --git a/exercises/8_start_templating/exercise.js b/exercises/8_start_templating/exercise.js index b651204..d194d74 100644 --- a/exercises/8_start_templating/exercise.js +++ b/exercises/8_start_templating/exercise.js @@ -1,10 +1,9 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"); var run = { - json: { name: "Tom", login: [ @@ -29,4 +28,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/8_start_templating/solution/solution.js b/exercises/8_start_templating/solution/solution.js index becfacf..3382b49 100644 --- a/exercises/8_start_templating/solution/solution.js +++ b/exercises/8_start_templating/solution/solution.js @@ -1,8 +1,10 @@ +'use strict'; + var _ = require("lodash"); -var template = function(inputvar) { +var template = function (inputvar) { - var mytemplate= "Hello <%= name %> (logins: <%= login.length %>)"; + var mytemplate = "Hello <%= name %> (logins: <%= login.length %>)"; return _.template(mytemplate, inputvar); }; diff --git a/exercises/9_todo_template/exercise.js b/exercises/9_todo_template/exercise.js index ef87801..2e37745 100644 --- a/exercises/9_todo_template/exercise.js +++ b/exercises/9_todo_template/exercise.js @@ -1,8 +1,8 @@ 'use strict'; -var verify = require("../../lib/verify.js"); -var _ = require("lodash"); -var moment = require("moment"); +var verify = require("../../lib/verify.js"), + _ = require("lodash"), + moment = require("moment"); var run = { @@ -90,4 +90,4 @@ var testing = { } }; -module.exports = verify(testing,run); +module.exports = verify(testing, run); diff --git a/exercises/9_todo_template/solution/solution.js b/exercises/9_todo_template/solution/solution.js index 58801a8..966b51c 100644 --- a/exercises/9_todo_template/solution/solution.js +++ b/exercises/9_todo_template/solution/solution.js @@ -1,23 +1,23 @@ +'use strict'; + var _ = require("lodash"); -var template = function(inputvar) { +var template = function (inputvar) { // Sort by date - _.forEach(inputvar, function (item, key){ + _.forEach(inputvar, function (item, key) { inputvar[key] = _.sortBy(item, "date"); }); - var helper = { - /* - Check if a given date is less than 2 days in the feature - @param date {string} - Date-String to check - @return {boolean} - */ - checkdate: function(date) { - // 24*60*60*1000*2 == 2 days in ms - return (new Date(date) - new Date()) < 24*60*60*1000*2; - } + /* + Check if a given date is less than 2 days in the feature + @param date {string} - Date-String to check + @return {boolean} + */ + var checkdate = function (date) { + // 24*60*60*1000*2 == 2 days in ms + return (new Date(date) - new Date()) < 24 * 60 * 60 * 1000 * 2; }; var mytemplate = ''; - return _.template(mytemplate, {input: inputvar}, { 'imports': { 'helper': helper } }); + return _.template(mytemplate, {input: inputvar}, { 'imports': { 'checkdate': checkdate } }); }; module.exports = template; \ No newline at end of file diff --git a/lib/verify.js b/lib/verify.js index 77d4f10..c6d4c0d 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -1,11 +1,50 @@ 'use strict'; -var path = require('path'); +var path = require('path'), + _ = require('lodash'), + exerciser = require('workshopper-exercise'), + filecheck = require('workshopper-exercise/filecheck'), + execute = require('workshopper-exercise/execute'); + + + +function stringify(obj) { + return JSON.stringify(obj, null, 2).replace(/\\n/g, "\n"); +} + +function clean(str) { + if (!str) { + return void 0; + } + + if (typeof str !== "string") { + return str; + } + + return str.replace(/\r?\n|\r/g, '').replace(/(\s)+/g, '$1'); +} + +function line(n, sep) { + n = n || 20; + sep = sep || '-'; + return (new Array(n)).join(sep); +} + +function logcompare(input, output, shouldbe) { + console.log([ + line(), + 'Input', + line(), + stringify(input), + line(), + 'Output: ' + stringify(output), + line(), + 'Output should be: ' + stringify(shouldbe), + line() + ].join('\n')); +} + -var _ = require('lodash'); -var exerciser = require('workshopper-exercise'); -var filecheck = require('workshopper-exercise/filecheck'); -var execute = require('workshopper-exercise/execute'); module.exports = function (tests, run) { var exercise = _.compose(execute, filecheck)(exerciser()); @@ -16,15 +55,14 @@ module.exports = function (tests, run) { try { usersolution = require(path.resolve(process.cwd(), this.args[0])); } catch (e) { - var message = (e.code !== 'MODULE_NOT_FOUND' - ? 'Could not find your file. Make sure the path is correct.' + var message = (e.code !== 'MODULE_NOT_FOUND' ? 'Could not find your file. Make sure the path is correct.' : 'You need to install all of the dependencies you are using in your solution (e.g. lodash)'); this.emit('fail', message); return callback(null, false); } - if (typeof usersolution !== 'function'){ + if (typeof usersolution !== 'function') { this.emit('fail', 'You should always return a function using the module.exports object.'); return callback(null, false); } @@ -45,13 +83,13 @@ module.exports = function (tests, run) { return callback(null, true); } - var allPassed = _(tests).map(function(item, element){ + var allPassed = _(tests).map(function (item, element) { var isCorrect; try { isCorrect = _.isEqual(clean(item.shouldbe), clean(usersolution(item.input))); } catch (e) { } - if(!isCorrect) { + if (!isCorrect) { self.emit('fail', element); logcompare(item.input, usersolution(item.input), item.shouldbe); return false; @@ -65,38 +103,4 @@ module.exports = function (tests, run) { }); return exercise; -}; - -function logcompare(input, output, shouldbe){ - console.log([ - line(), - 'Input', - line(), - stringify(input), - line(), - 'Output: ' + stringify(output), - line(), - 'Output should be: ' + stringify(shouldbe), - line() - ].join('\n')); -} - -function stringify(obj) { - return JSON.stringify(obj, null, 2).replace(/\\n/g,"\n"); -} - -function clean(str) { - if (!str) return void 0; - - if (typeof str !== "string") { - return str; - } - - return str.replace(/\r?\n|\r/g, '').replace(/(\s)+/g, '$1'); -} - -function line(n, sep) { - n = n || 20; - sep = sep || '-'; - return (new Array(n)).join(sep); -} \ No newline at end of file +}; \ No newline at end of file