-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.js
88 lines (81 loc) · 3.28 KB
/
format.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* 压缩文件的格式化
* @author liming
* @version 1.0.0
* @desc 下一个版本对解压缩函数进行优化包括=(){}等特殊符号旁边的空格
*/
(function (exports) {
const os = require("os")
"use strict";
/**
* @desc 解析对象
* @param { string } data
* @param { object } option keys[delMComment,delSComment,addSemicolon,compress]
*/
function BackAnalyze(data, option) {
if(!data && typeof data !== 'string'){
throw new Error('Cannot Analyze null and non-string')
}
this.curBig = 0; // 当前匹配大括号的个数
this.leftBigPos = 0; // 当前左大括号的位置
this.isfirstLeftBig = true; //前一个左大括号的位置
this.rightBigPos = 0; // 当前右大括号的位置
this.speicalBigSemicolon = /([\}])(\s)*(;)/gm; //特殊分号 位于中括号和大括号之后的
this.data = data;
this.output = ''; // 输出内容
this.option = Object.assign({delMComment:true,delSComment:true,addSemicolon:false,compress:true}, option || {})
}
/**
* 还原大括号
*/
BackAnalyze.prototype.backBig = function () {
this.leftBigPos = this.data.indexOf("{");
if(this.leftBigPos > -1 && this.data.indexOf("}") > this.leftBigPos){
this.curBig++;
this.output = this.output + this.data.slice(0,this.leftBigPos + 1) + os.EOL + this.repeatString("\t",this.curBig);
this.data = this.data.slice(this.leftBigPos + 1);
}
/** 当前仅当在一对{} 中间没有其他的{ 执行此函数 */
this.rightBigPos = this.data.indexOf("}");
if(this.rightBigPos > -1 && this.data.indexOf("{") > this.rightBigPos ){
--this.curBig;
this.output = this.output + this.data.slice(0,this.rightBigPos) + os.EOL + this.repeatString("\t",this.curBig) + "}" + os.EOL + this.repeatString("\t",this.curBig);
this.data = this.data.slice(this.rightBigPos + 1);
}else if(this.data.indexOf("{") == -1 && this.rightBigPos > -1 ){
--this.curBig;
this.output = this.output + this.data.slice(0,this.rightBigPos) + os.EOL + this.repeatString("\t",this.curBig) + "}" + os.EOL + this.repeatString("\t",this.curBig);
this.data = this.data.slice(this.rightBigPos + 1);
}
if(this.data.indexOf("}") > -1){
this.backBig();
}else{
this.output += this.data;
}
}
/**
* @desc 添加空行在非}之后的;后面
*/
BackAnalyze.prototype.addBlankLine = function (string,number) {
this.output = this.output.replace(this.speicalBigSemicolon,"$1$3"+os.EOL+"$2")
}
/**
* @desc 返回重复指定次数的字符串
* @param {String} string
* @param {Number} number
* @return {String} str 拼接之后的字符串
*/
BackAnalyze.prototype.repeatString = function (string,number) {
let str = "";
while (number){
str += string;
number--
}
return str;
}
BackAnalyze.prototype.format = function () {
this.backBig();
this.addBlankLine();
return this.output
}
exports.BackAnalyze = BackAnalyze;
}(typeof exports === 'object' && exports || this))