1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var _ = require('lodash');
|
13 | var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
|
14 | var sprintf = require("sprintf").sprintf;
|
15 | var Symbol = require("symbol");
|
16 |
|
17 | var UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
18 | var HEX_CHARS = 'abcdef1234567890';
|
19 | var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
20 |
|
21 |
|
22 | var DateTimeRegex = /^\d{4}-([0]\d|1[0-2])-([0-2]\d|3[01])(?:[T ](\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?)?(?:Z(-?\d*))?$/g;
|
23 | var BooleanTrueRegex = /^true$/ig;
|
24 | var BooleanFalseRegex = /^false$/ig;
|
25 | var NullRegex = /^null$/ig;
|
26 | var UndefinedRegex = /^undefined$/ig;
|
27 | var IntegerRegex =/^[-+]?\d+$/g;
|
28 | var FloatRegex =/^[+-]?\d+(\.\d+)?$/g;
|
29 | var GuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function UnknownPropertyDescriptor(obj, name) {
|
38 | Object.defineProperty(this, 'value', { configurable:false, enumerable:true, get: function() { return obj[name]; }, set: function(value) { obj[name]=value; } });
|
39 | Object.defineProperty(this, 'name', { configurable:false, enumerable:true, get: function() { return name; } });
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function LangUtils() {
|
48 |
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | LangUtils.inherits = function(ctor, superCtor) {
|
66 |
|
67 | if (typeof superCtor !== "function" && superCtor !== null) {
|
68 | throw new TypeError("Super expression must either be null or a function, not " + typeof superCtor);
|
69 | }
|
70 |
|
71 |
|
72 | if (isNode) {
|
73 | var utilModule = "util";
|
74 | var util = require(utilModule);
|
75 |
|
76 | return util.inherits(ctor, superCtor);
|
77 | }
|
78 |
|
79 | ctor.prototype = Object.create(superCtor && superCtor.prototype, {
|
80 | constructor: {
|
81 | value: ctor,
|
82 | enumerable: false,
|
83 | writable: true,
|
84 | configurable: true
|
85 | }
|
86 | });
|
87 | if (superCtor) {
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | if (typeof Object.setPrototypeOf === 'function') {
|
96 | Object.setPrototypeOf(ctor, superCtor)
|
97 | }
|
98 | else {
|
99 | ctor.__proto__ = superCtor
|
100 | }
|
101 | }
|
102 |
|
103 | ctor.super_ = ctor.__proto__;
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | LangUtils.getFunctionParams = function(fn) {
|
112 | if (!_.isFunction(fn))
|
113 | return [];
|
114 | var fnStr = fn.toString().replace(STRIP_COMMENTS, '');
|
115 | var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(/([^\s,]+)/g);
|
116 | if(result === null)
|
117 | result = [];
|
118 | return result;
|
119 | };
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | LangUtils.convert = function(value) {
|
126 | var result;
|
127 | if ((typeof value === 'string'))
|
128 | {
|
129 | if (value.length===0) {
|
130 | result = value
|
131 | }
|
132 | if (value.match(BooleanTrueRegex)) {
|
133 | result = true;
|
134 | }
|
135 | else if (value.match(BooleanFalseRegex)) {
|
136 | result = false;
|
137 | }
|
138 | else if (value.match(NullRegex) || value.match(UndefinedRegex)) {
|
139 | result = null;
|
140 | }
|
141 | else if (value.match(IntegerRegex)) {
|
142 | result = parseInt(value);
|
143 | }
|
144 | else if (value.match(FloatRegex)) {
|
145 | result = parseFloat(value);
|
146 | }
|
147 | else if (value.match(DateTimeRegex)) {
|
148 | result = new Date(Date.parse(value));
|
149 | }
|
150 | else {
|
151 | result = value;
|
152 | }
|
153 | }
|
154 | else {
|
155 | result = value;
|
156 | }
|
157 | return result;
|
158 | };
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | LangUtils.extend = function(origin, expr, value, options) {
|
169 |
|
170 | options = options || { convertValues:false };
|
171 |
|
172 | var match = /(^\w+)\[/.exec(expr), name, descriptor, expr1;
|
173 | if (match) {
|
174 |
|
175 | name = match[1];
|
176 |
|
177 | if (/^\d+$/g.test(name)) {
|
178 |
|
179 | if (!_.isArray(origin.value))
|
180 | origin.value = [];
|
181 |
|
182 | expr1 = expr.substr(match.index + match[1].length);
|
183 | LangUtils.extend(origin, expr1, value, options);
|
184 | }
|
185 | else {
|
186 |
|
187 | origin[name] = origin[name] || new LangUtils();
|
188 | descriptor = new UnknownPropertyDescriptor(origin, name);
|
189 |
|
190 | expr1 = expr.substr(match.index + match[1].length);
|
191 | LangUtils.extend(descriptor, expr1, value, options);
|
192 | }
|
193 | }
|
194 | else if (expr.indexOf('[')===0) {
|
195 |
|
196 | var re = /\[(.*?)\]/g;
|
197 | match = re.exec(expr);
|
198 | if (match) {
|
199 | name = match[1];
|
200 |
|
201 | expr1 = expr.substr(match.index + match[0].length);
|
202 | if (/^\d+$/g.test(name)) {
|
203 |
|
204 | if (!_.isArray(origin.value))
|
205 | origin.value = [];
|
206 | }
|
207 | if (expr1.length===0) {
|
208 | if (origin.value instanceof LangUtils) {
|
209 | origin.value = {};
|
210 | }
|
211 | var typedValue;
|
212 |
|
213 | if ((typeof value === 'string') && options.convertValues) {
|
214 | typedValue = LangUtils.convert(value);
|
215 | }
|
216 | else {
|
217 | typedValue = value;
|
218 | }
|
219 | if (_.isArray(origin.value))
|
220 | origin.value.push(typedValue);
|
221 | else
|
222 | origin.value[name] = typedValue;
|
223 | }
|
224 | else {
|
225 | if (origin.value instanceof LangUtils) {
|
226 | origin.value = { };
|
227 | }
|
228 | origin.value[name] = origin.value[name] || new LangUtils();
|
229 | descriptor = new UnknownPropertyDescriptor(origin.value, name);
|
230 | LangUtils.extend(descriptor, expr1, value, options);
|
231 | }
|
232 | }
|
233 | else {
|
234 | throw new Error('Invalid object property notation. Expected [name]');
|
235 | }
|
236 | }
|
237 | else if (/^\w+$/.test(expr)) {
|
238 | if (options.convertValues)
|
239 | origin[expr] = LangUtils.convert(value);
|
240 | else
|
241 | origin[expr] = value;
|
242 | }
|
243 | else {
|
244 | throw new Error('Invalid object property notation. Expected property[name] or [name]');
|
245 | }
|
246 | return origin;
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | LangUtils.parseForm = function (form, options) {
|
257 | var result = {};
|
258 | if (typeof form === 'undefined' || form===null)
|
259 | return result;
|
260 | var keys = Object.keys(form);
|
261 | keys.forEach(function(key) {
|
262 | if (form.hasOwnProperty(key))
|
263 | {
|
264 | LangUtils.extend(result, key, form[key], options)
|
265 | }
|
266 | });
|
267 | return result;
|
268 | };
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | LangUtils.parseValue = function(any) {
|
275 | return LangUtils.convert(any);
|
276 | };
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | LangUtils.parseInt = function(any) {
|
283 | return parseInt(any) || 0;
|
284 | };
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | LangUtils.parseFloat = function(any) {
|
291 | return parseFloat(any) || 0;
|
292 | };
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | LangUtils.parseBoolean = function(any) {
|
299 | if (typeof any === 'undefined' || any === null)
|
300 | return false;
|
301 | else if (typeof any === 'number')
|
302 | return any !== 0;
|
303 | else if (typeof any === 'string') {
|
304 | if (any.match(LangUtils.IntegerRegex) || any.match(LangUtils.FloatRegex)) {
|
305 | return parseInt(any, 10) !== 0;
|
306 | }
|
307 | else if (any.match(LangUtils.BooleanTrueRegex))
|
308 | return true;
|
309 | else if (any.match(LangUtils.BooleanFalseRegex))
|
310 | return false;
|
311 | else if (/^yes$|^on$|^y$|^valid$/i.test(any))
|
312 | return true;
|
313 | else if (/^no$|^off$|^n$|^invalid$/i.test(any))
|
314 | return false;
|
315 | else
|
316 | return false;
|
317 | }
|
318 | else if (typeof any === 'boolean')
|
319 | return any;
|
320 | else {
|
321 | return (parseInt(any) || 0) !== 0;
|
322 | }
|
323 | };
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | LangUtils.isDate = function(value) {
|
331 | if (value instanceof Date) {
|
332 | return true;
|
333 | }
|
334 | return DateTimeRegex.test(value);
|
335 | };
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 | function Args() {
|
350 |
|
351 | }
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | Args.check = function(expr, err) {
|
359 | Args.notNull(expr,"Expression");
|
360 | var res;
|
361 | if (typeof expr === 'function') {
|
362 | res = !(expr.call());
|
363 | }
|
364 | else {
|
365 | res = (!expr);
|
366 | }
|
367 | if (res) {
|
368 | if (err instanceof Error) {
|
369 | throw err;
|
370 | }
|
371 | throw new ArgumentError(err, "ECHECK");
|
372 | }
|
373 | };
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | Args.notNull = function(arg, name) {
|
381 | if (typeof arg === 'undefined' || arg === null) {
|
382 | throw new ArgumentError(name + " may not be null or undefined", "ENULL");
|
383 | }
|
384 | };
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | Args.notString = function(arg, name) {
|
391 | if (typeof arg !== 'string') {
|
392 | throw new ArgumentError(name + " must be a string", "EARG");
|
393 | }
|
394 | };
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | Args.notFunction = function(arg, name) {
|
401 | if (typeof arg !== 'function') {
|
402 | throw new ArgumentError(name + " must be a function", "EARG");
|
403 | }
|
404 | };
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 | Args.notNumber = function(arg, name) {
|
411 | if ((typeof arg !== 'number') || isNaN(arg)) {
|
412 | throw new ArgumentError(name + " must be number", "EARG");
|
413 | }
|
414 | };
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | Args.notEmpty = function(arg, name) {
|
421 | Args.notNull(arg,name);
|
422 | if ((Object.prototype.toString.bind(arg)() === '[object Array]') && (arg.length === 0)) {
|
423 | throw new ArgumentError(name + " may not be empty","EEMPTY");
|
424 | }
|
425 | else if ((typeof arg === 'string') && (arg.length===0)) {
|
426 | throw new ArgumentError(name + " may not be empty","EEMPTY");
|
427 | }
|
428 | };
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 | Args.notNegative = function(arg, name) {
|
435 | Args.notNumber(arg,name);
|
436 | if (arg<0) {
|
437 | throw new ArgumentError(name + " may not be negative", "ENEG");
|
438 | }
|
439 | };
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | Args.notPositive = function(arg, name) {
|
446 | Args.notNumber(arg,name);
|
447 | if (arg<=0) {
|
448 | throw new ArgumentError(name + " may not be negative or zero", "EPOS");
|
449 | }
|
450 | };
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 | function TextUtils() {
|
458 |
|
459 | }
|
460 |
|
461 | |
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | TextUtils.toMD5 = function(value) {
|
468 |
|
469 | if (typeof value === 'undefined' || value === null) {
|
470 | return;
|
471 | }
|
472 |
|
473 | var md5, md5module;
|
474 | if (typeof window !== 'undefined') {
|
475 | md5module = 'blueimp-md5';
|
476 | md5 = require(md5module);
|
477 | if (typeof value === 'string') {
|
478 | return md5(value);
|
479 | }
|
480 | else if (value instanceof Date) {
|
481 | return md5(value.toUTCString());
|
482 | }
|
483 | else {
|
484 | return md5(JSON.stringify(value));
|
485 | }
|
486 | }
|
487 |
|
488 | md5module = 'crypto';
|
489 | var crypto = require(md5module);
|
490 | md5 = crypto.createHash('md5');
|
491 | if (typeof value === 'string') {
|
492 | md5.update(value);
|
493 | }
|
494 | else if (value instanceof Date) {
|
495 | md5.update(value.toUTCString());
|
496 | }
|
497 | else {
|
498 | md5.update(JSON.stringify(value));
|
499 | }
|
500 | return md5.digest('hex');
|
501 | };
|
502 |
|
503 | |
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 | TextUtils.toSHA1 = function(value) {
|
510 |
|
511 | var cryptoModule = 'crypto';
|
512 | if (typeof window !== 'undefined') {
|
513 | throw new Error('This method is not implemented for this environment')
|
514 | }
|
515 |
|
516 | var crypto = require(cryptoModule);
|
517 | if (typeof value === 'undefined' || value === null) {
|
518 | return;
|
519 | }
|
520 | var sha1 = crypto.createHash('sha1');
|
521 | if (typeof value === 'string') {
|
522 | sha1.update(value);
|
523 | }
|
524 | else if (value instanceof Date) {
|
525 | sha1.update(value.toUTCString());
|
526 | }
|
527 | else {
|
528 | sha1.update(JSON.stringify(value));
|
529 | }
|
530 | return sha1.digest('hex');
|
531 | };
|
532 |
|
533 | |
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | TextUtils.toSHA256 = function(value) {
|
540 |
|
541 | var cryptoModule = 'crypto';
|
542 | if (typeof window !== 'undefined') {
|
543 | throw new Error('This method is not implemented for this environment')
|
544 | }
|
545 |
|
546 | var crypto = require(cryptoModule);
|
547 | if (typeof value === 'undefined' || value === null) {
|
548 | return;
|
549 | }
|
550 | var sha256 = crypto.createHash('sha256');
|
551 | if (typeof value === 'string') {
|
552 | sha256.update(value);
|
553 | }
|
554 | else if (value instanceof Date) {
|
555 | sha256.update(value.toUTCString());
|
556 | }
|
557 | else {
|
558 | sha256.update(JSON.stringify(value));
|
559 | }
|
560 | return sha256.digest('hex');
|
561 | };
|
562 |
|
563 | |
564 |
|
565 |
|
566 |
|
567 |
|
568 | TextUtils.newUUID = function() {
|
569 | var chars = UUID_CHARS;
|
570 | var uuid = [];
|
571 |
|
572 | var r = void 0;
|
573 |
|
574 | uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
|
575 | uuid[14] = "4";
|
576 |
|
577 |
|
578 |
|
579 | for (var i = 0; i < 36; i++) {
|
580 | if (!uuid[i]) {
|
581 | r = 0 | Math.random() * 16;
|
582 | uuid[i] = chars[i === 19 ? r & 0x3 | 0x8 : r];
|
583 | }
|
584 | }
|
585 | return uuid.join("");
|
586 | };
|
587 |
|
588 | var loggerProperty = Symbol("logger");
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | function TraceUtils() {
|
595 |
|
596 | }
|
597 |
|
598 | TraceUtils[loggerProperty] = new TraceLogger();
|
599 |
|
600 | TraceUtils.useLogger = function(logger) {
|
601 | TraceUtils[loggerProperty] = logger;
|
602 | };
|
603 |
|
604 | TraceUtils.level = function(level) {
|
605 | TraceUtils[loggerProperty].level(level);
|
606 | };
|
607 |
|
608 | |
609 |
|
610 |
|
611 |
|
612 |
|
613 | TraceUtils.log = function(data) {
|
614 | TraceUtils[loggerProperty].log.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
615 | };
|
616 |
|
617 | |
618 |
|
619 |
|
620 |
|
621 |
|
622 | TraceUtils.error = function(data) {
|
623 | TraceUtils[loggerProperty].error.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
624 | };
|
625 |
|
626 | |
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 | TraceUtils.info = function(data) {
|
633 | TraceUtils[loggerProperty].info.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
634 | };
|
635 |
|
636 | |
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | TraceUtils.warn= function(data) {
|
643 | TraceUtils[loggerProperty].warn.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
644 | };
|
645 |
|
646 | |
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | TraceUtils.verbose = function(data) {
|
653 | TraceUtils[loggerProperty].verbose.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
654 | };
|
655 |
|
656 | |
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 | TraceUtils.debug = function(data) {
|
663 | TraceUtils[loggerProperty].debug.apply(TraceUtils[loggerProperty], Array.prototype.slice.call(arguments));
|
664 | };
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | function RandomUtils() {
|
671 |
|
672 | }
|
673 |
|
674 | |
675 |
|
676 |
|
677 |
|
678 | RandomUtils.randomChars = function(length) {
|
679 | length = length || 8;
|
680 | var chars = "abcdefghkmnopqursuvwxz2456789ABCDEFHJKLMNPQURSTUVWXYZ";
|
681 | var str = "";
|
682 | for(var i = 0; i < length; i++) {
|
683 | str += chars.substr(this.randomInt(0, chars.length-1),1);
|
684 | }
|
685 | return str;
|
686 | };
|
687 |
|
688 | |
689 |
|
690 |
|
691 |
|
692 |
|
693 | RandomUtils.randomInt = function(min, max) {
|
694 | return Math.floor(Math.random() * (max - min + 1)) + min;
|
695 | };
|
696 |
|
697 | |
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 | RandomUtils.randomHex = function(length) {
|
704 | length = (length || 8)*2;
|
705 | var str = "";
|
706 | for(var i = 0; i < length; i++) {
|
707 | str += HEX_CHARS.substr(this.randomInt(0, HEX_CHARS.length-1),1);
|
708 | }
|
709 | return str;
|
710 | };
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 | function NumberUtils() {
|
717 |
|
718 | }
|
719 |
|
720 | |
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 | NumberUtils.fromBase26 = function(s) {
|
727 | var num = 0;
|
728 | if (!/[a-z]{8}/.test(s)) {
|
729 | throw new Error('Invalid base-26 format.');
|
730 | }
|
731 | var a = 'a'.charCodeAt(0);
|
732 | for (var i = 7; i >=0; i--) {
|
733 | num = (num * 26) + (s[i].charCodeAt(0) - a);
|
734 | }
|
735 | return num;
|
736 | };
|
737 |
|
738 | |
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 | NumberUtils.toBase26 = function(x) {
|
745 |
|
746 | var num = parseInt(x);
|
747 | if (num<0) {
|
748 | throw new Error('A non-positive integer cannot be converted to base-26 format.');
|
749 | }
|
750 | if (num>208827064575) {
|
751 | throw new Error('A positive integer bigger than 208827064575 cannot be converted to base-26 format.');
|
752 | }
|
753 | var out = "";
|
754 | var length= 1;
|
755 | var a = 'a'.charCodeAt(0);
|
756 | while(length<=8)
|
757 | {
|
758 | out += String.fromCharCode(a + (num % 26));
|
759 | num = Math.floor(num / 26);
|
760 | length += 1;
|
761 | }
|
762 | return out;
|
763 | };
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 | function PathUtils() {
|
770 |
|
771 | }
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 | PathUtils.join = function (part) {
|
780 | var pathModule = "path";
|
781 | if (isNode) {
|
782 | var path = require(pathModule);
|
783 | return path.join.apply(null, Array.prototype.slice.call(arguments));
|
784 | }
|
785 |
|
786 | var parts = [], i, l;
|
787 | for (i = 0, l = arguments.length; i < l; i++) {
|
788 | parts = parts.concat(arguments[i].split("/"));
|
789 | }
|
790 |
|
791 | var newParts = [];
|
792 | for (i = 0, l = parts.length; i < l; i++) {
|
793 | var part1 = parts[i];
|
794 |
|
795 |
|
796 | if (!part1 || part1 === ".") continue;
|
797 |
|
798 | if (part1 === "..") newParts.pop();
|
799 |
|
800 | else newParts.push(part1);
|
801 | }
|
802 |
|
803 | if (parts[0] === "") newParts.unshift("");
|
804 |
|
805 | return newParts.join("/") || (newParts.length ? "/" : ".");
|
806 | };
|
807 |
|
808 | var Reset = "\x1b[0m";
|
809 | var FgBlack = "\x1b[30m";
|
810 | var FgRed = "\x1b[31m";
|
811 | var FgGreen = "\x1b[32m";
|
812 |
|
813 | var FgYellow = "\x1b[33m";
|
814 | var FgBlue = "\x1b[34m";
|
815 | var FgMagenta = "\x1b[35m";
|
816 |
|
817 | var FgCyan = "\x1b[36m";
|
818 |
|
819 | var FgWhite = "\x1b[37m";
|
820 |
|
821 | var Bold = "\x1b[1m";
|
822 |
|
823 | var LogLevels = {
|
824 | error: 0,
|
825 | warn: 1,
|
826 | info: 2,
|
827 | verbose: 3,
|
828 | debug: 4
|
829 | };
|
830 |
|
831 | var LogLevelColors = {
|
832 | error: FgRed,
|
833 | warn: FgMagenta,
|
834 | info: FgBlack,
|
835 | verbose: FgBlue,
|
836 | debug: Bold + FgGreen
|
837 | };
|
838 |
|
839 | function timestamp() {
|
840 | return (new Date()).toUTCString();
|
841 | }
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 | function writeError(level, err) {
|
849 |
|
850 | var keys = _.filter(_.keys(err), function(x) {
|
851 | return err.hasOwnProperty(x) && x!=='message' && typeof err[x] !== 'undefined' && err[x] != null;
|
852 | });
|
853 | if (err instanceof Error) {
|
854 | if (err.hasOwnProperty('stack')) {
|
855 | this.write(level, err.stack);
|
856 | }
|
857 | else {
|
858 | this.write(level, err.toString());
|
859 | }
|
860 | }
|
861 | else {
|
862 | this.write(level, err.toString());
|
863 | }
|
864 | if (keys.length>0) {
|
865 | this.write(level, "Error: " + _.map(keys, function(x) {
|
866 | return "[" + x + "]=" + err[x].toString()
|
867 | }).join(', '));
|
868 | }
|
869 | }
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 | function TraceLogger(options) {
|
877 | this.options = {
|
878 | colors:false,
|
879 | level:"info"
|
880 | };
|
881 | if (typeof options === "undefined" && options !== null && isNode) {
|
882 | if (isNode && process.env.NODE_ENV === "development") {
|
883 | this.options.level = "debug";
|
884 | }
|
885 | }
|
886 | if (typeof options !== "undefined" && options !== null ) {
|
887 | this.options = options;
|
888 |
|
889 | Args.check(LogLevels.hasOwnProperty(this.options.level), "Invalid logging level. Expected error, warn, info, verbose or debug.");
|
890 | }
|
891 | }
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 | TraceLogger.prototype.level = function(level) {
|
898 | Args.check(LogLevels.hasOwnProperty(level), "Invalid logging level. Expected error, warn, info, verbose or debug.");
|
899 | this.options.level = level;
|
900 | return this;
|
901 | };
|
902 |
|
903 |
|
904 |
|
905 |
|
906 | TraceLogger.prototype.log = function(data) {
|
907 | var args = Array.prototype.slice.call(arguments);
|
908 | if (typeof data === 'undefined' || data === null) {
|
909 | return;
|
910 | }
|
911 | if (data instanceof Error) {
|
912 | return writeError.bind(this)("info",data);
|
913 | }
|
914 | if (typeof data !== 'string') {
|
915 | return this.write("info", data.toString());
|
916 | }
|
917 | if (args.length>1) {
|
918 | return this.write("info", sprintf.apply(null, args));
|
919 | }
|
920 | this.write("info", data);
|
921 | };
|
922 |
|
923 |
|
924 |
|
925 |
|
926 | TraceLogger.prototype.info = function(data) {
|
927 | var args = Array.prototype.slice.call(arguments);
|
928 | if (typeof data === 'undefined' || data === null) {
|
929 | return;
|
930 | }
|
931 | if (data instanceof Error) {
|
932 | return writeError.bind(this)("info",data);
|
933 | }
|
934 | if (typeof data !== 'string') {
|
935 | return this.write("info", data.toString());
|
936 | }
|
937 | if (args.length>1) {
|
938 | return this.write("info", sprintf.apply(null, args));
|
939 | }
|
940 | this.write("info", data);
|
941 | };
|
942 |
|
943 |
|
944 |
|
945 |
|
946 | TraceLogger.prototype.error = function(data) {
|
947 | var args = Array.prototype.slice.call(arguments);
|
948 | if (typeof data === 'undefined' || data === null) {
|
949 | return;
|
950 | }
|
951 | if (data instanceof Error) {
|
952 | return writeError.bind(this)("error",data);
|
953 | }
|
954 | if (typeof data !== 'string') {
|
955 | return this.write("error", data.toString());
|
956 | }
|
957 | if (args.length>1) {
|
958 | return this.write("error", sprintf.apply(null, args));
|
959 | }
|
960 | this.write("error", data);
|
961 | };
|
962 |
|
963 |
|
964 |
|
965 |
|
966 | TraceLogger.prototype.warn = function(data) {
|
967 | var args = Array.prototype.slice.call(arguments);
|
968 | if (typeof data === 'undefined' || data === null) {
|
969 | return;
|
970 | }
|
971 | if (data instanceof Error) {
|
972 | return writeError.bind(this)("warn",data);
|
973 | }
|
974 | if (typeof data !== 'string') {
|
975 | return this.write("warn", data.toString());
|
976 | }
|
977 | if (args.length>1) {
|
978 | return this.write("warn", sprintf.apply(null, args));
|
979 | }
|
980 | this.write("warn", data);
|
981 | };
|
982 |
|
983 |
|
984 |
|
985 |
|
986 | TraceLogger.prototype.verbose = function(data) {
|
987 | var args = Array.prototype.slice.call(arguments);
|
988 | if (typeof data === 'undefined' || data === null) {
|
989 | return;
|
990 | }
|
991 | if (data instanceof Error) {
|
992 | return writeError.bind(this)("verbose",data);
|
993 | }
|
994 | if (typeof data !== 'string') {
|
995 | return this.write("verbose", data.toString());
|
996 | }
|
997 | if (args.length>1) {
|
998 | return this.write("verbose", sprintf.apply(null, args));
|
999 | }
|
1000 | this.write("verbose", data);
|
1001 | };
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 | TraceLogger.prototype.debug = function(data) {
|
1007 | var args = Array.prototype.slice.call(arguments);
|
1008 | if (typeof data === 'undefined' || data === null) {
|
1009 | return;
|
1010 | }
|
1011 | if (data instanceof Error) {
|
1012 | return writeError.bind(this)("debug",data);
|
1013 | }
|
1014 | if (typeof data !== 'string') {
|
1015 | return this.write("debug", data.toString());
|
1016 | }
|
1017 | if (args.length>1) {
|
1018 | return this.write("debug", sprintf.apply(null, args));
|
1019 | }
|
1020 | this.write("debug", data);
|
1021 |
|
1022 | };
|
1023 |
|
1024 | TraceLogger.prototype.write = function(level, text) {
|
1025 | if (LogLevels[level]>LogLevels[this.options.level]) {
|
1026 | return;
|
1027 | }
|
1028 | if (this.options.colors) {
|
1029 |
|
1030 | console.log(LogLevelColors[level] + timestamp() + " [" + level.toUpperCase() + "] " + text, Reset);
|
1031 | } else {
|
1032 |
|
1033 | console.log(timestamp() + " [" + level.toUpperCase() + "] " + text);
|
1034 | }
|
1035 | };
|
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 | function Base26Number(value) {
|
1042 |
|
1043 | var thisValue = value;
|
1044 | this.toString = function() {
|
1045 | return Base26Number.toBase26(thisValue);
|
1046 | }
|
1047 | }
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 | Base26Number.toBase26 = function(x) {
|
1055 | var num = Math.floor(x | 0);
|
1056 | if (num<0) {
|
1057 | throw new Error("A non-positive integer cannot be converted to base-26 format.");
|
1058 | }
|
1059 | if (num>208827064575) {
|
1060 | throw new Error("A positive integer bigger than 208827064575 cannot be converted to base-26 format.");
|
1061 | }
|
1062 | var out = "";
|
1063 | var length= 1;
|
1064 | var a = "a".charCodeAt(0);
|
1065 | while(length<=8) {
|
1066 | out += String.fromCharCode(a + (num % 26));
|
1067 | num = Math.floor(num / 26);
|
1068 | length += 1;
|
1069 | }
|
1070 | return out;
|
1071 | };
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 | Base26Number.fromBase26 = function(s) {
|
1078 | var num = 0;
|
1079 | if (!/[a-z]{8}/.test(s)) {
|
1080 | throw new Error("Invalid base-26 format.");
|
1081 | }
|
1082 | var a = "a".charCodeAt(0);
|
1083 | for (var i = 7; i >=0; i--) {
|
1084 | num = (num * 26) + (s[i].charCodeAt(0) - a);
|
1085 | }
|
1086 | return num;
|
1087 | };
|
1088 |
|
1089 | var valueProperty = Symbol("value");
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 | function Guid(value) {
|
1096 | if (typeof value === "string") {
|
1097 | var test = value.replace(/^{/,"").replace(/{$/,"");
|
1098 | Args.check(GuidRegex.test(test),"Value must be a valid UUID");
|
1099 | this[valueProperty] = test;
|
1100 | return;
|
1101 | }
|
1102 | this[valueProperty] = TextUtils.newUUID();
|
1103 | }
|
1104 |
|
1105 | Guid.prototype.toJSON = function() {
|
1106 | return this[valueProperty];
|
1107 | };
|
1108 |
|
1109 | Guid.prototype.valueOf = function() {
|
1110 | return this[valueProperty];
|
1111 | };
|
1112 |
|
1113 | Guid.prototype.toString = function() {
|
1114 | return this[valueProperty];
|
1115 | };
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 | Guid.isGuid = function (s) {
|
1122 | if (s instanceof Guid) {
|
1123 | return true;
|
1124 | }
|
1125 | if (typeof s !== "string") {
|
1126 | return false;
|
1127 | }
|
1128 | return GuidRegex.test(s);
|
1129 | };
|
1130 |
|
1131 |
|
1132 |
|
1133 | Guid.newGuid = function() {
|
1134 | return new Guid();
|
1135 | };
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 | function ArgumentError(msg, code) {
|
1145 | ArgumentError.super_.bind(this)(msg);
|
1146 | this.message = msg;
|
1147 | this.code = code || "EARG";
|
1148 | if (typeof Error.captureStackTrace === 'function') {
|
1149 | Error.captureStackTrace(this, this.constructor);
|
1150 | }
|
1151 | }
|
1152 |
|
1153 | LangUtils.inherits(ArgumentError, TypeError);
|
1154 |
|
1155 | if (typeof exports !== 'undefined') {
|
1156 | module.exports.ArgumentError = ArgumentError;
|
1157 | module.exports.Args = Args;
|
1158 | module.exports.UnknownPropertyDescriptor = UnknownPropertyDescriptor;
|
1159 | module.exports.LangUtils = LangUtils;
|
1160 | module.exports.NumberUtils = NumberUtils;
|
1161 | module.exports.RandomUtils = RandomUtils;
|
1162 | module.exports.TraceUtils = TraceUtils;
|
1163 | module.exports.TextUtils = TextUtils;
|
1164 | module.exports.PathUtils = PathUtils;
|
1165 | module.exports.Base26Number = Base26Number;
|
1166 | module.exports.Guid = Guid;
|
1167 | }
|