Utils.js 8.1 KB

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