scratch – Diff between revs 75 and 125

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