-
Notifications
You must be signed in to change notification settings - Fork 2
/
fmtzh.js
41 lines (33 loc) · 889 Bytes
/
fmtzh.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
var fs = require('fs')
var path = require('path')
function fmtZh(text) {
return text.replace(/([\u4e00-\u9fff])(\w)/g, '$1 $2')
.replace(/(\w)([\u4e00-\u9fff])/g, '$1 $2')
}
function processFile(fname) {
console.log(`file: ${fname}`)
var co = fs.readFileSync(fname, 'utf-8')
co = fmtZh(co)
fs.writeFileSync(fname, co)
}
function processDir(dir) {
var files = fs.readdirSync(dir)
for (var fname of files) {
fname = path.join(dir, fname)
if(!fs.statSync(fname).isFile())
continue
processFile(fname)
}
}
function main() {
var fname = process.argv[2]
if(!fname) {
console.log('请提供文件或目录。')
process.exit()
}
if(fs.statSync(fname).isDirectory())
processDir(fname)
else
processFile(fname)
}
if(module == require.main) main()