forked from Vieira-zj/zj_macaca_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld.js
223 lines (189 loc) · 5.33 KB
/
helloworld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Hello world demo.
*
*/
'use strict';
let testPrintVar = function (name = 'zhengjin') {
console.log(`hello world, ${name}`);
};
let testPath = function () {
const path = require('path');
// path.join concatenates all given path segments together
// using the platform specific separator as a delimiter, then normalizes the resulting path.
console.log('path.join:', path.join(__dirname, '../logs'));
// path.resolve() process the sequence of paths from right to left,
// with each subsequent path prepended until an absolute path is constructed.
console.log('path.resolve:', path.resolve(__dirname, '../logs', '/bar/bae'));
};
let testObject = function testObject() {
// #1
let tmpBoolean = true;
let testObject = {
testBoolean1: tmpBoolean ? 'pass' : 'failed',
testBoolean2: function () {
let ret = tmpBoolean ? 'pass' : 'failed';
return ret.toUpperCase();
}
}
console.log('Object boolean1 value:', testObject.testBoolean1);
console.log('Object boolean2 value:', testObject.testBoolean2());
// #2
function Student(name, age) {
this.name = name
this.age = age
this.getName = function () {
return this.name
}
}
Student.prototype.sayHello = function () {
console.log(`Hello, my name is ${this.name}, I am ${this.age} years old.`)
}
let s = new Student('Henry', 21)
console.log('name:', s.getName())
s.sayHello()
};
let testFnName = function (fn) {
console.log('funcion name:', fn.name);
console.log('funcion name:', arguments[0].name);
};
let fnCaller = function (fn) {
console.log('hello', fn());
};
let fnCallBack1 = function (text) {
return text;
};
let testCallBack = function () {
// must be within the same context
let fnCallBack2 = function () {
return nameText;
};
const nameText = 'zhengjin';
// #1, call by anonymous function
fnCaller(function () {
return nameText;
});
// #2, call by function
fnCaller(() => fnCallBack1(nameText));
// #3, call by function variable
fnCaller(fnCallBack2);
};
let testJsonLoad = function () {
// auto convert json string (in file) to json object
let loadJson = require('./package.json');
console.log('load json:', typeof loadJson);
console.log('project description:', loadJson.description);
};
let testFnParams = function () {
let tmpFn = function (firstName, lastName) {
if (lastName) {
console.log('hello', firstName, lastName);
return;
}
console.log('hello', firstName);
}
tmpFn('henry');
tmpFn('zheng', 'jin');
};
let testAddFn = function () {
let tmpObj = {
name: 'zheng jin',
title: 'tester'
}
tmpObj['toMessage'] = function () {
console.log(`Message: ${this.name}'s title is ${this.title}`);
}
tmpObj.toMessage();
};
let testObjectDestruct = function () {
// matched by the property name in object
// example 01, pass as arguments
let printFullName = function ({
firstName,
lastName
}) {
console.log(`hello, ${firstName} ${lastName}`);
};
let tmpName = {
national: 'China',
firstName: 'zheng',
lastName: 'jin',
age: 30
};
printFullName(tmpName);
// example 02, as return
let getUserInfo = function () {
return {
national: 'China',
firstName: 'zheng',
lastName: 'jin',
age: 30
};
};
const {
firstName,
lastName
} = getUserInfo();
console.log('user name: ' + firstName + ' ' + lastName);
};
let testArrayDestruct = function () {
// match the order in array
let tmpArr = ['JS', 'Python', 'Java'];
const [first, second] = tmpArr;
console.log(`program: ${first}, ${second}`);
};
let testArgsJoin = function () {
let argsJoin = function (...args) {
console.log(args.join(' '));
};
argsJoin('hello', 'world', 'zheng', 'jin');
};
let testSelfRunFun = function () {
(function () {
console.log('self run function without name.');
})();
(function selfRun() {
console.log('self run function with name.');
})();
};
let testGetTcNameByRegExp1 = function () {
const tmpStr = `Log 2) [Smoke test] [SE-02-Settings-Domain]:
3) [CI Automation] [MP-01-001-Add Multiple Pages]:
4) Editor-Basic:
Job succeeded`;
const reg = /\d\)\s\[.+:/g;
let m = tmpStr.match(reg);
for (let tc of m) {
console.log(tc);
}
}
let testGetTcNameByRegExp2 = function () {
const fs = require('fs');
fs.readFile('./testdata/runlog_1020.log', function (err, data) {
if (err) {
console.error(err);
return;
}
const reg = /\d\)\s\[.+:/g;
let m = data.toString().match(reg);
for (let tc of m) {
console.log(tc);
}
});
}
if (require.main === module) {
// testPrintVar();
// testPrintVar('henry');
// testPath();
// testObject();
// testFnName(testPath);
// testCallBack();
// testJsonLoad();
// testFnParams();
// testAddFn();
// testObjectDestruct();
// testArrayDestruct();
// testArgsJoin();
// testSelfRunFun();
// testGetTcNameByRegExp1();
// testGetTcNameByRegExp2();
}