privateFunctionTest.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Created by Tony on 2017/4/5.
  3. */
  4. var test = require('tape');
  5. var headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  6. test('test private 1', function(t){
  7. private_getCellIdxStr = function(idx, pre){
  8. var rst = 'A', prefix = '', tmpIdx = idx;
  9. if (tmpIdx >= 26) {
  10. var ti = Math.floor(tmpIdx / 26), tj = tmpIdx % 26;
  11. if (ti <= 26) {
  12. rst = pre + headerStr[ti - 1] + headerStr[tj];
  13. } else {
  14. //rst = private_getCellIdxStr2(ti, 'A' + pre) + headerStr[tj];
  15. rst = private_getCellIdxStr2(ti, 'A' + pre) + headerStr[tj];
  16. }
  17. } else {
  18. rst = pre + headerStr[tmpIdx % 26];
  19. }
  20. return rst;
  21. };
  22. private_getCellIdxStr2 = function(idx){
  23. var rst = 'A';
  24. if (idx < 26) {
  25. rst = headerStr[idx];
  26. } else if (idx < 26*26+26) {
  27. var ti = Math.floor(idx / 26), tj = idx % 26;
  28. rst = headerStr[ti - 1] + headerStr[tj];
  29. } else if (idx < 26*26*26+26) {
  30. var ti = Math.floor(idx / (26*26)), tj = Math.floor((idx - ti * 26*26) / 26), tk = idx % 26;
  31. rst = headerStr[ti - 1] + headerStr[tj-1] + headerStr[tk];
  32. }
  33. return rst;
  34. };
  35. t.equal(private_getCellIdxStr2(0, ''), 'A');
  36. t.equal(private_getCellIdxStr2(20, ''), 'U');
  37. t.equal(private_getCellIdxStr2(25, ''), 'Z');
  38. t.equal(private_getCellIdxStr2(26, ''), 'AA');
  39. t.equal(private_getCellIdxStr2(27, ''), 'AB');
  40. t.equal(private_getCellIdxStr2(51, ''), 'AZ');
  41. t.equal(private_getCellIdxStr2(52, ''), 'BA');
  42. t.equal(private_getCellIdxStr2(77, ''), 'BZ');
  43. t.equal(private_getCellIdxStr2(103, ''), 'CZ');
  44. t.equal(private_getCellIdxStr2(675, ''), 'YZ');
  45. t.equal(private_getCellIdxStr2(676, ''), 'ZA');
  46. t.equal(private_getCellIdxStr2(701, ''), 'ZZ');
  47. t.equal(private_getCellIdxStr2(702, ''), 'AAA');
  48. t.equal(private_getCellIdxStr2(727, ''), 'AAZ');
  49. t.equal(private_getCellIdxStr2(728, ''), 'ABA');
  50. t.end();
  51. })