Skip to content

Commit

Permalink
增加doc文档的字数检查
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyvi committed Nov 16, 2012
1 parent b5bd59e commit d491c8d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
61 changes: 61 additions & 0 deletions lib/checklist/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
var fs = require("fs");
var path = require("path");
var lineReader = require('line-reader');
/**
* 规则描述
*/
exports.rule = "文档。doc目录下每个文档加5分。每个文档去空格后,不应少于500字。";
/**
* 检查项
* @param {String} source 检查的目录
* @param {Function} callback 返回数据的回调函数
*/
exports.check = function (source, callback) {
// 你的实现
// 不对你用同步或异步方法做任何限制,但是为了兼容两种情况,结果请用callback传递返回
var result = {score : 0, info : ''},
err,
docpath = path.join(source,'doc'),
isExistDoc = fs.existsSync(docpath);
if (isExistDoc) {
// 遍历doc目录下文件
var processDir = function(tmp_path){
var filelist = fs.readdirSync(tmp_path);
for(var i =0, len = filelist.length; i < len; i++){
var filepath = path.join(tmp_path,filelist[i]),
stat = fs.lstatSync(filepath);
if(stat.isFile()){
(function(){
var sum = 0, filename = filepath;
lineReader.eachLine(filename,function(line,last){
// 对于特定格式的文档,无法统计字数, 比如doc。
if( line.replace(' ','').length + sum > 500 ){
result.score += 5;
return false; // stop reading.
}
if(last && sum < 500){
result.info += 'doc ' + filename + ' number of words < 500';
}
});
}());
}else if (stat.isDirectory()){
processDir(filepath);
}else{
return;
}
}
}
processDir(docpath);
} else {
result = {score : 0, info : 'doc folder not exist!'};
}

if (err) {
callback(err);
} else {
// 返回的结果包含两个属性。分数和纠错信息
// result = {score: 10, info: somereason};
callback(null, result);
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
],
"dependencies": {
"commander": "*",
"eventproxy": ">=0.1.4"
"eventproxy": ">=0.1.4",
"line-reader" : ">=0.2.3"
},
"author": "Jackson Tian",
"license": "MIT"
Expand Down

0 comments on commit d491c8d

Please sign in to comment.