numerals.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.hebrew.numerals"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.hebrew.numerals"] = true;
  8. dojo.provide("dojox.date.hebrew.numerals");
  9. //Conversion from "Hindi" numerals to Hebrew numerals and vice versa
  10. (function(){
  11. var DIG="אבגדהוזחט";
  12. var TEN="יכלמנסעפצ";
  13. var HUN="קרשת";
  14. var transformChars = function(str, nogrsh){
  15. str = str.replace("יה", "טו").replace("יו", "טז");
  16. if(!nogrsh){
  17. var len = str.length;
  18. if(len > 1){
  19. str = str.substr(0, len - 1) + '"' + str.charAt(len - 1);
  20. }else{
  21. str += "\u05F3"; // 05F3:geresh
  22. }
  23. }
  24. return str; // String
  25. };
  26. var parseStrToNumber = function(str){
  27. var num = 0;
  28. dojo.forEach(str, function(ch){
  29. var i;
  30. if((i = DIG.indexOf(ch)) != -1){
  31. num += ++i;
  32. }else if((i = TEN.indexOf(ch)) != -1){
  33. num += 10 * ++i;
  34. }else if((i = HUN.indexOf(ch)) != -1){
  35. num += 100 * ++i;
  36. }
  37. });
  38. return num; //Number
  39. };
  40. var convertNumberToStr = function(num){
  41. var str = "", n = 4, j = 9;
  42. while(num){
  43. if(num >= n*100){
  44. str += HUN.charAt(n-1);
  45. num -= n*100;
  46. continue;
  47. }else if(n > 1){
  48. n--;
  49. continue;
  50. }else if(num >= j*10){
  51. str += TEN.charAt(j-1);
  52. num -= j*10;
  53. }else if(j > 1){
  54. j--;
  55. continue;
  56. }else if(num > 0){
  57. str += DIG.charAt(num-1);
  58. num = 0;
  59. }
  60. }
  61. return str; //String
  62. };
  63. dojox.date.hebrew.numerals.getYearHebrewLetters = function(/*Number */ year){
  64. // summary: converts the year from an integer to Hebrew numerals.
  65. //
  66. // example:
  67. // | var date1 = new dojox.date.hebrew.Date();
  68. // |
  69. // | document.writeln(dojox.date.hebrew.numerals.getYearHebrewLetters(date1.getFullYear());
  70. var rem = year % 1000;
  71. //FIXME: tests include dates outside this range and seem to pass.
  72. // if((year - rem) / 1000 != 5){ throw new Error("Hebrew year "+year+" is not in range 5001-5999");}
  73. return transformChars(convertNumberToStr(rem)); // String
  74. };
  75. dojox.date.hebrew.numerals.parseYearHebrewLetters = function(/*String hebrew year*/ year){
  76. // summary: converts the year written in Hebrew numerals to an integer
  77. //
  78. // example:
  79. // | var date = new dojox.date.hebrew.Date();
  80. // | date.setFullYear(dojox.date.hebrew.numerals.parseYearHebrewLetters('\u05ea\u05e9\u05e1\u05f4\u05d7'));
  81. return parseStrToNumber(year) + 5000; // int
  82. };
  83. dojox.date.hebrew.numerals.getDayHebrewLetters = function(day, /*boolean?*/ nogrsh){
  84. // summary: converts an integer to a String representing the number in Hebrew numerals. Can be formatted with or without geresh ׳
  85. //
  86. // example:
  87. // | var date1 = new dojox.date.hebrew.Date();
  88. // |
  89. // | document.writeln(dojox.date.hebrew.numerals.getDayHebrewLetters(date1.getDay());
  90. return transformChars(convertNumberToStr(day), nogrsh); // String
  91. };
  92. dojox.date.hebrew.numerals.parseDayHebrewLetters = function(/*String hebrew*/ day){
  93. // summary: converts the string containing a Hebrew numeral to an integer
  94. //
  95. // example:
  96. // | var date1 = new dojox.date.hebrew.Date();
  97. // |
  98. // | date1.setDate(dojox.date.hebrew.numerals.parseDayHebrewLetters('\u05d0')); // ALEPH
  99. return parseStrToNumber(day); // int
  100. };
  101. dojox.date.hebrew.numerals.getMonthHebrewLetters = function(/*int*/month){
  102. // summary: converts an integer representing a month to a String written in Hebrew numerals
  103. //
  104. // example:
  105. // | var date1 = new dojox.date.hebrew.Date();
  106. // |
  107. // | document.writeln(dojox.date.hebrew.numerals.getMonthHebrewLetters(date1.getMonth());
  108. return transformChars(convertNumberToStr(month+1)); // String
  109. };
  110. dojox.date.hebrew.numerals.parseMonthHebrewLetters = function(/*String*/monthStr){
  111. // summary: converts a Hebrew numeral string representing
  112. // a month to an integer. The returned value
  113. // is indexed in the month name array. To use it for
  114. // setMonth, do correction for leap year
  115. //
  116. // example:
  117. // | var date = new dojox.date.hebrew.Date();
  118. // | var number = dojox.date.hebrew.numerals.parseMonthHebrewLetters("\u05ea\u05de\u05d5\u05d6"); // Tammuz
  119. // | date.setMonth(number);
  120. //month number from 0 to 12
  121. var monnum = dojox.date.hebrew.numerals.parseDayHebrewLetters(monthStr) - 1;
  122. if(monnum == -1 || monnum > 12){
  123. throw new Error("The month name is incorrect , month = " + monnum);
  124. }
  125. return monnum;
  126. };
  127. })();
  128. }