stringTest.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. t.equal(strUtil.convertToCaptionNum(0.123456789, false, false), '零点一二三四五六七八九');
  80. t.equal(strUtil.convertToCaptionNum(10.123456789, false, false), '一十点一二三四五六七八九');
  81. t.equal(strUtil.convertToCaptionNum(123456789.15, false, false), '一亿二千三百四十五万六千七百八十九点一五');
  82. t.equal(strUtil.convertToCaptionNum(123456789.15, false, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖点壹伍');
  83. t.equal(strUtil.convertToCaptionNum(123456789.15, true, false), '一亿二千三百四十五万六千七百八十九元一角五分');
  84. t.equal(strUtil.convertToCaptionNum(123456789.15, true, true), '壹億贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元壹角伍分');
  85. t.end();
  86. });