UNPKG

1.66 kBPlain TextView Raw
1import fileUtil = require("./tool/FileUtil");
2import fs = require("fs");
3import path = require("path");
4
5class JsonYS {
6 private _fixed = 2;
7 constructor(args:any[]) {
8 let self = this;
9 let baseUrl:string;
10 let fixed:number;
11 if(args[1])
12 {
13 if(isNaN(args[1]))
14 {
15 baseUrl = args[1];
16 fixed = Number(args[2]) || 2;
17 }
18 else
19 {
20 baseUrl = process.cwd();
21 fixed = Number(args[1]) || 2;
22 }
23 }
24 else
25 {
26 baseUrl = process.cwd();
27 fixed = 2;
28 }
29 self._fixed = fixed;
30 fileUtil.FileUtil.walkDir(baseUrl, self.onFile, null, self);
31 }
32
33 private onFile(url: string): void {
34 let self = this;
35 if (path.extname(url) != ".json") return;
36 let data = JSON.parse(fs.readFileSync(url, 'utf-8'));
37 self.transNumJson(data);
38 fs.writeFileSync(url, JSON.stringify(data));
39 }
40
41 private transNumJson(data:any):void{
42 for(let key in data)
43 {
44 if(data[key] instanceof Object || data[key] instanceof Array)
45 {
46 this.transNumJson(data[key]);
47 }
48 else
49 {
50 if(typeof(data[key]) == 'number')
51 {
52 let bs = Math.pow(10,this._fixed);
53 data[key] = Math.round(data[key] * bs) / bs;
54 }
55 }
56 }
57 }
58}
59
60export function run(args:any[]): void {
61 new JsonYS(args);
62}
\No newline at end of file