Skip to content

Commit 1afa2bd

Browse files
committed
chore: use array destructure with splite
1 parent 5b6c3da commit 1afa2bd

File tree

4 files changed

+10
-31
lines changed

4 files changed

+10
-31
lines changed

06-buffer/03-decode.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ if(process.argv.length !== 3) {
77

88
// method A
99
const buf = atob(process.argv[2]);
10-
const info = buf.split(':');
10+
const [userName, pwd] = buf.split(':');
1111

1212
// method B
1313
/*
1414
const buf = Buffer.from(process.argv[2], 'base64');
15-
const info = buf.toString('utf8').split(':');
15+
const [userName, pwd] = buf.toString('utf8').split(':');
1616
*/
1717

18-
if(info.length !== 2) {
19-
console.error('信息有误!');
20-
process.exit(2);
21-
}
22-
23-
console.log('user name: %s\npassword: %s', info[0], info[1]);
18+
console.log(`userName: ${userName}\npassword: ${pwd}`);

10-file-system/27-passwd-fp.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
const fs = require('fs');
44

55
function line2obj(line) {
6-
const arr = line.split(':');
7-
return {
8-
user_name: arr[0],
9-
user_id: arr[2],
10-
group_id: arr[3],
11-
home_dir: arr[5],
12-
login_shell: arr[6],
13-
};
6+
const [user_name,, user_id, group_id,, home_dir, login_shell] = line.split(':');
7+
return {user_name, user_id, group_id, home_dir, login_shell};
148
}
159

1610
let lines = fs.readFileSync('/etc/passwd', 'utf8');

10-file-system/27-passwd.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ const data = [];
66
let lines = fs.readFileSync('/etc/passwd', 'utf8');
77
lines = lines.split('\n');
88
lines.forEach(line => {
9-
const arr = line.split(':');
10-
const obj = {
11-
user_name: arr[0],
12-
user_id: arr[2],
13-
group_id: arr[3],
14-
home_dir: arr[5],
15-
login_shell: arr[6],
16-
};
17-
data.push(obj);
9+
const [user_name,, user_id, group_id,, home_dir, login_shell] = line.split(':');
10+
data.push({user_name, user_id, group_id, home_dir, login_shell});
1811
});
1912

2013
console.table(data);

17-http-server/03-req-header-parse.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ http.createServer((req, res) =>{
1515

1616
log('authorization:', req.headers.authorization);
1717

18-
let auth = req.headers.authorization;
18+
const auth = req.headers.authorization;
1919

2020
if(typeof auth !== 'undefined') {
21-
auth = auth.split(' ');
22-
if(auth[0] === 'Basic') {
23-
let buf = Buffer.from(auth[1], 'base64');
24-
log('username & password:', buf.toString('utf8'));
25-
}
21+
const [type, user] = auth.split(' ');
22+
if(type === 'Basic') log('username:password:', atob(user));
2623
}
2724

2825
res.end('OK!');

0 commit comments

Comments
 (0)