Skip to content

Commit 2e52a6d

Browse files
committed
Fix code style
1 parent 0ad80e8 commit 2e52a6d

File tree

8 files changed

+108
-84
lines changed

8 files changed

+108
-84
lines changed

JavaScript/2-level-up/1-metaprogramming.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const http = require('http');
55

66
// Parse duration to seconds
77
// Example: duration('1d 10h 7m 13s')
8-
98
const duration = s => {
109
if (typeof s === 'number') return s;
1110
let result = 0;
@@ -22,6 +21,15 @@ const duration = s => {
2221
}
2322
};
2423

24+
// Transports
25+
26+
// TODO: stub to be implemented
27+
const transport = {
28+
get: http.get,
29+
post: () => {},
30+
put: () => {},
31+
};
32+
2533
// Metadata
2634

2735
const tasks = [
@@ -53,14 +61,14 @@ const iterate = tasks => {
5361
const closureTask = task => () => {
5462
console.dir(task);
5563
let source;
56-
if (task.get) source = http.get(task.get);
64+
if (task.get) source = transport.get(task.get);
5765
if (task.load) source = fs.createReadStream(task.load);
5866
if (task.save) source.pipe(fs.createWriteStream(task.save));
59-
if (task.post) source.pipe(http.post(task.post));
60-
if (task.put) source.pipe(http.put(task.put));
67+
if (task.post) source.pipe(transport.post(task.post));
68+
if (task.put) source.pipe(transport.put(task.put));
6169
};
62-
for (let i = 0; i < tasks.length; i++) {
63-
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
70+
for (const task of tasks) {
71+
setInterval(closureTask(task), duration(task.interval));
6472
}
6573
};
6674

JavaScript/2-level-up/2-metameta.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const DURATION_UNITS = {
1313
};
1414

1515
// Parse duration to seconds
16-
// s - string, duration syntax
17-
// Returns: number, milliseconds
16+
// s <string> duration syntax
17+
// Returns: <number> milliseconds
1818
// Example: duration('1d 10h 7m 13s')
1919
const duration = s => {
2020
if (typeof s === 'number') return s;
@@ -61,13 +61,13 @@ const iterate = tasks => {
6161

6262
// Configuration metadata
6363
const sources = {
64-
get: request.get,
64+
get: request.get,
6565
load: fs.createReadStream
6666
};
6767
const destinations = {
6868
save: fs.createWriteStream,
6969
post: request.post,
70-
put: request.put
70+
put: request.put
7171
};
7272

7373
// Abcstract logic
@@ -82,8 +82,8 @@ const iterate = tasks => {
8282
}
8383
};
8484

85-
for (let i = 0; i < tasks.length; i++) {
86-
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
85+
for (const task of tasks) {
86+
setInterval(closureTask(task), duration(task.interval));
8787
}
8888
};
8989

JavaScript/2-level-up/3-optimized.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const DURATION_UNITS = {
1313
};
1414

1515
// Parse duration to seconds
16-
// s - string, duration syntax
17-
// Returns: number, milliseconds
16+
// s <string> duration syntax
17+
// Returns: <number> milliseconds
1818
// Example: duration('1d 10h 7m 13s')
1919
const duration = s => {
2020
if (typeof s === 'number') return s;
@@ -61,13 +61,13 @@ const iterate = tasks => {
6161

6262
// Configuration metadata
6363
const sources = {
64-
get: request.get,
64+
get: request.get,
6565
load: fs.createReadStream
6666
};
6767
const destinations = {
6868
save: fs.createWriteStream,
6969
post: request.post,
70-
put: request.put
70+
put: request.put
7171
};
7272

7373
// Abcstract logic
@@ -87,8 +87,7 @@ const iterate = tasks => {
8787
}
8888
};
8989

90-
for (let i = 0; i < tasks.length; i++) {
91-
const task = tasks[i];
90+
for (const task of tasks) {
9291
const interval = duration(task.interval);
9392
setInterval(closureTask(task), interval);
9493
}

JavaScript/2-level-up/4-metedata.jstp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
save: 'file3.json' },
1616
],
1717
error: (err, request) => console.log('Error'),
18-
success: (err, request) => counters.success++
18+
success: (err, request) => counters.success++,
1919
}

JavaScript/2-level-up/file1.json

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
{"statusCode":404}

JavaScript/3-introspection/wcl.js

+58-48
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
1-
(function(wcl) {
1+
'use strict';
2+
3+
(wcl => {
24

35
wcl.AjaxAPI = function(methods) { // params: { method: { get/post:url }, ... }
4-
var api = {};
6+
const api = {};
57
api.request = function(apiMethod, params, callback) {
6-
var err = null, requestParams = this.methods[apiMethod];
8+
let err = null, requestParams = this.methods[apiMethod];
79
if (requestParams) {
8-
var httpMethod, url;
9-
if (requestParams.get ) { httpMethod = 'GET'; url = requestParams.get; }
10-
if (requestParams.post) { httpMethod = 'POST'; url = requestParams.post; }
10+
let httpMethod, url;
11+
if (requestParams.get) {
12+
httpMethod = 'GET';
13+
url = requestParams.get;
14+
}
15+
if (requestParams.post) {
16+
httpMethod = 'POST';
17+
url = requestParams.post;
18+
}
1119
if (httpMethod) {
1220
wcl.request(httpMethod, url, params, true, callback);
1321
return;
14-
} else err = new Error('DataSource error: HTTP method is not specified');
15-
} else err = new Error('DataSource error: AJAX method is not specified');
22+
} else {
23+
err = new Error('DataSource error: HTTP method is not specified');
24+
}
25+
} else {
26+
err = new Error('DataSource error: AJAX method is not specified');
27+
}
1628
callback(err, null);
1729
};
1830
api.init = function(methods) {
1931
api.methods = methods;
2032
for (var method in api.methods) {
2133
(function() {
22-
var apiMethod = method;
23-
if (apiMethod === 'introspect') api[apiMethod] = function(params, callback) {
24-
api.request(apiMethod, params, function(err, data) {
25-
api.init(data);
26-
callback(err, data);
27-
});
28-
}; else api[apiMethod] = function(params, callback) {
29-
api.request(apiMethod, params, callback);
30-
};
31-
} ());
34+
const apiMethod = method;
35+
if (apiMethod === 'introspect') {
36+
api[apiMethod] = function(params, callback) {
37+
api.request(apiMethod, params, (err, data) => {
38+
api.init(data);
39+
callback(err, data);
40+
});
41+
};
42+
} else {
43+
api[apiMethod] = (params, callback) => {
44+
api.request(apiMethod, params, callback);
45+
};
46+
}
47+
}());
3248
}
3349
};
3450
api.init(methods);
@@ -49,9 +65,9 @@
4965
};
5066

5167
wcl.AjaxDataSource = function(methods) {
52-
var ds = wcl.AjaxAPI(methods);
68+
const ds = wcl.AjaxAPI(methods);
5369
ds.read = function(query, callback) {
54-
ds.request('read', query, function(err, data) {
70+
ds.request('read', query, (err, data) => {
5571
// TODO: autocreate Record
5672
// callback(err, wcl.Record({ data:data }));
5773
//
@@ -62,72 +78,68 @@
6278
};
6379

6480
wcl.MemoryDataSource = function(params) { // { data:Hash, metadata:Hash }
65-
var ds = {};
81+
const ds = {};
6682
ds.data = params.data;
6783
ds.metadata = params.metadata;
6884
ds.each = function(params, callback) {
69-
for (var i = 0; i < ds.data.length; i++) {
70-
var d = ds.data[i], match = true;
71-
for (var key in params) match = match && (d[key] === params[key]);
85+
for (let i = 0; i < ds.data.length; i++) {
86+
let d = ds.data[i], match = true;
87+
for (const key in params) match = match && (d[key] === params[key]);
7288
if (match) { if (callback(i)) return; }
7389
}
7490
};
7591
ds.read = function(params, callback) {
76-
var data = ds.data;
77-
ds.each(params, function(key) { callback(null, data[key]); return true; });
92+
const data = ds.data;
93+
ds.each(params, key => { callback(null, data[key]); return true; });
7894
callback(new Error('Record not found'), null);
7995
};
8096
ds.insert = function(params, callback) {
8197
ds.data.push(params);
8298
callback();
8399
};
84100
ds.update = function(params, callback) {
85-
var data = ds.data;
86-
ds.each(params, function(key) { data[key] = params; return true; });
101+
const data = ds.data;
102+
ds.each(params, key => { data[key] = params; return true; });
87103
callback();
88104
};
89105
ds.delete = function(params, callback) {
90-
var data = ds.data;
91-
ds.each(params, function(key) { delete data[key]; });
106+
const data = ds.data;
107+
ds.each(params, key => { delete data[key]; });
92108
callback();
93109
};
94110
ds.find = function(params, callback) {
95-
var data = ds.data, result = [];
96-
ds.each(params, function(key) { result.push(data[key]); });
111+
let data = ds.data, result = [];
112+
ds.each(params, key => { result.push(data[key]); });
97113
callback(null, result);
98114
};
99115
return ds;
100116
};
101117

102118
wcl.parse = function(json) {
103-
var result;
119+
let result;
104120
eval('result = new Object(' + json + ')');
105121
return result;
106122
};
107123

108124
wcl.htmlEscape = function(content) {
109-
return content.replace(/[&<>"'\/]/g, function(char) {
110-
return ({ "&":"&amp;","<":"&lt;", ">":"&gt;", '"':"&quot;", "'":"&#39;" }[char]);
111-
});
125+
return content.replace(/[&<>"'\/]/g, char => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#39;' }[char]));
112126
};
113127

114128
wcl.template = function(tpl, data, escapeHtml) {
115-
return tpl.replace(/@([\-\.0-9a-zA-Z]+)@/g, function(s, key) {
116-
return escapeHtml ? wcl.htmlEscape(data[key]) : data[key];
117-
});
129+
return tpl.replace(/@([\-\.0-9a-zA-Z]+)@/g, (s, key) => (escapeHtml ? wcl.htmlEscape(data[key]) : data[key]));
118130
};
119131

120132
wcl.templateHtml = function(tpl, data) {
121133
return wcl.template(tpl, data, true);
122134
};
123135

124136
wcl.request = function(method, url, params, parseResponse, callback) {
125-
var req = new XMLHttpRequest(), data = [], value = '';
137+
let req = new XMLHttpRequest(), data = [], value = '';
126138
req.open(method, url, true);
127-
for (var key in params) {
139+
for (const key in params) {
128140
if (!params.hasOwnProperty(key)) continue;
129141
value = params[key];
130-
if (typeof(value) != 'string') value = JSON.stringify(value);
142+
if (typeof(value) !== 'string') value = JSON.stringify(value);
131143
data.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
132144
}
133145
data = data.join('&');
@@ -136,18 +148,16 @@
136148
req.setRequestHeader('Connection', 'close');
137149
req.onreadystatechange = function() {
138150
if (req.readyState === 4) {
139-
var err = null, res = req.responseText;
151+
let err = null, res = req.responseText;
140152
if (req.status === 0 || req.status === 200) {
141153
if (parseResponse) {
142-
try { res = JSON.parse(res); }
143-
catch(e) { err = new Error('JSON parse code: ' + e); }
154+
try { res = JSON.parse(res); } catch (e) { err = new Error('JSON parse code: ' + e); }
144155
}
145156
} else err = new Error('HTTP error code: ' + req.status);
146157
callback(err, res);
147158
}
148159
};
149-
try { req.send(data); }
150-
catch(e) { }
160+
try { req.send(data); } catch (e) { }
151161
};
152162

153163
wcl.get = function(url, params, callback) {
@@ -158,4 +168,4 @@
158168
wcl.request('POST', url, params, true, callback);
159169
};
160170

161-
} (global.wcl = global.wcl || {}));
171+
}(global.wcl = global.wcl || {}));

JavaScript/4-class-factory/class-inheritance-metaprogramming.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
// Base class
44

5-
function Ground(area) {
6-
this.area = area;
7-
}
5+
class Ground
6+
constructor(area) {
7+
this.area = area;
8+
}
89

9-
Ground.prototype.calculateCost = function(price) {
10-
return this.area * price;
11-
};
10+
calculateCost(price) {
11+
return this.area * price;
12+
}
13+
}
1214

1315
// MetaFactory to build descendant classes
1416

@@ -34,12 +36,14 @@ const LandOwnership = inheritGround({
3436
type: 'ownership',
3537
// Add method to descendant class prototype
3638
toString() {
37-
return this.category + ' ' + this.type + ' / ' + this.area;
39+
const { category, type, area } = this;
40+
return `${category} ${type} / ${area}`;
3841
}
3942
});
4043

4144
// Create and use instance
4245

4346
const land = new LandOwnership(50);
4447
console.dir(land);
45-
console.log('Cost is: ' + land.calculateCost(7) + ' for ' + land.toString());
48+
const cost = land.calculateCost(7);
49+
console.log(`Cost is: ${cost} for ${land.toString()}`);

0 commit comments

Comments
 (0)