Skip to content

Commit

Permalink
- Fix typemistake in the solution of exercises 7
Browse files Browse the repository at this point in the history
- Refactoring output (reduce)
- Add jsdiff to the output
- Add update-notifier
- 0.2.0 => npm publish
  • Loading branch information
mdunisch committed Aug 22, 2014
1 parent 7cba91a commit 35e2595
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion exercises/7_analyze/solution/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var analyze = function (item) {
return {
average: average,
underperform: underperform,
overperfrom: overperform
overperform: overperform
};

};
Expand Down
34 changes: 29 additions & 5 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,22 +41,35 @@ 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(),
'Input',
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());

Expand Down
8 changes: 6 additions & 2 deletions lololodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
Expand Down

0 comments on commit 35e2595

Please sign in to comment.