Skip to content

Commit 3271665

Browse files
committed
Code refactoring
1 parent e7ae5d2 commit 3271665

File tree

10 files changed

+171
-157
lines changed

10 files changed

+171
-157
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.log

1-extract-metadata/filter-metaprogramming.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Data
1+
'use strict';
2+
3+
// Data
24
//
3-
var names = [
5+
let names = [
46
'Marcus Aurelius Antoninus Augustus',
57
'Darth Vader',
68
'Victor Michailovich Glushkov',
@@ -17,13 +19,13 @@ var names = [
1719

1820
// Metadata
1921
//
20-
var conditions = {
21-
length: [ 10, 200 ],
22+
let conditions = {
23+
length: [10, 200],
2224
contains: 'Mich',
2325
starts: 'V',
2426
ends: 'ov',
2527
not: {
26-
length: [ 50, 65 ],
28+
length: [50, 65],
2729
contains: 'Abu',
2830
starts: 'Lev',
2931
ends: 'iov'
@@ -33,19 +35,19 @@ var conditions = {
3335
// Metamodel
3436
//
3537
function filter(names, conditions) {
36-
var operations = {
37-
length: function(s,v) { return s.length >= v[0] && s.length <= v[1] },
38-
contains: function(s,v) { return s.indexOf(v) > -1 },
39-
starts: function(s,v) { return s.indexOf(v) === 0 },
40-
ends: function(s,v) { return s.slice(-v.length) === v },
41-
not: function(s,v) { return !check(s,v) }
38+
let operations = {
39+
length: (s,v) => s.length >= v[0] && s.length <= v[1],
40+
contains: (s,v) => s.indexOf(v) > -1,
41+
starts: (s,v) => s.indexOf(v) === 0,
42+
ends: (s,v) => s.slice(-v.length) === v,
43+
not: (s,v) => !check(s,v)
4244
};
4345
function check(s, conditions) {
44-
var valid = true;
45-
for (var key in conditions) valid &= operations[key](s, conditions[key]);
46+
let valid = true;
47+
for (let key in conditions) valid &= operations[key](s, conditions[key]);
4648
return valid;
4749
}
48-
return names.filter(function(s) { return check(s, conditions); });
50+
return names.filter((s) => check(s, conditions));
4951
}
5052

5153
// Execution

1-extract-metadata/filter-simple.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Data
1+
'use strict';
2+
3+
// Data
24
//
3-
var names = [
5+
let names = [
46
'Marcus Aurelius Antoninus Augustus',
57
'Darth Vader',
68
'Victor Michailovich Glushkov',
@@ -18,8 +20,8 @@ var names = [
1820
// Logic
1921
//
2022
function filter(names) {
21-
var result = [], name;
22-
for (var i=0; i<names.length; i++) {
23+
let result = [], name;
24+
for (let i = 0; i < names.length; i++) {
2325
name = names[i];
2426
if (
2527
name.length >= 10 && name.length <= 200 &&

2-level-up/2-metadata.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var test = {
2-
counters: { success:0, error:0, wrong:0 },
3-
tasks: [
4-
{ interval:'5s', get:'http://127.0.0.1/api/method1.json', expect:'OK', save:'file1.json' },
5-
{ interval:'8s', get:'http://127.0.0.1/api/method2.json', put:'http://127.0.0.1/api/method4.json', save:'file2.json' },
6-
{ interval:'7s', get:'http://127.0.0.1/api/method3.json', expect:'Done', post:'http://127.0.0.1/api/method5.json' },
7-
{ interval:'4s', load:'file1.json', expect:'OK', put:'http://127.0.0.1/api/method6.json' },
8-
{ interval:'9s', load:'file2.json', post:'http://127.0.0.1/api/method7.json', save:'file1.json' },
9-
{ interval:'3s', load:'file1.json', save:'file3.json' },
10-
],
11-
error: function(err, request) {
12-
console.log('Error');
13-
},
14-
success: function(err, request) {
15-
test.counters.success++;
16-
}
17-
};
1+
var test = {
2+
counters: { success:0, error:0, wrong:0 },
3+
tasks: [
4+
{ interval:'5s', get:'http://127.0.0.1/api/method1.json', expect:'OK', save:'file1.json' },
5+
{ interval:'8s', get:'http://127.0.0.1/api/method2.json', put:'http://127.0.0.1/api/method4.json', save:'file2.json' },
6+
{ interval:'7s', get:'http://127.0.0.1/api/method3.json', expect:'Done', post:'http://127.0.0.1/api/method5.json' },
7+
{ interval:'4s', load:'file1.json', expect:'OK', put:'http://127.0.0.1/api/method6.json' },
8+
{ interval:'9s', load:'file2.json', post:'http://127.0.0.1/api/method7.json', save:'file1.json' },
9+
{ interval:'3s', load:'file1.json', save:'file3.json' },
10+
],
11+
error: function(err, request) {
12+
console.log('Error');
13+
},
14+
success: function(err, request) {
15+
test.counters.success++;
16+
}
17+
};

2-level-up/2-speedtest.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
var duration1 = function(s) {
2-
var result = 0;
3-
if (typeof(s) === 'string') {
4-
var days = s.match(/(\d+)\s*d/),
5-
hours = s.match(/(\d+)\s*h/),
6-
minutes = s.match(/(\d+)\s*m/),
7-
seconds = s.match(/(\d+)\s*s/);
8-
if (days) result += parseInt(days[1])*86400;
9-
if (hours) result += parseInt(hours[1])*3600;
10-
if (minutes) result += parseInt(minutes[1])*60;
11-
if (seconds) result += parseInt(seconds[1]);
12-
result = result*1000;
13-
} if (typeof(s) === 'number') result = s;
14-
return result;
15-
};
16-
17-
var duration2 = function(s) {
18-
var result = 0;
19-
if (typeof(s) === 'string') {
20-
var days = s.match(/(\d+)\s*d/),
21-
hours = s.match(/(\d+)\s*h/),
22-
minutes = s.match(/(\d+)\s*m/),
23-
seconds = s.match(/(\d+)\s*s/);
24-
if (days) result += parseInt(days[1])*86400;
25-
if (hours) result += parseInt(hours[1])*3600;
26-
if (minutes) result += parseInt(minutes[1])*60;
27-
if (seconds) result += parseInt(seconds[1]);
28-
result = result * 1000;
29-
} if (typeof(s) === 'number') result = s;
30-
return result;
31-
};
32-
33-
for (k = 0; k < 9; ++k) {
34-
35-
var start = new Date().getTime();
36-
for (i = 0; i < 1000000; ++i) {
37-
duration2('1d 10h 7m 13s');
38-
}
39-
var end = new Date().getTime();
40-
var time = end - start;
41-
console.log('duration1 Execution time (1mln): ' + time);
42-
43-
var start = new Date().getTime();
44-
for (i = 0; i < 1000000; ++i) {
45-
duration2('1d 10h 7m 13s');
46-
}
47-
var end = new Date().getTime();
48-
var time = end - start;
49-
console.log('duration2 Execution time (1mln): ' + time);
50-
51-
}
1+
var duration1 = function(s) {
2+
var result = 0;
3+
if (typeof(s) === 'string') {
4+
var days = s.match(/(\d+)\s*d/),
5+
hours = s.match(/(\d+)\s*h/),
6+
minutes = s.match(/(\d+)\s*m/),
7+
seconds = s.match(/(\d+)\s*s/);
8+
if (days) result += parseInt(days[1])*86400;
9+
if (hours) result += parseInt(hours[1])*3600;
10+
if (minutes) result += parseInt(minutes[1])*60;
11+
if (seconds) result += parseInt(seconds[1]);
12+
result = result*1000;
13+
} if (typeof(s) === 'number') result = s;
14+
return result;
15+
};
16+
17+
var duration2 = function(s) {
18+
var result = 0;
19+
if (typeof(s) === 'string') {
20+
var days = s.match(/(\d+)\s*d/),
21+
hours = s.match(/(\d+)\s*h/),
22+
minutes = s.match(/(\d+)\s*m/),
23+
seconds = s.match(/(\d+)\s*s/);
24+
if (days) result += parseInt(days[1])*86400;
25+
if (hours) result += parseInt(hours[1])*3600;
26+
if (minutes) result += parseInt(minutes[1])*60;
27+
if (seconds) result += parseInt(seconds[1]);
28+
result = result * 1000;
29+
} if (typeof(s) === 'number') result = s;
30+
return result;
31+
};
32+
33+
for (k = 0; k < 9; ++k) {
34+
35+
var start = new Date().getTime();
36+
for (i = 0; i < 1000000; ++i) {
37+
duration2('1d 10h 7m 13s');
38+
}
39+
var end = new Date().getTime();
40+
var time = end - start;
41+
console.log('duration1 Execution time (1mln): ' + time);
42+
43+
var start = new Date().getTime();
44+
for (i = 0; i < 1000000; ++i) {
45+
duration2('1d 10h 7m 13s');
46+
}
47+
var end = new Date().getTime();
48+
var time = end - start;
49+
console.log('duration2 Execution time (1mln): ' + time);
50+
51+
}

2-level-up/metametaprogramming.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
var fs = require('fs'),
1+
'use strict';
2+
3+
let fs = require('fs'),
24
request = require('request');
35

46
// Parse duration to seconds
5-
// Example: duration("1d 10h 7m 13s")
7+
// Example: duration('1d 10h 7m 13s')
68
//
79
function duration(s) {
8-
if (typeof(s) == 'number') return s;
9-
var units = {
10-
days: { rx:/(\d+)\s*d/, mul:86400 },
11-
hours: { rx:/(\d+)\s*h/, mul:3600 },
12-
minutes: { rx:/(\d+)\s*m/, mul:60 },
13-
seconds: { rx:/(\d+)\s*s/, mul:1 }
10+
if (typeof(s) === 'number') return s;
11+
let units = {
12+
days: { rx: /(\d+)\s*d/, mul: 86400 },
13+
hours: { rx: /(\d+)\s*h/, mul: 3600 },
14+
minutes: { rx: /(\d+)\s*m/, mul: 60 },
15+
seconds: { rx: /(\d+)\s*s/, mul: 1 }
1416
};
15-
var result = 0, unit, match;
16-
if (typeof(s) == 'string') for (var key in units) {
17+
let result = 0, unit, match;
18+
if (typeof(s) === 'string') for (let key in units) {
1719
unit = units[key];
1820
match = s.match(unit.rx);
19-
if (match) result += parseInt(match[1])*unit.mul;
21+
if (match) result += parseInt(match[1]) * unit.mul;
2022
}
21-
return result*1000;
23+
return result * 1000;
2224
}
2325

2426
// Metadata
2527
//
26-
var tasks = [
27-
{ interval:5000, get:"http://127.0.0.1/api/method1.json", expect:"OK", save:"file1.json" },
28-
{ interval:"8s", get:"http://127.0.0.1/api/method2.json", put:"http://127.0.0.1/api/method4.json", save:"file2.json" },
29-
{ interval:"7s", get:"http://127.0.0.1/api/method3.json", expect:"Done", post:"http://127.0.0.1/api/method5.json" },
30-
{ interval:"4s", load:"file1.json", expect:"OK", put:"http://127.0.0.1/api/method6.json" },
31-
{ interval:"9s", load:"file2.json", post:"http://127.0.0.1/api/method7.json", save:"file1.json" },
32-
{ interval:"3s", load:"file1.json", save:"file3.json" },
28+
let tasks = [
29+
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
30+
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
31+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', expect: 'Done', post: 'http://127.0.0.1/api/method5.json' },
32+
{ interval: '4s', load: 'file1.json', expect: 'OK', put: 'http://127.0.0.1/api/method6.json' },
33+
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
34+
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
3335
];
3436

3537
// Metamodel
@@ -38,11 +40,11 @@ function iterate(tasks) {
3840

3941
// Metamodel configuration metadata
4042
//
41-
var sources = {
43+
let sources = {
4244
get: request.get,
4345
load: fs.createReadStream
4446
};
45-
var destinations = {
47+
let destinations = {
4648
save: fs.createWriteStream,
4749
post: request.post,
4850
put: request.put
@@ -51,14 +53,16 @@ function iterate(tasks) {
5153
// Metamodel logic
5254
//
5355
function closureTask(task) {
54-
return function () {
56+
return () => {
5557
console.dir(task);
56-
var key, verb, source, destination;
58+
let key, verb, source, destination;
5759
for (key in sources) if (task[key]) source = sources[key](task[key]);
5860
for (key in destinations) if (task[key]) source.pipe(destinations[key](task[key]));
5961
};
6062
}
61-
for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
63+
for (let i = 0; i < tasks.length; i++) {
64+
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
65+
}
6266
}
6367

6468
// Execution

2-level-up/metaprogramming.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
var fs = require('fs'),
1+
'use strict';
2+
3+
var fs = require('fs'),
24
request = require('request');
35

46
// Parse duration to seconds
5-
// Example: duration("1d 10h 7m 13s")
7+
// Example: duration('1d 10h 7m 13s')
68
//
79
function duration(s) {
810
var result = 0;
9-
if (typeof(s) == 'string') {
11+
if (typeof(s) === 'string') {
1012
var days = s.match(/(\d+)\s*d/),
1113
hours = s.match(/(\d+)\s*h/),
1214
minutes = s.match(/(\d+)\s*m/),
1315
seconds = s.match(/(\d+)\s*s/);
14-
if (days) result += parseInt(days[1])*86400;
15-
if (hours) result += parseInt(hours[1])*3600;
16-
if (minutes) result += parseInt(minutes[1])*60;
16+
if (days) result += parseInt(days[1]) * 86400;
17+
if (hours) result += parseInt(hours[1]) * 3600;
18+
if (minutes) result += parseInt(minutes[1]) * 60;
1719
if (seconds) result += parseInt(seconds[1]);
18-
result = result*1000;
19-
} if (typeof(s) == 'number') result = s;
20+
result = result * 1000;
21+
} if (typeof(s) === 'number') result = s;
2022
return result;
2123
}
2224

2325
// Metadata
2426
//
25-
var tasks= [
26-
{ interval:5000, get:"http://127.0.0.1/api/method1.json", expect:"OK", save:"file1.json" },
27-
{ interval:"8s", get:"http://127.0.0.1/api/method2.json", put:"http://127.0.0.1/api/method4.json", save:"file2.json" },
28-
{ interval:"7s", get:"http://127.0.0.1/api/method3.json", expect:"Done", post:"http://127.0.0.1/api/method5.json" },
29-
{ interval:"4s", load:"file1.json", expect:"OK", put:"http://127.0.0.1/api/method6.json" },
30-
{ interval:"9s", load:"file2.json", post:"http://127.0.0.1/api/method7.json", save:"file1.json" },
31-
{ interval:"3s", load:"file1.json", save:"file3.json" },
27+
var tasks = [
28+
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
29+
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
30+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', expect: 'Done', post: 'http://127.0.0.1/api/method5.json' },
31+
{ interval: '4s', load: 'file1.json', expect: 'OK', put: 'http://127.0.0.1/api/method6.json' },
32+
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
33+
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
3234
];
3335

3436
// Metamodel
3537
//
3638
function iterate(tasks) {
3739
function closureTask(task) {
38-
return function () {
40+
return () => {
3941
console.dir(task);
4042
var source;
4143
if (task.get) source = request.get(task.get);
@@ -45,7 +47,9 @@ function iterate(tasks) {
4547
if (task.put) source.pipe(request.put(task.put));
4648
};
4749
}
48-
for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
50+
for (var i = 0; i < tasks.length; i++) {
51+
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
52+
}
4953
}
5054

5155
// Execution

3-introspection/wcl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function(wcl) {
1+
(function(wcl) {
22

33
wcl.AjaxAPI = function(methods) { // params: { method: { get/post:url }, ... }
44
var api = {};
@@ -98,7 +98,7 @@
9898
};
9999
return ds;
100100
};
101-
101+
102102
wcl.parse = function(json) {
103103
var result;
104104
eval('result = new Object(' + json + ')');

0 commit comments

Comments
 (0)