UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fileUtil = require("./tool/FileUtil");
4var fs = require("fs");
5var path = require("path");
6var JsonYS = /** @class */ (function () {
7 function JsonYS(args) {
8 this._fixed = 2;
9 var self = this;
10 var baseUrl;
11 var fixed;
12 if (args[1]) {
13 if (isNaN(args[1])) {
14 baseUrl = args[1];
15 fixed = Number(args[2]) || 2;
16 }
17 else {
18 baseUrl = process.cwd();
19 fixed = Number(args[1]) || 2;
20 }
21 }
22 else {
23 baseUrl = process.cwd();
24 fixed = 2;
25 }
26 self._fixed = fixed;
27 fileUtil.FileUtil.walkDir(baseUrl, self.onFile, null, self);
28 }
29 JsonYS.prototype.onFile = function (url) {
30 var self = this;
31 if (path.extname(url) != ".json")
32 return;
33 var data = JSON.parse(fs.readFileSync(url, 'utf-8'));
34 self.transNumJson(data);
35 fs.writeFileSync(url, JSON.stringify(data));
36 };
37 JsonYS.prototype.transNumJson = function (data) {
38 for (var key in data) {
39 if (data[key] instanceof Object || data[key] instanceof Array) {
40 this.transNumJson(data[key]);
41 }
42 else {
43 if (typeof (data[key]) == 'number') {
44 var bs = Math.pow(10, this._fixed);
45 data[key] = Math.round(data[key] * bs) / bs;
46 }
47 }
48 }
49 };
50 return JsonYS;
51}());
52function run(args) {
53 new JsonYS(args);
54}
55exports.run = run;