Skip to content

Commit f636a3c

Browse files
committed
APIAuto:StringUtil 新增 isBizName, isSchemaName, isTableName,完善 isBoolKey, isDateKey, isTimeKey
1 parent 17abd2d commit f636a3c

1 file changed

Lines changed: 127 additions & 11 deletions

File tree

  • APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/StringUtil.js

Lines changed: 127 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var StringUtil = {
6565
return s <= 0;
6666
}
6767

68-
if (trim) {
68+
if (trim && typeof s == 'string') {
6969
s = s.trim();
7070
}
7171
return s.length <= 0;
@@ -79,30 +79,82 @@ var StringUtil = {
7979
* @param s
8080
* @return
8181
*/
82-
isName(s) {
82+
isName: function(s) {
8383
return s != null && s.length > 0 && /[a-zA-Z_]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
8484
},
8585

8686
/**判断是否为代码名称,只能包含字母,数字或下划线
8787
* @param s
8888
* @return
8989
*/
90-
isBigName(s) {
90+
isBigName: function(s) {
9191
return s != null && s.length > 0 && /[A-Z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
9292
},
9393

9494
/**判断是否为代码名称,只能包含字母,数字或下划线
9595
* @param s
9696
* @return
9797
*/
98-
isSmallName(s) {
98+
isSmallName: function(s) {
9999
return s != null && s.length > 0 && /[a-z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
100100
},
101101

102-
isConstName(s) {
102+
isConstName: function(s) {
103103
return s != null && s.length > 0 && /[A-Z_]/.test(s.substring(0, 1)) && /^[0-9A-Z_]+$/.test(s);
104104
},
105105

106+
SYNTAX_NAMES: ['bool', 'boolean', 'int', 'integer', 'num', 'number', 'str', 'string', 'arr', 'array', 'obj', 'object'
107+
, 'type', 'class', 'fun', 'func', 'function', 'var', 'variable', 'val', 'value', 'const', 'is', 'as', 'id', 'index', 'unique'
108+
, 'from', 'join', 'on', 'at', 'in', 'out', 'where', 'by', 'having', 'limit', 'offset', 'find', 'list', 'get', 'set', 'put', 'remove'
109+
],
110+
COLUMN_NAMES: ['page', 'count', 'size', 'pagesize', 'pagenum', 'pageno', 'col', 'column', 'name', 'age', 'sex', 'gender', 'amount'
111+
, 'email', 'phone', 'tel', 'telephone', 'detail', 'describe', 'description', 'total', 'index', 'position', 'key', 'row', 'len', 'length', 'data'
112+
],
113+
114+
isBizName: function(s) {
115+
if (typeof s != 'string' || s.trim().length < 3) {
116+
return false;
117+
}
118+
if (StringUtil.isBigName(s) && || ! StringUtil.isConstName(s)) {
119+
return true;
120+
}
121+
if (! StringUtil.isName(s)) {
122+
return false;
123+
}
124+
125+
s = s.toLowerCase();
126+
var names = StringUtil.SYNTAX_NAMES || [];
127+
for (var i = 0; i < names.length; i ++) {
128+
if (s.endsWith(names[i])) {
129+
return false;
130+
}
131+
}
132+
133+
return true;
134+
},
135+
136+
isSchemaName: function(s) {
137+
return typeof s == 'string' && StringUtil.isBizName(s.replaceAll('-', ''));
138+
},
139+
isTableName: function(s) {
140+
if (StringUtil.isBigName(s) && || ! StringUtil.isConstName(s)) {
141+
return true;
142+
}
143+
if (! StringUtil.isBizName(s)) {
144+
return false;
145+
}
146+
147+
s = s.toLowerCase();
148+
var names = StringUtil.COLUMN_NAMES || [];
149+
for (var i = 0; i < names.length; i ++) {
150+
var n = names[i];
151+
if (s.endsWith(n)) {
152+
return false;
153+
}
154+
}
155+
156+
return true;
157+
},
106158

107159
/**添加后缀
108160
* @param key
@@ -467,8 +519,72 @@ var StringUtil = {
467519
return false;
468520
}
469521

470-
return ((key.startsWith('is') || key.startsWith('Is')) && /[a-z]/g.test(k) != true)
471-
|| (key.startsWith('IS') && /[A-Za-z]/g.test(k) != true);
522+
if ((key.startsWith('is') || key.startsWith('Is')) && /[a-z]/g.test(k) != true)
523+
|| (key.startsWith('IS') && /[A-Za-z]/g.test(k) != true) {
524+
return true;
525+
}
526+
527+
k = key.substring(3, 4);
528+
if (StringUtil.isEmpty(k, true)) {
529+
return false;
530+
}
531+
532+
if ((key.startsWith('has') || key.startsWith('Has')) && /[a-z]/g.test(k) != true)
533+
|| (key.startsWith('HAS') && /[A-Za-z]/g.test(k) != true) {
534+
return true;
535+
}
536+
537+
if ((key.startsWith('can') || key.startsWith('Can')) && /[a-z]/g.test(k) != true)
538+
|| (key.startsWith('CAN') && /[A-Za-z]/g.test(k) != true) {
539+
return true;
540+
}
541+
542+
k = key.substring(4, 5);
543+
if (StringUtil.isEmpty(k, true)) {
544+
return false;
545+
}
546+
547+
if ((key.startsWith('have') || key.startsWith('Have')) && /[a-z]/g.test(k) != true)
548+
|| (key.startsWith('HAVE') && /[A-Za-z]/g.test(k) != true) {
549+
return true;
550+
}
551+
552+
k = key.substring(5, 6);
553+
if (StringUtil.isEmpty(k, true)) {
554+
return false;
555+
}
556+
557+
if ((key.startsWith('shall') || key.startsWith('Shall')) && /[a-z]/g.test(k) != true)
558+
|| (key.startsWith('SHALL') && /[A-Za-z]/g.test(k) != true) {
559+
return true;
560+
}
561+
562+
k = key.substring(6, 7);
563+
if (StringUtil.isEmpty(k, true)) {
564+
return false;
565+
}
566+
567+
if ((key.startsWith('should') || key.startsWith('Should')) && /[a-z]/g.test(k) != true)
568+
|| (key.startsWith('SHOULD') && /[A-Za-z]/g.test(k) != true) {
569+
return true;
570+
}
571+
572+
if ((key.startsWith('enable') || key.startsWith('Enable')) && /[a-z]/g.test(k) != true)
573+
|| (key.startsWith('ENABLE') && /[A-Za-z]/g.test(k) != true) {
574+
return true;
575+
}
576+
577+
k = key.substring(7, 8);
578+
if (StringUtil.isEmpty(k, true)) {
579+
return false;
580+
}
581+
582+
if ((key.startsWith('disable') || key.startsWith('Disable')) && /[a-z]/g.test(k) != true)
583+
|| (key.startsWith('DISABLE') && /[A-Za-z]/g.test(k) != true) {
584+
return true;
585+
}
586+
587+
return false;
472588
},
473589
isIntKey: function (key) {
474590
return StringUtil.isKeyOfCategory(key, 'Int') || StringUtil.isKeyOfCategory(key, 'Integer');
@@ -612,10 +728,10 @@ var StringUtil = {
612728
return StringUtil.isKeyOfCategory(key, 'Uri');
613729
},
614730
isDateKey: function (key) {
615-
return StringUtil.isKeyOfCategory(key, 'Date');
731+
return ['createat', 'createdat', 'updateat', 'updatedat'].indexOf(StringUtil.get(key).toLowerCase()) >= 0 || StringUtil.isKeyOfCategory(key, 'Date');
616732
},
617733
isTimeKey: function (key) {
618-
return StringUtil.isKeyOfCategory(key, 'Time');
734+
return ['createat', 'createdat', 'updateat', 'updatedat'].indexOf(StringUtil.get(key).toLowerCase()) >= 0 || StringUtil.isKeyOfCategory(key, 'Time');
619735
},
620736
isKeyOfCategory: function (key, category) {
621737
if (StringUtil.isEmpty(key, true) || StringUtil.isEmpty(category, true) || key.length < category.length) {
@@ -667,14 +783,14 @@ var StringUtil = {
667783
return '';
668784
}
669785

670-
var json = parseJSON(s);
786+
var json = parseJSON(s) || {};
671787
var newStr = '';
672788
for (var k in json) {
673789
var v = json[k];
674790
if (v instanceof Object || v instanceof Array) {
675791
v = JSON.stringify(v);
676792
}
677-
newStr += '\n' + k + ': ' + (quote && typeof v == 'string' ? "'" + v.replaceAll("'", "\\'") + "'" : StringUtil.get(v));
793+
newStr += '\n' + k + ': ' + (quote && typeof v == 'string' ? "'" + v.replaceAll("'", "\\'") + "'" : StringUtil.trim(v));
678794
}
679795

680796
return StringUtil.trim(newStr);

0 commit comments

Comments
 (0)