Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e6723ec

Browse files
committedAug 19, 2016
Code style
1 parent 854f595 commit e6723ec

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed
 

‎1-extract-metadata/filter-metaprogramming.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ let conditions = {
3636
//
3737
function filter(names, conditions) {
3838
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)
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)
4444
};
4545
function check(s, conditions) {
4646
let valid = true;
4747
for (let key in conditions) valid &= operations[key](s, conditions[key]);
4848
return valid;
4949
}
50-
return names.filter((s) => check(s, conditions));
50+
return names.filter(s => check(s, conditions));
5151
}
5252

5353
// Execution

‎2-level-up/metadata.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
counters: { success: 0, error: 0, wrong: 0 },
33
tasks: [
4-
{ interval: '5s', get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
4+
{ interval: '5s', get: 'http://127.0.0.1/api/method1.json', save: 'file1.json' },
55
{ 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' },
6+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', post: 'http://127.0.0.1/api/method5.json' },
7+
{ interval: '4s', load: 'file1.json', put: 'http://127.0.0.1/api/method6.json' },
88
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
99
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
1010
],
1111
error: (err, request) => console.log('Error'),
12-
success: (err, request) => test.counters.success++
12+
success: (err, request) => counters.success++
1313
}

‎2-level-up/metametaprogramming.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
let fs = require('fs'),
4-
request = require('request');
3+
global.api = {};
4+
api.fs = require('fs'),
5+
api.request = require('request');
56

67
// Parse duration to seconds
78
// Example: duration('1d 10h 7m 13s')
@@ -41,13 +42,13 @@ function iterate(tasks) {
4142
// Metamodel configuration metadata
4243
//
4344
let sources = {
44-
get: request.get,
45-
load: fs.createReadStream
45+
get: api.request.get,
46+
load: api.fs.createReadStream
4647
};
4748
let destinations = {
48-
save: fs.createWriteStream,
49-
post: request.post,
50-
put: request.put
49+
save: api.fs.createWriteStream,
50+
post: api.request.post,
51+
put: api.request.put
5152
};
5253

5354
// Metamodel logic

‎2-level-up/metaprogramming.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22

3-
var fs = require('fs'),
4-
request = require('request');
3+
global.api = {};
4+
api.fs = require('fs'),
5+
api.request = require('request');
56

67
// Parse duration to seconds
78
// Example: duration('1d 10h 7m 13s')
89
//
910
function duration(s) {
10-
var result = 0;
11+
let result = 0;
1112
if (typeof(s) === 'string') {
12-
var days = s.match(/(\d+)\s*d/),
13+
let days = s.match(/(\d+)\s*d/),
1314
hours = s.match(/(\d+)\s*h/),
1415
minutes = s.match(/(\d+)\s*m/),
1516
seconds = s.match(/(\d+)\s*s/);
@@ -24,13 +25,13 @@ function duration(s) {
2425

2526
// Metadata
2627
//
27-
var tasks = [
28+
let tasks = [
2829
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', save: 'file1.json' },
2930
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
3031
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', post: 'http://127.0.0.1/api/method5.json' },
3132
{ interval: '4s', load: 'file1.json', put: 'http://127.0.0.1/api/method6.json' },
3233
{ 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' },
34+
{ interval: '3s', load: 'file1.json', save: 'file3.json' }
3435
];
3536

3637
// Metamodel
@@ -39,15 +40,15 @@ function iterate(tasks) {
3940
function closureTask(task) {
4041
return () => {
4142
console.dir(task);
42-
var source;
43-
if (task.get) source = request.get(task.get);
44-
if (task.load) source = fs.createReadStream(task.load);
45-
if (task.save) source.pipe(fs.createWriteStream(task.save));
46-
if (task.post) source.pipe(request.post(task.post));
47-
if (task.put) source.pipe(request.put(task.put));
43+
let source;
44+
if (task.get) source = api.request.get(task.get);
45+
if (task.load) source = api.fs.createReadStream(task.load);
46+
if (task.save) source.pipe(api.fs.createWriteStream(task.save));
47+
if (task.post) source.pipe(api.request.post(task.post));
48+
if (task.put) source.pipe(api.request.put(task.put));
4849
};
4950
}
50-
for (var i = 0; i < tasks.length; i++) {
51+
for (let i = 0; i < tasks.length; i++) {
5152
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
5253
}
5354
}

0 commit comments

Comments
 (0)
Please sign in to comment.