Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / util.js
1
2 /**
3  * Status of page load.
4  */
5
6 var pageLoaded = false;
7
8 /**
9  * Global reference.
10  */
11
12 var global = 'undefined' != typeof window ? window : global;
13
14 /**
15  * Inheritance.
16  *
17  * @param {Function} ctor a
18  * @param {Function} ctor b
19  * @api private
20  */
21
22 exports.inherits = function inherits (a, b) {
23   function c () { }
24   c.prototype = b.prototype;
25   a.prototype = new c;
26 };
27
28 /**
29  * Object.keys
30  */
31
32 exports.keys = Object.keys || function (obj) {
33   var ret = [];
34   var has = Object.prototype.hasOwnProperty;
35
36   for (var i in obj) {
37     if (has.call(obj, i)) {
38       ret.push(i);
39     }
40   }
41
42   return ret;
43 };
44
45 /**
46  * Adds an event.
47  *
48  * @api private
49  */
50
51 exports.on = function (element, event, fn, capture) {
52   if (element.attachEvent) {
53     element.attachEvent('on' + event, fn);
54   } else if (element.addEventListener) {
55     element.addEventListener(event, fn, capture);
56   }
57 };
58
59 /**
60  * Load utility.
61  *
62  * @api private
63  */
64
65 exports.load = function (fn) {
66   if (global.document && document.readyState === 'complete' || pageLoaded) {
67     return fn();
68   }
69
70   exports.on(global, 'load', fn, false);
71 };
72
73 /**
74  * Change the internal pageLoaded value.
75  */
76
77 if ('undefined' != typeof window) {
78   exports.load(function () {
79     pageLoaded = true;
80   });
81 }
82
83 /**
84  * Defers a function to ensure a spinner is not displayed by the browser.
85  *
86  * @param {Function} fn
87  * @api private
88  */
89
90 exports.defer = function (fn) {
91   if (!exports.ua.webkit || 'undefined' != typeof importScripts) {
92     return fn();
93   }
94
95   exports.load(function () {
96     setTimeout(fn, 100);
97   });
98 };
99
100 /**
101  * JSON parse.
102  *
103  * @see Based on jQuery#parseJSON (MIT) and JSON2
104  * @api private
105  */
106
107 var rvalidchars = /^[\],:{}\s]*$/
108   , rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g
109   , rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g
110   , rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g
111   , rtrimLeft = /^\s+/
112   , rtrimRight = /\s+$/
113
114 exports.parseJSON = function (data) {
115   if ('string' != typeof data || !data) {
116     return null;
117   }
118
119   data = data.replace(rtrimLeft, '').replace(rtrimRight, '');
120
121   // Attempt to parse using the native JSON parser first
122   if (global.JSON && JSON.parse) {
123     return JSON.parse(data);
124   }
125
126   if (rvalidchars.test(data.replace(rvalidescape, '@')
127       .replace(rvalidtokens, ']')
128       .replace(rvalidbraces, ''))) {
129     return (new Function('return ' + data))();
130   }
131 };
132
133 /**
134  * UA / engines detection namespace.
135  *
136  * @namespace
137  */
138
139 exports.ua = {};
140
141 /**
142  * Whether the UA supports CORS for XHR.
143  *
144  * @api private
145  */
146
147 exports.ua.hasCORS = 'undefined' != typeof XMLHttpRequest && (function () {
148   try {
149     var a = new XMLHttpRequest();
150   } catch (e) {
151     return false;
152   }
153
154   return a.withCredentials != undefined;
155 })();
156
157 /**
158  * Detect webkit.
159  *
160  * @api private
161  */
162
163 exports.ua.webkit = 'undefined' != typeof navigator &&
164   /webkit/i.test(navigator.userAgent);
165
166 /**
167  * Detect gecko.
168  *
169  * @api private
170  */
171
172 exports.ua.gecko = 'undefined' != typeof navigator &&
173   /gecko/i.test(navigator.userAgent);
174
175 /**
176  * Detect android;
177  */
178
179 exports.ua.android = 'undefined' != typeof navigator &&
180   /android/i.test(navigator.userAgent);
181
182 /**
183  * Detect iOS.
184  */
185
186 exports.ua.ios = 'undefined' != typeof navigator &&
187   /^(iPad|iPhone|iPod)$/.test(navigator.platform);
188 exports.ua.ios6 = exports.ua.ios && /OS 6_/.test(navigator.userAgent);
189
190 /**
191  * XHR request helper.
192  *
193  * @param {Boolean} whether we need xdomain
194  * @api private
195  */
196
197 exports.request = function request (xdomain) {
198   if ('undefined' != typeof process) {
199     var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
200     return new XMLHttpRequest();
201   }
202
203   if (xdomain && 'undefined' != typeof XDomainRequest && !exports.ua.hasCORS) {
204     return new XDomainRequest();
205   }
206
207   // XMLHttpRequest can be disabled on IE
208   try {
209     if ('undefined' != typeof XMLHttpRequest && (!xdomain || exports.ua.hasCORS)) {
210       return new XMLHttpRequest();
211     }
212   } catch (e) { }
213
214   if (!xdomain) {
215     try {
216       return new ActiveXObject('Microsoft.XMLHTTP');
217     } catch(e) { }
218   }
219 };
220
221 /**
222  * Parses an URI
223  *
224  * @author Steven Levithan <stevenlevithan.com> (MIT license)
225  * @api private
226  */
227
228 var re = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
229
230 var parts = [
231     'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host'
232   , 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
233 ];
234
235 exports.parseUri = function (str) {
236   var m = re.exec(str || '')
237     , uri = {}
238     , i = 14;
239
240   while (i--) {
241     uri[parts[i]] = m[i] || '';
242   }
243
244   return uri;
245 };
246
247 /**
248  * Compiles a querystring
249  *
250  * @param {Object}
251  * @api private
252  */
253
254 exports.qs = function (obj) {
255   var str = '';
256
257   for (var i in obj) {
258     if (obj.hasOwnProperty(i)) {
259       if (str.length) str += '&';
260       str += i + '=' + encodeURIComponent(obj[i]);
261     }
262   }
263
264   return str;
265 };