-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hyvi
committed
Nov 16, 2012
1 parent
b5bd59e
commit d491c8d
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters