HxOverrides.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. @:noDoc
  23. class HxOverrides {
  24. static function dateStr( date :Date ) : String {
  25. var m = date.getMonth() + 1;
  26. var d = date.getDate();
  27. var h = date.getHours();
  28. var mi = date.getMinutes();
  29. var s = date.getSeconds();
  30. return date.getFullYear()
  31. +"-"+(if( m < 10 ) "0"+m else ""+m)
  32. +"-"+(if( d < 10 ) "0"+d else ""+d)
  33. +" "+(if( h < 10 ) "0"+h else ""+h)
  34. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  35. +":"+(if( s < 10 ) "0"+s else ""+s);
  36. }
  37. static function strDate( s : String ) : Date {
  38. switch( s.length ) {
  39. case 8: // hh:mm:ss
  40. var k = s.split(":");
  41. var d : Date = untyped __new__(Date);
  42. untyped d["setTime"](0);
  43. untyped d["setUTCHours"](k[0]);
  44. untyped d["setUTCMinutes"](k[1]);
  45. untyped d["setUTCSeconds"](k[2]);
  46. return d;
  47. case 10: // YYYY-MM-DD
  48. var k = s.split("-");
  49. return new Date(cast k[0],cast untyped k[1] - 1,cast k[2],0,0,0);
  50. case 19: // YYYY-MM-DD hh:mm:ss
  51. var k = s.split(" ");
  52. var y = k[0].split("-");
  53. var t = k[1].split(":");
  54. return new Date(cast y[0],cast untyped y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  55. default:
  56. throw "Invalid date format : " + s;
  57. }
  58. }
  59. @:pure
  60. static function cca( s : String, index : Int ) : Null<Int> {
  61. var x = (cast s).charCodeAt(index);
  62. if( x != x ) // fast isNaN
  63. return js.Lib.undefined; // isNaN will still return true
  64. return x;
  65. }
  66. @:pure
  67. static function substr( s : String, pos : Int, ?len : Int ) : String {
  68. if (len == null) {
  69. len = s.length;
  70. } else if (len < 0) {
  71. if (pos == 0)
  72. len = s.length + len;
  73. else
  74. return "";
  75. }
  76. #if (js_es < 5)
  77. if (pos < 0) {
  78. pos = s.length + pos;
  79. if (pos < 0)
  80. pos = 0;
  81. }
  82. #end
  83. return (cast s).substr(pos, len);
  84. }
  85. @:pure
  86. static function indexOf<T>( a : Array<T>, obj : T, i : Int) {
  87. var len = a.length;
  88. if (i < 0) {
  89. i += len;
  90. if (i < 0) i = 0;
  91. }
  92. while (i < len)
  93. {
  94. if (untyped __js__("a[i] === obj"))
  95. return i;
  96. i++;
  97. }
  98. return -1;
  99. }
  100. @:pure
  101. static function lastIndexOf<T>( a : Array<T>, obj : T, i : Int) {
  102. var len = a.length;
  103. if (i >= len)
  104. i = len - 1;
  105. else if (i < 0)
  106. i += len;
  107. while (i >= 0)
  108. {
  109. if (untyped __js__("a[i] === obj"))
  110. return i;
  111. i--;
  112. }
  113. return -1;
  114. }
  115. static function remove<T>( a : Array<T>, obj : T ) {
  116. var i = a.indexOf(obj);
  117. if( i == -1 ) return false;
  118. a.splice(i,1);
  119. return true;
  120. }
  121. @:pure
  122. static function iter<T>( a : Array<T> ) : Iterator<T> untyped {
  123. return {
  124. cur : 0,
  125. arr : a,
  126. hasNext : function() {
  127. return __this__.cur < __this__.arr.length;
  128. },
  129. next : function() {
  130. return __this__.arr[__this__.cur++];
  131. }
  132. };
  133. }
  134. static function __init__() untyped {
  135. #if (js_es < 5)
  136. __feature__('HxOverrides.indexOf', if( Array.prototype.indexOf ) __js__("HxOverrides").indexOf = function(a,o,i) return Array.prototype.indexOf.call(a, o, i));
  137. __feature__('HxOverrides.lastIndexOf', if( Array.prototype.lastIndexOf ) __js__("HxOverrides").lastIndexOf = function(a,o,i) return Array.prototype.lastIndexOf.call(a, o, i));
  138. #end
  139. }
  140. }