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