MaiXinRong пре 5 година
родитељ
комит
3c57d3f5ab
2 измењених фајлова са 14 додато и 4 уклоњено
  1. 4 3
      app/extend/helper.js
  2. 10 1
      test/app/extend/helper.test.js

+ 4 - 3
app/extend/helper.js

@@ -427,9 +427,10 @@ module.exports = {
     getChapterCode(code, symbol = '-') {
     getChapterCode(code, symbol = '-') {
         if (!code || code === '') return '';
         if (!code || code === '') return '';
         const codePath = code.split(symbol);
         const codePath = code.split(symbol);
-        const reg = /[0-9]*$/;
-        if (reg.test(codePath[0])) {
-            const num = parseInt(codePath[0]);
+        const reg = /[0-9]{3,4}$/;
+        const result = codePath[0].match(reg);
+        if (result) {
+            const num = parseInt(result[0]);
             return this.mul(this.div(num, 100, 0), 100) + '';
             return this.mul(this.div(num, 100, 0), 100) + '';
         } else {
         } else {
             return '10000';
             return '10000';

+ 10 - 1
test/app/extend/helper.test.js

@@ -142,5 +142,14 @@ describe('test/app/extend/helper.test.js', () => {
         const id = ['abc', 'efj'];
         const id = ['abc', 'efj'];
         const str = 'And id in (' + ctx.helper.getInArrStrSqlFilter(id) + ')';
         const str = 'And id in (' + ctx.helper.getInArrStrSqlFilter(id) + ')';
         assert(str === "And id in ('abc','efj')");
         assert(str === "And id in ('abc','efj')");
-    })
+    });
+
+    it('test getChapterCode', function () {
+        const ctx = app.mockContext();
+        const testData = ['A101-1-a', 'A201B102-1', '新增203-1', '新增1001-1'];
+        const targetData = ['100', '100', '200', '1000'];
+        for (const i in testData) {
+            assert(ctx.helper.getChapterCode(testData[i]) === targetData[i]);
+        }
+    });
 });
 });