Skip to content

Commit e7ae5d2

Browse files
committed
Code style fixes
1 parent 111e241 commit e7ae5d2

File tree

11 files changed

+381
-315
lines changed

11 files changed

+381
-315
lines changed

1-extract-metadata/filter-metaprogramming.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
// Data
22
//
33
var names = [
4-
"Marcus Aurelius Antoninus Augustus",
5-
"Darth Vader",
6-
"Victor Michailovich Glushkov",
7-
"Gottfried Wilhelm von Leibniz",
8-
"Mao Zedong",
9-
"Vladimir Sergeevich Soloviov",
10-
"Ibn Arabi",
11-
"Lev Nikolayevich Tolstoy",
12-
"Muammar Muhammad Abu Minyar al-Gaddafi",
13-
"Rene Descartes",
14-
"Fyodor Mikhailovich Dostoyevsky",
15-
"Benedito de Espinosa"
4+
'Marcus Aurelius Antoninus Augustus',
5+
'Darth Vader',
6+
'Victor Michailovich Glushkov',
7+
'Gottfried Wilhelm von Leibniz',
8+
'Mao Zedong',
9+
'Vladimir Sergeevich Soloviov',
10+
'Ibn Arabi',
11+
'Lev Nikolayevich Tolstoy',
12+
'Muammar Muhammad Abu Minyar al-Gaddafi',
13+
'Rene Descartes',
14+
'Fyodor Mikhailovich Dostoyevsky',
15+
'Benedito de Espinosa'
1616
];
1717

1818
// Metadata
1919
//
2020
var conditions = {
21-
length: [10, 200],
22-
contains: "Mich",
23-
starts: "V",
24-
ends: "ov",
25-
not: {
26-
length: [50, 65],
27-
contains: "Abu",
28-
starts: "Lev",
29-
ends: "iov"
30-
}
21+
length: [ 10, 200 ],
22+
contains: 'Mich',
23+
starts: 'V',
24+
ends: 'ov',
25+
not: {
26+
length: [ 50, 65 ],
27+
contains: 'Abu',
28+
starts: 'Lev',
29+
ends: 'iov'
30+
}
3131
};
3232

3333
// Metamodel
3434
//
3535
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) }
42-
};
43-
function check(s, conditions) {
44-
var valid = true;
45-
for (var key in conditions) valid &= operations[key](s, conditions[key]);
46-
return valid;
47-
}
48-
return names.filter(function(s) { return check(s, 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) }
42+
};
43+
function check(s, conditions) {
44+
var valid = true;
45+
for (var key in conditions) valid &= operations[key](s, conditions[key]);
46+
return valid;
47+
}
48+
return names.filter(function(s) { return check(s, conditions); });
4949
}
5050

5151
// Execution

1-extract-metadata/filter-simple.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
// Data
22
//
33
var names = [
4-
"Marcus Aurelius Antoninus Augustus",
5-
"Darth Vader",
6-
"Victor Michailovich Glushkov",
7-
"Gottfried Wilhelm von Leibniz",
8-
"Mao Zedong",
9-
"Vladimir Sergeevich Soloviov",
10-
"Ibn Arabi",
11-
"Lev Nikolayevich Tolstoy",
12-
"Muammar Muhammad Abu Minyar al-Gaddafi",
13-
"Rene Descartes",
14-
"Fyodor Mikhailovich Dostoyevsky",
15-
"Benedito de Espinosa"
4+
'Marcus Aurelius Antoninus Augustus',
5+
'Darth Vader',
6+
'Victor Michailovich Glushkov',
7+
'Gottfried Wilhelm von Leibniz',
8+
'Mao Zedong',
9+
'Vladimir Sergeevich Soloviov',
10+
'Ibn Arabi',
11+
'Lev Nikolayevich Tolstoy',
12+
'Muammar Muhammad Abu Minyar al-Gaddafi',
13+
'Rene Descartes',
14+
'Fyodor Mikhailovich Dostoyevsky',
15+
'Benedito de Espinosa'
1616
];
1717

1818
// Logic
1919
//
2020
function filter(names) {
21-
var result = [], name;
22-
for (var i=0; i<names.length; i++) {
23-
name = names[i];
24-
if (
25-
name.length>=10 && name.length<=200 &&
26-
name.indexOf("Mich") > -1 &&
27-
name.indexOf("V") === 0 &&
28-
name.slice(-2) == "ov" &&
29-
!(
30-
name.length>=50 && name.length<=65 &&
31-
name.indexOf("Abu") > -1 &&
32-
name.indexOf("Lev") === 0 &&
33-
name.slice(-3) == "iov"
34-
)
35-
) result.push(name);
36-
}
37-
return result;
21+
var result = [], name;
22+
for (var i=0; i<names.length; i++) {
23+
name = names[i];
24+
if (
25+
name.length >= 10 && name.length <= 200 &&
26+
name.indexOf('Mich') > -1 &&
27+
name.indexOf('V') === 0 &&
28+
name.slice(-2) === 'ov' &&
29+
!(
30+
name.length >= 50 && name.length <= 65 &&
31+
name.indexOf('Abu') > -1 &&
32+
name.indexOf('Lev') === 0 &&
33+
name.slice(-3) === 'iov'
34+
)
35+
) result.push(name);
36+
}
37+
return result;
3838
}
3939

4040
// Execution

2-level-up/2-metadata.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
};

2-level-up/2-speedtest.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}

2-level-up/file1.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
{"empty":"file"}

2-level-up/file3.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
{"empty":"file"}

2-level-up/metametaprogramming.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
var fs = require('fs'),
2-
request = require('request');
2+
request = require('request');
33

44
// Parse duration to seconds
55
// Example: duration("1d 10h 7m 13s")
66
//
77
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 }
14-
};
15-
var result = 0, unit, match;
16-
if (typeof(s) == 'string') for (var key in units) {
17-
unit = units[key];
18-
match = s.match(unit.rx);
19-
if (match) result += parseInt(match[1])*unit.mul;
20-
}
21-
return result*1000;
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 }
14+
};
15+
var result = 0, unit, match;
16+
if (typeof(s) == 'string') for (var key in units) {
17+
unit = units[key];
18+
match = s.match(unit.rx);
19+
if (match) result += parseInt(match[1])*unit.mul;
20+
}
21+
return result*1000;
2222
}
2323

2424
// Metadata
2525
//
2626
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" },
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" },
3333
];
3434

3535
// Metamodel
3636
//
3737
function iterate(tasks) {
3838

39-
// Metamodel configuration metadata
40-
//
41-
var sources = {
42-
get: request.get,
43-
load: fs.createReadStream
44-
};
45-
var destinations = {
46-
save: fs.createWriteStream,
47-
post: request.post,
48-
put: request.put
49-
};
39+
// Metamodel configuration metadata
40+
//
41+
var sources = {
42+
get: request.get,
43+
load: fs.createReadStream
44+
};
45+
var destinations = {
46+
save: fs.createWriteStream,
47+
post: request.post,
48+
put: request.put
49+
};
5050

51-
// Metamodel logic
52-
//
53-
function closureTask(task) {
54-
return function () {
55-
console.dir(task);
56-
var verb, source, destination;
57-
for (key in sources) if (task[key]) source = sources[key](task[key]);
58-
for (key in destinations) if (task[key]) source.pipe(destinations[key](task[key]));
59-
}
60-
}
61-
for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
51+
// Metamodel logic
52+
//
53+
function closureTask(task) {
54+
return function () {
55+
console.dir(task);
56+
var key, verb, source, destination;
57+
for (key in sources) if (task[key]) source = sources[key](task[key]);
58+
for (key in destinations) if (task[key]) source.pipe(destinations[key](task[key]));
59+
};
60+
}
61+
for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
6262
}
6363

6464
// Execution

0 commit comments

Comments
 (0)