ECMAScript 6的代码有多么舒畅
这是我的一个项目的源码, 我会在2个月内把ta实际部署在服务器上.
来看看ECMAScript 6带来的革命性设计.
import * as userDB from '../database/user';
import * as session from '../memory/session';
import * as recoveryMM from '../memory/recovery';
import * as validate from './validate';
import * as cparser from '../cookie-parser';
const TIME_SESSION = 864000;
const ssx = 'sid={sid}; max-age={max-age}; path=/; secure; HttpOnly';
function escapeInput(value) {
return typeof value === 'string'
? value.trim()
: '';
}
export function* isUser(ynext, next, req, res) {
// Not exists session
if (!req.cookies.sid) {
res.writeHead(302, { 'location':'/login' });
res.end();
return;
}
let [err, user] = yield session.find(req.cookies.sid, ynext);
// Redis-server error
if (err) {
res.writeHead(500);
res.end();
return;
}
// Not exists session
if (!user) {
res.writeHead(302, { 'location':'/login' });
res.end();
return;
}
let [errExpire] = yield session.expire(req.cookies.sid, user.username, ynext);
// Redis-server error
if (errExpire) {
}
// Config session
req.session = user;
req.session.sid = req.cookies.sid;
yield* next;
}
function toDateTime(time) {
let y = time.getFullYear();
let m = time.getMonth() + 1;
let d = time.getDate();
let h = time.getHours();
let mi = time.getMinutes();
let s = time.getSeconds();
return `${y}-${m}-${d} ${h}:${mi}:${s}`;
}
...
import * as fansDB from '../../lib/database/fans';
import {pool} from '../../lib/database/init';
import {scc, mcc, describe} from 'rocore';
import assert from 'assert';
import fs from 'fs';
import path from 'path';
let {ok, strictEqual} = assert;
scc(function* (ynext) {
describe('Rebuilding database ...');
yield pool.query(fs.readFileSync(path.join(__dirname, '../../lib/database/create.sql'),
{encoding:'utf-8'}),
ynext);
var [err, affected] = yield fansDB.insert(
1,
2,
ynext
);
ok(affected);
describe('Insert a fans', 'fansingId=1 fansId=2', affected);
var [err, affected] = yield fansDB.remove(
1,
2,
ynext
);
ok(affected);
describe('Remove a fans', 'fansingId=1 fansId=2', affected);
process.exit(0);
});
king@king-pc:~/box$ node trace/test
→ Rebuilding database ...
→ Insert a fans fansingId=1 fansId=2
true
→ Remove a fans fansingId=1 fansId=2
true
king@king-pc:~/box$
export let insert = `
insert into tasks
set
pubberId = ?,
keyword = ?,
random = floor(rand() * 10e16),
pubdate = now()
`;
export let findState = `
select
pubberId,
locked,
finished
from tasks
where taskId = ?
`;
export let updateToLocked = `
start transaction;
update tasks
set
locked = 'Y'
where taskId = ?;
insert into lockedTasks
set
taskId = ?,
lockerId = ?,
lockdate = now();
commit;
`;
Rust lang代码体验…
let arr = [1i, 2i, 3i];
for x in arr.iter() {
println!("{}", x); // 1 2 3
}
let x:i32 = 10;
let y = if x == 10 {100} else {101}; // y = 100
let x = 100i;
match x {
100i => println!("100"),
101i => println!("101"),
102i => println!("102"),
}
fn my(x:i32, y:i32) -> i32 {
let z = x + y;
z + 1
}
fn main() {
let z = my(100i32, 200i32); // => 301
}
Rust 中变量默认是不可变得, 符合函数式思想: let x:int = 1;
但是可以设置为可变变量: let mut x:int = 1;
x:int x:i32 x:i64 代表整形, 32位整形, 64位整形.