|
@@ -4,62 +4,88 @@
|
|
|
/**
|
|
|
* Created by Tony on 2017/6/21.
|
|
|
*/
|
|
|
-var test = require('tape');
|
|
|
+let test = require('tape');
|
|
|
+let strUtil = require('../../public/stringUtil');
|
|
|
|
|
|
-test('string test1', function(t){
|
|
|
- let str = "at('1.1') + at('1.2') + at('1.3') + at('1.4')";
|
|
|
- //let re = /at(/g;
|
|
|
- //let re = new RegExp("at\(", "g");
|
|
|
- //let str1 = str.replaceAll('re', '@(');
|
|
|
- //let str1 = str.replace('at(', '@(');
|
|
|
- var str1 = str.split('at1(').join('@(');
|
|
|
- //t.equal(str1, "@('1.1') + @('1.2') + @('1.3') + @('1.4')");
|
|
|
- t.equal(str1, "at('1.1') + at('1.2') + at('1.3') + at('1.4')");
|
|
|
- t.end();
|
|
|
-})
|
|
|
-
|
|
|
-test('string test2', function(t){
|
|
|
- var str="hello(world)";
|
|
|
- var nstr = str.replace(/\([^\)]*\)/g,"");
|
|
|
+// test('string test1', function(t){
|
|
|
+// let str = "at('1.1') + at('1.2') + at('1.3') + at('1.4')";
|
|
|
+// //let re = /at(/g;
|
|
|
+// //let re = new RegExp("at\(", "g");
|
|
|
+// //let str1 = str.replaceAll('re', '@(');
|
|
|
+// //let str1 = str.replace('at(', '@(');
|
|
|
+// var str1 = str.split('at1(').join('@(');
|
|
|
+// //t.equal(str1, "@('1.1') + @('1.2') + @('1.3') + @('1.4')");
|
|
|
+// t.equal(str1, "at('1.1') + at('1.2') + at('1.3') + at('1.4')");
|
|
|
+// t.end();
|
|
|
+// })
|
|
|
+//
|
|
|
+// test('string test2', function(t){
|
|
|
+// var str="hello(world)";
|
|
|
+// var nstr = str.replace(/\([^\)]*\)/g,"");
|
|
|
+//
|
|
|
+// t.equal(nstr , "hello");
|
|
|
+// t.end();
|
|
|
+// })
|
|
|
+//
|
|
|
+// test('string test3', function(t){
|
|
|
+// var str="共 (%S) 页";
|
|
|
+// var nstr = str.replace("(%S)",10);
|
|
|
+//
|
|
|
+// t.equal(nstr , "共 10 页");
|
|
|
+// t.end();
|
|
|
+// })
|
|
|
+//
|
|
|
+// test('test extract', function(t){
|
|
|
+// let private_extract_code = function(str, idx){
|
|
|
+// let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
|
|
|
+// for (let i = idx; i < str.length; i++) {
|
|
|
+// if (str[i] === '(') {
|
|
|
+// lBracket++;
|
|
|
+// if (lBracket == 1) firstIdx = i + 1;
|
|
|
+// }
|
|
|
+// if (str[i] === ')') {
|
|
|
+// rBracket++;
|
|
|
+// if (lBracket == rBracket) {
|
|
|
+// lastIdx = i - 1;
|
|
|
+// if (lastIdx > firstIdx) {
|
|
|
+// if (str[firstIdx] === "'") firstIdx++;
|
|
|
+// if (str[lastIdx] !== "'") lastIdx++;
|
|
|
+// if (lastIdx > firstIdx) {
|
|
|
+// rst = str.slice(firstIdx, lastIdx);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return rst;
|
|
|
+// };
|
|
|
+// let code = private_extract_code("at('1.1') + at('1.2')", 10);
|
|
|
+// t.equal(code , "1.2");
|
|
|
+// t.end();
|
|
|
+// })
|
|
|
|
|
|
- t.equal(nstr , "hello");
|
|
|
- t.end();
|
|
|
-})
|
|
|
+test('test number to Chinese', function(t){
|
|
|
+ t.equal(strUtil.convertToCaptionNum(0, false, false), '零');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(0, true, false), '零元整');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(0.68, true, false), '零元六角八分');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(0.68, true, true), '零元陆角捌分');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(10, false, false), '一十');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(10, false, true), '壹拾');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(11, false, false), '一十一');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(11, false, true), '壹拾壹');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(12, false, false), '一十二');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(12, false, true), '壹拾贰');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(21, false, false), '二十一');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(21, false, true), '贰拾壹');
|
|
|
|
|
|
-test('string test3', function(t){
|
|
|
- var str="共 (%S) 页";
|
|
|
- var nstr = str.replace("(%S)",10);
|
|
|
+ t.equal(strUtil.convertToCaptionNum(0.123456789, false, false), '零点一二三四五六七八九');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(10.123456789, false, false), '一十点一二三四五六七八九');
|
|
|
|
|
|
- t.equal(nstr , "共 10 页");
|
|
|
- t.end();
|
|
|
-})
|
|
|
+ t.equal(strUtil.convertToCaptionNum(123456789.15, false, false), '一亿二千三百四十五万六千七百八十九点一五');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(123456789.15, false, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖点壹伍');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(123456789.15, true, false), '一亿二千三百四十五万六千七百八十九元一角五分');
|
|
|
+ t.equal(strUtil.convertToCaptionNum(123456789.15, true, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元壹角伍分');
|
|
|
|
|
|
-test('test extract', function(t){
|
|
|
- let private_extract_code = function(str, idx){
|
|
|
- let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
|
|
|
- for (let i = idx; i < str.length; i++) {
|
|
|
- if (str[i] === '(') {
|
|
|
- lBracket++;
|
|
|
- if (lBracket == 1) firstIdx = i + 1;
|
|
|
- }
|
|
|
- if (str[i] === ')') {
|
|
|
- rBracket++;
|
|
|
- if (lBracket == rBracket) {
|
|
|
- lastIdx = i - 1;
|
|
|
- if (lastIdx > firstIdx) {
|
|
|
- if (str[firstIdx] === "'") firstIdx++;
|
|
|
- if (str[lastIdx] !== "'") lastIdx++;
|
|
|
- if (lastIdx > firstIdx) {
|
|
|
- rst = str.slice(firstIdx, lastIdx);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return rst;
|
|
|
- };
|
|
|
- let code = private_extract_code("at('1.1') + at('1.2')", 10);
|
|
|
- t.equal(code , "1.2");
|
|
|
t.end();
|
|
|
-})
|
|
|
+});
|