stringTest.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Created by Tony on 2017/6/21.
  3. */
  4. /**
  5. * Created by Tony on 2017/6/21.
  6. */
  7. let test = require('tape');
  8. let strUtil = require('../../public/stringUtil');
  9. // test('string test1', function(t){
  10. // let str = "at('1.1') + at('1.2') + at('1.3') + at('1.4')";
  11. // //let re = /at(/g;
  12. // //let re = new RegExp("at\(", "g");
  13. // //let str1 = str.replaceAll('re', '@(');
  14. // //let str1 = str.replace('at(', '@(');
  15. // var str1 = str.split('at1(').join('@(');
  16. // //t.equal(str1, "@('1.1') + @('1.2') + @('1.3') + @('1.4')");
  17. // t.equal(str1, "at('1.1') + at('1.2') + at('1.3') + at('1.4')");
  18. // t.end();
  19. // })
  20. // test('string test1', function(t){
  21. // let str = "abc|def";
  22. // // let str1 = str.replace('|', '\n');
  23. // // let str1 = strUtil.replaceAll(str, "|", "\n\r");
  24. // // let str1 = strUtil.replaceAll(str, "\|", "+");
  25. // let str1 = str.split('|').join('\n\r');
  26. // console.log(str1);
  27. // //t.equal(str1, "@('1.1') + @('1.2') + @('1.3') + @('1.4')");
  28. // // t.equal(str1, "at('1.1') + at('1.2') + at('1.3') + at('1.4')");
  29. // t.end();
  30. // })
  31. test('string ', function(t){
  32. let str = "{abc}";
  33. console.log(str);
  34. console.log(str.slice(1, -1));
  35. t.end();
  36. })
  37. //
  38. // test('string test2', function(t){
  39. // var str="hello(world)";
  40. // var nstr = str.replace(/\([^\)]*\)/g,"");
  41. //
  42. // t.equal(nstr , "hello");
  43. // t.end();
  44. // })
  45. //
  46. // test('string test3', function(t){
  47. // var str="共 (%S) 页";
  48. // var nstr = str.replace("(%S)",10);
  49. //
  50. // t.equal(nstr , "共 10 页");
  51. // t.end();
  52. // })
  53. //
  54. // test('test extract', function(t){
  55. // let private_extract_code = function(str, idx){
  56. // let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  57. // for (let i = idx; i < str.length; i++) {
  58. // if (str[i] === '(') {
  59. // lBracket++;
  60. // if (lBracket == 1) firstIdx = i + 1;
  61. // }
  62. // if (str[i] === ')') {
  63. // rBracket++;
  64. // if (lBracket == rBracket) {
  65. // lastIdx = i - 1;
  66. // if (lastIdx > firstIdx) {
  67. // if (str[firstIdx] === "'") firstIdx++;
  68. // if (str[lastIdx] !== "'") lastIdx++;
  69. // if (lastIdx > firstIdx) {
  70. // rst = str.slice(firstIdx, lastIdx);
  71. // }
  72. // }
  73. // break;
  74. // }
  75. // }
  76. // }
  77. // return rst;
  78. // };
  79. // let code = private_extract_code("at('1.1') + at('1.2')", 10);
  80. // t.equal(code , "1.2");
  81. // t.end();
  82. // })
  83. // test('test number to Chinese', function(t){
  84. // t.equal(strUtil.convertToCaptionNum(0, false, false), '零');
  85. // t.equal(strUtil.convertToCaptionNum(0, true, false), '零元整');
  86. // t.equal(strUtil.convertToCaptionNum(0.68, true, false), '零元六角八分');
  87. // t.equal(strUtil.convertToCaptionNum(0.68, true, true), '零元陆角捌分');
  88. // t.equal(strUtil.convertToCaptionNum(10, false, false), '一十');
  89. // t.equal(strUtil.convertToCaptionNum(10, false, true), '壹拾');
  90. // t.equal(strUtil.convertToCaptionNum(11, false, false), '一十一');
  91. // t.equal(strUtil.convertToCaptionNum(11, false, true), '壹拾壹');
  92. // t.equal(strUtil.convertToCaptionNum(12, false, false), '一十二');
  93. // t.equal(strUtil.convertToCaptionNum(12, false, true), '壹拾贰');
  94. // t.equal(strUtil.convertToCaptionNum(21, false, false), '二十一');
  95. // t.equal(strUtil.convertToCaptionNum(21, false, true), '贰拾壹');
  96. //
  97. // t.equal(strUtil.convertToCaptionNum(0.123456789, false, false), '零点一二三四五六七八九');
  98. // t.equal(strUtil.convertToCaptionNum(10.123456789, false, false), '一十点一二三四五六七八九');
  99. //
  100. // t.equal(strUtil.convertToCaptionNum(123456789.15, false, false), '一亿二千三百四十五万六千七百八十九点一五');
  101. // t.equal(strUtil.convertToCaptionNum(123456789.15, false, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖点壹伍');
  102. // t.equal(strUtil.convertToCaptionNum(123456789.15, true, false), '一亿二千三百四十五万六千七百八十九元一角五分');
  103. // t.equal(strUtil.convertToCaptionNum(123456789.15, true, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元壹角伍分');
  104. //
  105. // t.end();
  106. // });
  107. // test('test string combine', function(t){
  108. // t.pass('ABC:' + 1 + 0);
  109. //
  110. // t.end();
  111. // });
  112. // test('test string replacement of return/new line', function(t){
  113. // new RegExp('\n\r','g');
  114. // let str = 'ABC-\n\r-123\r\n-zyx\n-987\r';
  115. // str = str.replace(new RegExp('\n\r','g'), '|').replace(new RegExp('\r\n','g'), '|').replace(new RegExp('\n','g'), '|').replace(new RegExp('\r','g'), '|');
  116. // t.pass('test result: ' + str);
  117. // t.end();
  118. // });
  119. //
  120. // test('test URI', function(t){
  121. // let str = "测试 ''URI STRING";
  122. // console.log(encodeURI(str));
  123. // console.log(decodeURI(str));
  124. // t.pass('test result: ' + str);
  125. // t.end();
  126. // });