Skip to content

Commit e2c35f0

Browse files
committed
Use StandardJS
I noticed that other workshoppers like `learnyounode` are using StandardJS style. This commit updates `javascripting` to use it, as well. https://standardjs.com
1 parent a3a1ef1 commit e2c35f0

275 files changed

Lines changed: 1058 additions & 1078 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LOCALIZING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ In the `index.js` file, the workshopper is instantiated with a list of supported
1010

1111
```js
1212
const workshopper = require('workshopper-adventure')({
13-
appDir: __dirname
14-
, languages: ['en', 'ja', 'zh-cn']
15-
, header: require('workshopper-adventure/default/header')
16-
, footer: require('./lib/footer.js')
17-
});
13+
appDir: __dirname,
14+
languages: ['en', 'ja', 'zh-cn'],
15+
header: require('workshopper-adventure/default/header'),
16+
footer: require('./lib/footer.js')
17+
})
1818
```
1919

2020
If you want to add a new language, e.g. Spanish, add an entry `'es'` to the array:

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
var jsing = require('workshopper-adventure')({
2-
appDir: __dirname
3-
, languages: ['en', 'ja', 'ko', 'es', 'zh-cn', 'zh-tw', 'pt-br', 'nb-no', 'uk', 'it', 'ru', 'fr']
4-
, header: require('workshopper-adventure/default/header')
5-
, footer: require('./lib/footer.js')
6-
});
2+
appDir: __dirname,
3+
languages: ['en', 'ja', 'ko', 'es', 'zh-cn', 'zh-tw', 'pt-br', 'nb-no', 'uk', 'it', 'ru', 'fr'],
4+
header: require('workshopper-adventure/default/header'),
5+
footer: require('./lib/footer.js')
6+
})
77

88
jsing.addAll(require('./menu.json').map(function (problem) {
99
return {
1010
name: problem,
1111
fn: function () {
12-
var p = problem.toLowerCase().replace(/\s/g, '-');
13-
var dir = require('path').join(__dirname, 'problems', p);
14-
return require(dir);
12+
var p = problem.toLowerCase().replace(/\s/g, '-')
13+
var dir = require('path').join(__dirname, 'problems', p)
14+
return require(dir)
1515
}
1616
}
1717
}))
1818

19-
module.exports = jsing;
19+
module.exports = jsing

lib/compare-solution.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,49 @@
1-
require("colors");
1+
require('colors')
22

3-
var path = require("path");
4-
var diff = require("diff");
5-
var run = require(path.join(__dirname, "run-solution"));
3+
var path = require('path')
4+
var diff = require('diff')
5+
var run = require(path.join(__dirname, 'run-solution'))
66

7-
module.exports = function(solution, attempt, i18n, cb) {
8-
run(solution, i18n, function(err, solutionResult) {
9-
10-
if(err) {
11-
console.error(err);
12-
return cb(false);
7+
module.exports = function (solution, attempt, i18n, cb) {
8+
run(solution, i18n, function (err, solutionResult) {
9+
if (err) {
10+
console.error(err)
11+
return cb(false)
1312
}
1413

15-
run(attempt, i18n, function(err, attemptResult) {
16-
17-
if(err && err.code !== 8) {
18-
console.error(err);
19-
return cb(false);
14+
run(attempt, i18n, function (err, attemptResult) {
15+
if (err && err.code !== 8) {
16+
console.error(err)
17+
return cb(false)
2018
}
2119

22-
if(solutionResult === attemptResult) {
23-
return cb(true);
20+
if (solutionResult === attemptResult) {
21+
return cb(true)
2422
}
2523

2624
cb(false, {
2725
solution: solutionResult,
28-
attempt: err || attemptResult,
29-
diff: generateDiff(solutionResult, attemptResult)
30-
});
31-
32-
});
33-
34-
});
35-
26+
attempt: err || attemptResult,
27+
diff: generateDiff(solutionResult, attemptResult)
28+
})
29+
})
30+
})
3631
}
3732

38-
function generateDiff(solution, attempt) {
33+
function generateDiff (solution, attempt) {
34+
var parts = diff.diffChars(solution, attempt)
3935

40-
var parts = diff.diffChars(solution, attempt);
36+
var result = ''
4137

42-
var result = "";
43-
44-
parts.forEach(function(part) {
45-
46-
if(part.added) {
47-
result += part.value["bgRed"];
48-
} else if(part.removed) {
49-
result += part.value["bgGreen"];
38+
parts.forEach(function (part) {
39+
if (part.added) {
40+
result += part.value.bgRed
41+
} else if (part.removed) {
42+
result += part.value.bgGreen
5043
} else {
51-
result += part.value;
44+
result += part.value
5245
}
46+
})
5347

54-
});
55-
56-
return result;
57-
48+
return result
5849
}

lib/footer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var path = require('path')
22
module.exports = [
3-
{text: '---', type: 'md'}
4-
, {file: path.join(__dirname, '..', 'i18n', 'footer', '{lang}.md')}
5-
, {text: '', type: 'md'}
6-
, require('workshopper-adventure/default/footer')
7-
]
3+
{ text: '---', type: 'md' },
4+
{ file: path.join(__dirname, '..', 'i18n', 'footer', '{lang}.md') },
5+
{ text: '', type: 'md' },
6+
require('workshopper-adventure/default/footer')
7+
]

lib/get-file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var fs = require('fs');
2-
var path = require('path');
1+
var fs = require('fs')
2+
var path = require('path')
33

44
module.exports = function (filepath) {
55
return fs.readFileSync(filepath, 'utf8')
66
.replace(/'/g, "'")
77
.replace(/"/g, '"')
88
.replace(/&lt;/g, '<')
9-
.replace(/&gt;/g, '>');
10-
};
9+
.replace(/&gt;/g, '>')
10+
}

lib/problem.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
1-
var path = require('path');
2-
var getFile = require('./get-file');
3-
var compare = require('./compare-solution');
1+
var path = require('path')
2+
var getFile = require('./get-file')
3+
var compare = require('./compare-solution')
44

5-
module.exports = function createProblem(dirname) {
6-
var exports = {};
5+
module.exports = function createProblem (dirname) {
6+
var exports = {}
77

8-
var problemName = dirname.split(path.sep);
9-
var i18n;
8+
var problemName = dirname.split(path.sep)
9+
var i18n
1010

11-
problemName = problemName[problemName.length-1];
11+
problemName = problemName[problemName.length - 1]
1212

1313
exports.init = function (workshopper) {
14-
i18n = workshopper.i18n;
15-
var postfix = workshopper.i18n.lang() === 'en' ? '' : '_' + workshopper.i18n.lang();
16-
this.problem = {file: path.join(dirname, 'problem' + postfix + '.md')};
17-
this.solution = {file: path.join(dirname, 'solution' + postfix + '.md')};
18-
this.solutionPath = path.resolve(__dirname, '..', 'solutions', problemName, "index.js");
19-
this.troubleshootingPath = path.join(__dirname, '..', 'i18n', 'troubleshooting' + postfix + '.md');
14+
i18n = workshopper.i18n
15+
var postfix = workshopper.i18n.lang() === 'en' ? '' : '_' + workshopper.i18n.lang()
16+
this.problem = { file: path.join(dirname, 'problem' + postfix + '.md') }
17+
this.solution = { file: path.join(dirname, 'solution' + postfix + '.md') }
18+
this.solutionPath = path.resolve(__dirname, '..', 'solutions', problemName, 'index.js')
19+
this.troubleshootingPath = path.join(__dirname, '..', 'i18n', 'troubleshooting' + postfix + '.md')
2020
}
2121

2222
exports.verify = function (args, cb) {
23-
24-
var attemptPath = path.resolve(process.cwd(), args[0]);
25-
compare(this.solutionPath, attemptPath, i18n, function(match, obj) {
26-
27-
if(match) {
28-
return cb(true);
23+
var attemptPath = path.resolve(process.cwd(), args[0])
24+
compare(this.solutionPath, attemptPath, i18n, function (match, obj) {
25+
if (match) {
26+
return cb(true)
2927
}
3028

31-
if(!obj) {
29+
if (!obj) {
3230
// An error occured, we've already printed an error
33-
return;
31+
return
3432
}
3533

36-
var message = getFile(this.troubleshootingPath);
34+
var message = getFile(this.troubleshootingPath)
3735

38-
message = message.replace(/%solution%/g, obj.solution);
39-
message = message.replace(/%attempt%/g, obj.attempt);
40-
message = message.replace(/%diff%/g, obj.diff);
41-
message = message.replace(/%filename%/g, args[0]);
36+
message = message.replace(/%solution%/g, obj.solution)
37+
message = message.replace(/%attempt%/g, obj.attempt)
38+
message = message.replace(/%diff%/g, obj.diff)
39+
message = message.replace(/%filename%/g, args[0])
4240

4341
exports.fail = [
44-
{text: message, type: 'md' },
42+
{ text: message, type: 'md' },
4543
require('./footer.js')
4644
]
4745

48-
cb(false);
49-
50-
}.bind(this));
51-
};
46+
cb(false)
47+
}.bind(this))
48+
}
5249

53-
return exports;
50+
return exports
5451
}

lib/run-solution.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
var docs = path.join(__dirname, 'docs');
4-
var exec = require('child_process').exec;
1+
var fs = require('fs')
2+
var path = require('path')
3+
var docs = path.join(__dirname, 'docs')
4+
var exec = require('child_process').exec
55

66
if (typeof Promise === 'undefined') {
7-
var Promise = require('promise');
7+
var Promise = require('promise')
88
}
99

1010
/**
1111
* @param {!string} filePath
1212
* @return {Promise}
1313
*/
14-
function exists(filePath) {
15-
return new Promise(function(res, rej) {
16-
fs.stat(filePath, function(err, d) {
14+
function exists (filePath) {
15+
return new Promise(function (res, rej) {
16+
fs.stat(filePath, function (err, d) {
1717
if (err) {
18-
res(false);
18+
res(false)
1919
}
2020

21-
res(true);
22-
});
23-
});
21+
res(true)
22+
})
23+
})
2424
}
2525

2626
/**
2727
* @param {!string} filePath
2828
* @return {Promise}
2929
*/
30-
function executeSolution(filePath) {
31-
return new Promise(function(res, rej) {
30+
function executeSolution (filePath) {
31+
return new Promise(function (res, rej) {
3232
exec('node "' + filePath + '"', function (err, stdout, stderr) {
3333
if (err) {
34-
return rej(err);
34+
return rej(err)
3535
}
3636

37-
return res(stdout);
38-
});
39-
});
37+
return res(stdout)
38+
})
39+
})
4040
}
4141

42-
4342
/**
4443
* @param {string} solutionPath
4544
* @param {!{__: function(string, object)}} i18n
4645
* @param {function} cb
4746
*/
4847
module.exports = function (solutionPath, i18n, cb) {
49-
exists(solutionPath).then(function(solutionExists) {
48+
exists(solutionPath).then(function (solutionExists) {
5049
if (!solutionExists) {
51-
throw new Error(i18n.__('error.exercise.missing_file', {exerciseFile: solutionPath}));
50+
throw new Error(i18n.__('error.exercise.missing_file', { exerciseFile: solutionPath }))
5251
}
5352

54-
return executeSolution(solutionPath);
55-
}).then(function(stdout) {
56-
cb(null, stdout);
57-
}).catch(cb);
53+
return executeSolution(solutionPath)
54+
}).then(function (stdout) {
55+
cb(null, stdout)
56+
}).catch(cb)
5857
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@
1717
"diff": "^4.0.1",
1818
"workshopper-adventure": "^6.0.4"
1919
},
20-
"license": "MIT"
20+
"license": "MIT",
21+
"devDependencies": {
22+
"standard": "^14.2.0"
23+
},
24+
"scripts": {
25+
"test": "standard"
26+
}
2127
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require("../../lib/problem")(__dirname)
1+
module.exports = require('../../lib/problem')(__dirname)

problems/accessing-array-values/problem.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Here is an example:
66

77

88
```js
9-
const pets = ['cat', 'dog', 'rat'];
9+
const pets = ['cat', 'dog', 'rat']
1010

11-
console.log(pets[0]);
11+
console.log(pets[0])
1212
```
1313

1414
The above code will print the first element of `pets` array - string `cat`.
@@ -20,7 +20,7 @@ Dot notation is invalid.
2020
Valid notation:
2121

2222
```js
23-
console.log(pets[0]);
23+
console.log(pets[0])
2424
```
2525

2626
Invalid notation:
@@ -34,7 +34,7 @@ Create a file named `accessing-array-values.js`.
3434

3535
In that file, define array `food` :
3636
```js
37-
const food = ['apple', 'pizza', 'pear'];
37+
const food = ['apple', 'pizza', 'pear']
3838
```
3939

4040

0 commit comments

Comments
 (0)