scratch – Blame information for rev 125

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