stringTest.js 4.9 KB

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