stringTest.js 4.4 KB

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