utils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Polyfills */
  2. String.prototype.padZero = function(len, c) {
  3. var s = '', c = c || '0', len = (len || 2) - this.length;
  4. while (s.length < len)
  5. s += c;
  6. return s + this;
  7. }
  8. String.prototype.allTrim = function() {
  9. return this.replace(/ /g, '');
  10. }
  11. String.prototype.toCurrency = function() {
  12. var res = Number(this.replace(/[^0-9\,-]+/g,"").replace(',','.'));
  13. if (isNaN(res))
  14. return null;
  15. else
  16. return res;
  17. }
  18. Number.prototype.padZero = function(len, c) {
  19. return String(this).padZero(len, c);
  20. }
  21. Number.prototype.formatAsCurrency = function() {
  22. return this.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits:2});
  23. }
  24. /**
  25. * Number.prototype.format(n, x, s, c)
  26. *
  27. * @param integer n: length of decimal
  28. * @param mixed s: sections delimiter
  29. * @param mixed c: decimal delimiter
  30. */
  31. Number.prototype.format = function(n, s, c) {
  32. var re = '\\d(?=(\\d{' + (3 || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
  33. num = this.toFixed(Math.max(0, ~~n));
  34. return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
  35. };
  36. Number.prototype.toFormattedValue = function(){
  37. return this.format(2,'.',',');
  38. }
  39. String.prototype.toFormattedValue = function(){
  40. if (this==null || this=='')
  41. return '';
  42. var n=parseFloat(this.replace(',','.'));
  43. return n.toFormattedValue();
  44. }
  45. String.prototype.formatAsDateTime = function() {
  46. var dt = new Date(this.toString());
  47. //var dt = new Date(Date.fromISO(this.toString()));// new Date(this.toString());
  48. //var dt = new Date(Date(this.toString().replace(/-/g, '/')));
  49. return dt.getDate().padZero(2) + "/" + (dt.getMonth() + 1).padZero(2) + "/"
  50. + dt.getFullYear() + " " + dt.getHours().padZero(2) + ":"
  51. + dt.getMinutes().padZero(2);
  52. }
  53. String.prototype.formatAsDateTimeSec = function() {
  54. var dt = new Date(this.toString())
  55. //var dt = new Date(Date.fromISO(this.toString()));// new Date(this.toString());
  56. //var dt = new Date(Date(this.toString().replace(/-/g, '/')));
  57. return dt.getDate().padZero(2) + "/" + (dt.getMonth() + 1).padZero(2) + "/"
  58. + dt.getFullYear() + " " + dt.getHours().padZero(2) + ":"
  59. + dt.getMinutes().padZero(2) +":"+dt.getSeconds().padZero(2);
  60. }
  61. String.prototype.formatAsDate = function() {
  62. var dt = new Date(this.toString())
  63. //var dt = new Date(Date.fromISO(this.toString()));// new Date(this.toString());
  64. //var dt = new Date(Date(this.toString().replace(/-/g, '/')));
  65. return dt.getDate().padZero(2) + "/" + (dt.getMonth() + 1).padZero(2) + "/"
  66. + dt.getFullYear();
  67. }
  68. String.prototype.formatAsTime = function() {
  69. var dt = new Date(this.toString())
  70. //var dt = new Date(Date.fromISO(this.toString()));// new Date(this.toString());
  71. //var dt = new Date(Date(this.toString().replace(/-/g, '/')));
  72. return dt.getHours().padZero(2) + ":" + dt.getMinutes().padZero(2);
  73. }
  74. Date.fromISO= (function(){
  75. var testIso = '2011-11-24T09:00:27+0200';
  76. // Chrome
  77. var diso= Date.parse(testIso);
  78. if(diso===1322118027000) return function(s){
  79. return new Date(Date.parse(s));
  80. }
  81. // JS 1.8 gecko
  82. var noOffset = function(s) {
  83. var day= s.slice(0,-5).split(/\D/).map(function(itm){
  84. return parseInt(itm, 10) || 0;
  85. });
  86. day[1]-= 1;
  87. day= new Date(Date.UTC.apply(Date, day));
  88. var offsetString = s.slice(-5)
  89. var offset = parseInt(offsetString,10)/100;
  90. if (offsetString.slice(0,1)=="+") offset*=-1;
  91. day.setHours(day.getHours()+offset);
  92. return day.getTime();
  93. }
  94. if (noOffset(testIso)===1322118027000) {
  95. return noOffset;
  96. }
  97. return function(s){ // kennebec@SO + QTax@SO
  98. var day, tz,
  99. // rx = /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d{4}))?$/,
  100. rx = /^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):?(\d\d))?$/,
  101. p= rx.exec(s) || [];
  102. if(p[1]){
  103. day= p[1].split(/\D/).map(function(itm){
  104. return parseInt(itm, 10) || 0;
  105. });
  106. day[1]-= 1;
  107. day= new Date(Date.UTC.apply(Date, day));
  108. if(!day.getDate()) return NaN;
  109. if(p[5]){
  110. tz= parseInt(p[5], 10)/100*60;
  111. if(p[6]) tz += parseInt(p[6], 10);
  112. if(p[4]== "+") tz*= -1;
  113. if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
  114. }
  115. return day;
  116. }
  117. return NaN;
  118. }
  119. })()
  120. Number.prototype.roundTo2Digits = function() {
  121. return Math.round(this * 100) / 100;
  122. }
  123. if (!String.prototype.includes) {
  124. String.prototype.includes = function() {
  125. 'use strict';
  126. return String.prototype.indexOf.apply(this, arguments) !== -1;
  127. };
  128. }
  129. String.prototype.hashCode = function() {
  130. var hash = 0, i, chr, len;
  131. if (this.length == 0) return hash;
  132. for (i = 0, len = this.length; i < len; i++) {
  133. chr = this.charCodeAt(i);
  134. hash = ((hash << 5) - hash) + chr;
  135. hash |= 0; // Convert to 32bit integer
  136. }
  137. return hash;
  138. };
  139. Date.prototype.asDateString = function() {
  140. if (this==null)
  141. return "";
  142. var dd=this.getDate().padZero(2,'0');
  143. var mm=(this.getMonth()+1).padZero(2,'0');
  144. var yy=this.getYear()+1900;
  145. return dd+'/'+mm+'/'+yy
  146. }
  147. var cumulativeOffset = function(element) {
  148. var top = 0, left = 0;
  149. do {
  150. top += element.offsetTop || 0;
  151. left += element.offsetLeft || 0;
  152. element = element.offsetParent;
  153. } while(element);
  154. return {
  155. top: top,
  156. left: left
  157. };
  158. };
  159. if (!Array.prototype.forEach) {
  160. Array.prototype.forEach = function(fn, scope) {
  161. for(var i = 0, len = this.length; i < len; ++i) {
  162. fn.call(scope, this[i], i, this);
  163. }
  164. }
  165. }
  166. /* END Polyfills */
  167. function addDays(date, days) {
  168. var newDate = new Date();
  169. newDate.setDate(date.getDate() + days);
  170. return newDate;
  171. }
  172. function addMonths(date, months) {
  173. return new Date(new Date(date).setMonth(date.getMonth()+months));
  174. }
  175. function removeURLParameter(url, parameter) {
  176. //prefer to use l.search if you have a location/link object
  177. var urlparts= url.split('?');
  178. if (urlparts.length>=2) {
  179. var prefix= encodeURIComponent(parameter)+'=';
  180. var pars= urlparts[1].split(/[&;]/g);
  181. //reverse iteration as may be destructive
  182. for (var i= pars.length; i-- > 0;) {
  183. //idiom for string.startsWith
  184. if (pars[i].lastIndexOf(prefix, 0) !== -1) {
  185. pars.splice(i, 1);
  186. }
  187. }
  188. url= urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : "");
  189. return url;
  190. } else {
  191. return url;
  192. }
  193. }