stringTest.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. test('splice', function(t){
  38. let arr = ["a", "b"];
  39. arr.splice(2, 0, "c");
  40. console.log(arr);
  41. t.end();
  42. })
  43. //
  44. // test('string test2', function(t){
  45. // var str="hello(world)";
  46. // var nstr = str.replace(/\([^\)]*\)/g,"");
  47. //
  48. // t.equal(nstr , "hello");
  49. // t.end();
  50. // })
  51. //
  52. // test('string test3', function(t){
  53. // var str="共 (%S) 页";
  54. // var nstr = str.replace("(%S)",10);
  55. //
  56. // t.equal(nstr , "共 10 页");
  57. // t.end();
  58. // })
  59. //
  60. // test('test extract', function(t){
  61. // let private_extract_code = function(str, idx){
  62. // let rst = '', lBracket = 0, rBracket = 0, firstIdx = idx, lastIdx = 0;
  63. // for (let i = idx; i < str.length; i++) {
  64. // if (str[i] === '(') {
  65. // lBracket++;
  66. // if (lBracket == 1) firstIdx = i + 1;
  67. // }
  68. // if (str[i] === ')') {
  69. // rBracket++;
  70. // if (lBracket == rBracket) {
  71. // lastIdx = i - 1;
  72. // if (lastIdx > firstIdx) {
  73. // if (str[firstIdx] === "'") firstIdx++;
  74. // if (str[lastIdx] !== "'") lastIdx++;
  75. // if (lastIdx > firstIdx) {
  76. // rst = str.slice(firstIdx, lastIdx);
  77. // }
  78. // }
  79. // break;
  80. // }
  81. // }
  82. // }
  83. // return rst;
  84. // };
  85. // let code = private_extract_code("at('1.1') + at('1.2')", 10);
  86. // t.equal(code , "1.2");
  87. // t.end();
  88. // })
  89. // test('test number to Chinese', function(t){
  90. // t.equal(strUtil.convertToCaptionNum(0, false, false), '零');
  91. // t.equal(strUtil.convertToCaptionNum(0, true, false), '零元整');
  92. // t.equal(strUtil.convertToCaptionNum(0.68, true, false), '零元六角八分');
  93. // t.equal(strUtil.convertToCaptionNum(0.68, true, true), '零元陆角捌分');
  94. // t.equal(strUtil.convertToCaptionNum(10, false, false), '一十');
  95. // t.equal(strUtil.convertToCaptionNum(10, false, true), '壹拾');
  96. // t.equal(strUtil.convertToCaptionNum(11, false, false), '一十一');
  97. // t.equal(strUtil.convertToCaptionNum(11, false, true), '壹拾壹');
  98. // t.equal(strUtil.convertToCaptionNum(12, false, false), '一十二');
  99. // t.equal(strUtil.convertToCaptionNum(12, false, true), '壹拾贰');
  100. // t.equal(strUtil.convertToCaptionNum(21, false, false), '二十一');
  101. // t.equal(strUtil.convertToCaptionNum(21, false, true), '贰拾壹');
  102. //
  103. // t.equal(strUtil.convertToCaptionNum(0.123456789, false, false), '零点一二三四五六七八九');
  104. // t.equal(strUtil.convertToCaptionNum(10.123456789, false, false), '一十点一二三四五六七八九');
  105. //
  106. // t.equal(strUtil.convertToCaptionNum(123456789.15, false, false), '一亿二千三百四十五万六千七百八十九点一五');
  107. // t.equal(strUtil.convertToCaptionNum(123456789.15, false, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖点壹伍');
  108. // t.equal(strUtil.convertToCaptionNum(123456789.15, true, false), '一亿二千三百四十五万六千七百八十九元一角五分');
  109. // t.equal(strUtil.convertToCaptionNum(123456789.15, true, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元壹角伍分');
  110. //
  111. // t.end();
  112. // });
  113. // test('test string combine', function(t){
  114. // t.pass('ABC:' + 1 + 0);
  115. //
  116. // t.end();
  117. // });
  118. // test('test string replacement of return/new line', function(t){
  119. // new RegExp('\n\r','g');
  120. // let str = 'ABC-\n\r-123\r\n-zyx\n-987\r';
  121. // str = str.replace(new RegExp('\n\r','g'), '|').replace(new RegExp('\r\n','g'), '|').replace(new RegExp('\n','g'), '|').replace(new RegExp('\r','g'), '|');
  122. // t.pass('test result: ' + str);
  123. // t.end();
  124. // });
  125. //
  126. // test('test URI', function(t){
  127. // let str = "测试 ''URI STRING";
  128. // console.log(encodeURI(str));
  129. // console.log(decodeURI(str));
  130. // t.pass('test result: ' + str);
  131. // t.end();
  132. // });