From 35e259541fc6592288abbb8ee68a09f5d14865c1 Mon Sep 17 00:00:00 2001 From: mdunisch Date: Fri, 22 Aug 2014 10:33:57 +0200 Subject: [PATCH] - Fix typemistake in the solution of exercises 7 - Refactoring output (reduce) - Add jsdiff to the output - Add update-notifier - 0.2.0 => npm publish --- exercises/7_analyze/solution/solution.js | 2 +- lib/verify.js | 34 ++++++++++++++++++++---- lololodash.js | 8 ++++-- package.json | 4 ++- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/exercises/7_analyze/solution/solution.js b/exercises/7_analyze/solution/solution.js index 67a46cb..9bf4bd1 100644 --- a/exercises/7_analyze/solution/solution.js +++ b/exercises/7_analyze/solution/solution.js @@ -32,7 +32,7 @@ var analyze = function (item) { return { average: average, underperform: underperform, - overperfrom: overperform + overperform: overperform }; }; diff --git a/lib/verify.js b/lib/verify.js index c6d4c0d..f0b04bb 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -4,12 +4,23 @@ var path = require('path'), _ = require('lodash'), exerciser = require('workshopper-exercise'), filecheck = require('workshopper-exercise/filecheck'), - execute = require('workshopper-exercise/execute'); + execute = require('workshopper-exercise/execute'), + jsdiff = require("diff"), + colorsTmpl = require('workshopper/node_modules/colors-tmpl'); function stringify(obj) { - return JSON.stringify(obj, null, 2).replace(/\\n/g, "\n"); + var string = JSON.stringify(obj, null, " "); + + // reduce a few linebreaks + string = string.replace(/([^,])[\r\n]+/g, "$1"); + string = string.replace(/{ \"/g, '{ "'); + + // For templating-erercises (html-code line breaks) + string = string.replace(/\\n/g, "\n"); + + return string; } function clean(str) { @@ -30,6 +41,20 @@ function line(n, sep) { return (new Array(n)).join(sep); } +function diffsould(shouldbe, output) { + + var diff = jsdiff.diffChars(output, shouldbe); + var string = ""; + + diff.forEach(function (part) { + // green for additions, red for deletions, white right + var color = part.added ? 'green' : part.removed ? 'red' : 'white'; + string = string + colorsTmpl("{" + color + "}" + part.value + "{/" + color + "}"); + }); + + return string; +} + function logcompare(input, output, shouldbe) { console.log([ line(), @@ -37,15 +62,14 @@ function logcompare(input, output, shouldbe) { line(), stringify(input), line(), - 'Output: ' + stringify(output), + 'Output:\n' + diffsould(stringify(output), stringify(shouldbe)), line(), - 'Output should be: ' + stringify(shouldbe), + 'Output should be:\n' + stringify(shouldbe), line() ].join('\n')); } - module.exports = function (tests, run) { var exercise = _.compose(execute, filecheck)(exerciser()); diff --git a/lololodash.js b/lololodash.js index cf58881..b688a90 100644 --- a/lololodash.js +++ b/lololodash.js @@ -3,9 +3,13 @@ 'use strict'; var workshopper = require('workshopper'), - path = require('path'); + path = require('path'), + updateNotifier = require('update-notifier'), + pkg = require('./package.json'); -function fpath (f) { +updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify(); + +function fpath(f) { return path.join(__dirname, f); } diff --git a/package.json b/package.json index 21124e5..3461a34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lololodash", - "version": "0.1.4", + "version": "0.2.0", "description": "A Set of Lo-Dash Javascript Exercises", "main": "lololodash.js", "repository": { @@ -21,8 +21,10 @@ }, "homepage": "https://github.com/mdunisch/lololodash", "dependencies": { + "diff": "^1.0.8", "lodash": "^2.4.1", "moment": "^2.8.1", + "update-notifier": "^0.2.0", "workshopper": "^1.3.2", "workshopper-exercise": "^1.1.0" }