Utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Generated by CoffeeScript 1.10.0
  2. var Pattern, Utils;
  3. Pattern = require('./Pattern');
  4. Utils = (function() {
  5. function Utils() {}
  6. Utils.REGEX_LEFT_TRIM_BY_CHAR = {};
  7. Utils.REGEX_RIGHT_TRIM_BY_CHAR = {};
  8. Utils.REGEX_SPACES = /\s+/g;
  9. Utils.REGEX_DIGITS = /^\d+$/;
  10. Utils.REGEX_OCTAL = /[^0-7]/gi;
  11. Utils.REGEX_HEXADECIMAL = /[^a-f0-9]/gi;
  12. Utils.PATTERN_DATE = new Pattern('^' + '(?<year>[0-9][0-9][0-9][0-9])' + '-(?<month>[0-9][0-9]?)' + '-(?<day>[0-9][0-9]?)' + '(?:(?:[Tt]|[ \t]+)' + '(?<hour>[0-9][0-9]?)' + ':(?<minute>[0-9][0-9])' + ':(?<second>[0-9][0-9])' + '(?:\.(?<fraction>[0-9]*))?' + '(?:[ \t]*(?<tz>Z|(?<tz_sign>[-+])(?<tz_hour>[0-9][0-9]?)' + '(?::(?<tz_minute>[0-9][0-9]))?))?)?' + '$', 'i');
  13. Utils.LOCAL_TIMEZONE_OFFSET = new Date().getTimezoneOffset() * 60 * 1000;
  14. Utils.trim = function(str, _char) {
  15. var regexLeft, regexRight;
  16. if (_char == null) {
  17. _char = '\\s';
  18. }
  19. return str.trim();
  20. regexLeft = this.REGEX_LEFT_TRIM_BY_CHAR[_char];
  21. if (regexLeft == null) {
  22. this.REGEX_LEFT_TRIM_BY_CHAR[_char] = regexLeft = new RegExp('^' + _char + '' + _char + '*');
  23. }
  24. regexLeft.lastIndex = 0;
  25. regexRight = this.REGEX_RIGHT_TRIM_BY_CHAR[_char];
  26. if (regexRight == null) {
  27. this.REGEX_RIGHT_TRIM_BY_CHAR[_char] = regexRight = new RegExp(_char + '' + _char + '*$');
  28. }
  29. regexRight.lastIndex = 0;
  30. return str.replace(regexLeft, '').replace(regexRight, '');
  31. };
  32. Utils.ltrim = function(str, _char) {
  33. var regexLeft;
  34. if (_char == null) {
  35. _char = '\\s';
  36. }
  37. regexLeft = this.REGEX_LEFT_TRIM_BY_CHAR[_char];
  38. if (regexLeft == null) {
  39. this.REGEX_LEFT_TRIM_BY_CHAR[_char] = regexLeft = new RegExp('^' + _char + '' + _char + '*');
  40. }
  41. regexLeft.lastIndex = 0;
  42. return str.replace(regexLeft, '');
  43. };
  44. Utils.rtrim = function(str, _char) {
  45. var regexRight;
  46. if (_char == null) {
  47. _char = '\\s';
  48. }
  49. regexRight = this.REGEX_RIGHT_TRIM_BY_CHAR[_char];
  50. if (regexRight == null) {
  51. this.REGEX_RIGHT_TRIM_BY_CHAR[_char] = regexRight = new RegExp(_char + '' + _char + '*$');
  52. }
  53. regexRight.lastIndex = 0;
  54. return str.replace(regexRight, '');
  55. };
  56. Utils.isEmpty = function(value) {
  57. return !value || value === '' || value === '0' || (value instanceof Array && value.length === 0);
  58. };
  59. Utils.subStrCount = function(string, subString, start, length) {
  60. var c, i, j, len, ref, sublen;
  61. c = 0;
  62. string = '' + string;
  63. subString = '' + subString;
  64. if (start != null) {
  65. string = string.slice(start);
  66. }
  67. if (length != null) {
  68. string = string.slice(0, length);
  69. }
  70. len = string.length;
  71. sublen = subString.length;
  72. for (i = j = 0, ref = len; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
  73. if (subString === string.slice(i, sublen)) {
  74. c++;
  75. i += sublen - 1;
  76. }
  77. }
  78. return c;
  79. };
  80. Utils.isDigits = function(input) {
  81. this.REGEX_DIGITS.lastIndex = 0;
  82. return this.REGEX_DIGITS.test(input);
  83. };
  84. Utils.octDec = function(input) {
  85. this.REGEX_OCTAL.lastIndex = 0;
  86. return parseInt((input + '').replace(this.REGEX_OCTAL, ''), 8);
  87. };
  88. Utils.hexDec = function(input) {
  89. this.REGEX_HEXADECIMAL.lastIndex = 0;
  90. input = this.trim(input);
  91. if ((input + '').slice(0, 2) === '0x') {
  92. input = (input + '').slice(2);
  93. }
  94. return parseInt((input + '').replace(this.REGEX_HEXADECIMAL, ''), 16);
  95. };
  96. Utils.utf8chr = function(c) {
  97. var ch;
  98. ch = String.fromCharCode;
  99. if (0x80 > (c %= 0x200000)) {
  100. return ch(c);
  101. }
  102. if (0x800 > c) {
  103. return ch(0xC0 | c >> 6) + ch(0x80 | c & 0x3F);
  104. }
  105. if (0x10000 > c) {
  106. return ch(0xE0 | c >> 12) + ch(0x80 | c >> 6 & 0x3F) + ch(0x80 | c & 0x3F);
  107. }
  108. return ch(0xF0 | c >> 18) + ch(0x80 | c >> 12 & 0x3F) + ch(0x80 | c >> 6 & 0x3F) + ch(0x80 | c & 0x3F);
  109. };
  110. Utils.parseBoolean = function(input, strict) {
  111. var lowerInput;
  112. if (strict == null) {
  113. strict = true;
  114. }
  115. if (typeof input === 'string') {
  116. lowerInput = input.toLowerCase();
  117. if (!strict) {
  118. if (lowerInput === 'no') {
  119. return false;
  120. }
  121. }
  122. if (lowerInput === '0') {
  123. return false;
  124. }
  125. if (lowerInput === 'false') {
  126. return false;
  127. }
  128. if (lowerInput === '') {
  129. return false;
  130. }
  131. return true;
  132. }
  133. return !!input;
  134. };
  135. Utils.isNumeric = function(input) {
  136. this.REGEX_SPACES.lastIndex = 0;
  137. return typeof input === 'number' || typeof input === 'string' && !isNaN(input) && input.replace(this.REGEX_SPACES, '') !== '';
  138. };
  139. Utils.stringToDate = function(str) {
  140. var date, day, fraction, hour, info, minute, month, second, tz_hour, tz_minute, tz_offset, year;
  141. if (!(str != null ? str.length : void 0)) {
  142. return null;
  143. }
  144. info = this.PATTERN_DATE.exec(str);
  145. if (!info) {
  146. return null;
  147. }
  148. year = parseInt(info.year, 10);
  149. month = parseInt(info.month, 10) - 1;
  150. day = parseInt(info.day, 10);
  151. if (info.hour == null) {
  152. date = new Date(Date.UTC(year, month, day));
  153. return date;
  154. }
  155. hour = parseInt(info.hour, 10);
  156. minute = parseInt(info.minute, 10);
  157. second = parseInt(info.second, 10);
  158. if (info.fraction != null) {
  159. fraction = info.fraction.slice(0, 3);
  160. while (fraction.length < 3) {
  161. fraction += '0';
  162. }
  163. fraction = parseInt(fraction, 10);
  164. } else {
  165. fraction = 0;
  166. }
  167. if (info.tz != null) {
  168. tz_hour = parseInt(info.tz_hour, 10);
  169. if (info.tz_minute != null) {
  170. tz_minute = parseInt(info.tz_minute, 10);
  171. } else {
  172. tz_minute = 0;
  173. }
  174. tz_offset = (tz_hour * 60 + tz_minute) * 60000;
  175. if ('-' === info.tz_sign) {
  176. tz_offset *= -1;
  177. }
  178. }
  179. date = new Date(Date.UTC(year, month, day, hour, minute, second, fraction));
  180. if (tz_offset) {
  181. date.setTime(date.getTime() + tz_offset);
  182. }
  183. return date;
  184. };
  185. Utils.strRepeat = function(str, number) {
  186. var i, res;
  187. res = '';
  188. i = 0;
  189. while (i < number) {
  190. res += str;
  191. i++;
  192. }
  193. return res;
  194. };
  195. Utils.getStringFromFile = function(path, callback) {
  196. var data, fs, j, len1, name, ref, req, xhr;
  197. if (callback == null) {
  198. callback = null;
  199. }
  200. xhr = null;
  201. if (typeof window !== "undefined" && window !== null) {
  202. if (window.XMLHttpRequest) {
  203. xhr = new XMLHttpRequest();
  204. } else if (window.ActiveXObject) {
  205. ref = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
  206. for (j = 0, len1 = ref.length; j < len1; j++) {
  207. name = ref[j];
  208. try {
  209. xhr = new ActiveXObject(name);
  210. } catch (undefined) {}
  211. }
  212. }
  213. }
  214. if (xhr != null) {
  215. if (callback != null) {
  216. xhr.onreadystatechange = function() {
  217. if (xhr.readyState === 4) {
  218. if (xhr.status === 200 || xhr.status === 0) {
  219. return callback(xhr.responseText);
  220. } else {
  221. return callback(null);
  222. }
  223. }
  224. };
  225. xhr.open('GET', path, true);
  226. return xhr.send(null);
  227. } else {
  228. xhr.open('GET', path, false);
  229. xhr.send(null);
  230. if (xhr.status === 200 || xhr.status === 0) {
  231. return xhr.responseText;
  232. }
  233. return null;
  234. }
  235. } else {
  236. req = require;
  237. fs = req('fs');
  238. if (callback != null) {
  239. return fs.readFile(path, function(err, data) {
  240. if (err) {
  241. return callback(null);
  242. } else {
  243. return callback(String(data));
  244. }
  245. });
  246. } else {
  247. data = fs.readFileSync(path);
  248. if (data != null) {
  249. return String(data);
  250. }
  251. return null;
  252. }
  253. }
  254. };
  255. return Utils;
  256. })();
  257. module.exports = Utils;