Skip to content

Commit eaa550b

Browse files
committed
Added Internet Explorer compatibility for JSON and toString.call
1 parent 3985d70 commit eaa550b

File tree

1 file changed

+333
-1
lines changed

1 file changed

+333
-1
lines changed

src/amplitude.js

Lines changed: 333 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Checks if an object is an array
1616
*/
1717
var isArray = Array.isArray || function(obj) {
18-
return toString.call(obj) === '[object Array]';
18+
return Object.prototype.toString.call(obj) === '[object Array]';
1919
};
2020

2121
/*
@@ -271,6 +271,338 @@
271271
}
272272
};
273273

274+
/*
275+
* JSON stringify/parse
276+
* https://github.com/douglascrockford/JSON-js
277+
* json2.js
278+
*/
279+
var JSON = window.JSON;
280+
if (typeof JSON !== 'object') {
281+
JSON = {};
282+
}
283+
284+
(function () {
285+
'use strict';
286+
287+
function f(n) {
288+
// Format integers to have at least two digits.
289+
return n < 10 ? '0' + n : n;
290+
}
291+
292+
if (typeof Date.prototype.toJSON !== 'function') {
293+
294+
Date.prototype.toJSON = function (key) {
295+
296+
return isFinite(this.valueOf())
297+
? this.getUTCFullYear() + '-' +
298+
f(this.getUTCMonth() + 1) + '-' +
299+
f(this.getUTCDate()) + 'T' +
300+
f(this.getUTCHours()) + ':' +
301+
f(this.getUTCMinutes()) + ':' +
302+
f(this.getUTCSeconds()) + 'Z'
303+
: null;
304+
};
305+
306+
String.prototype.toJSON =
307+
Number.prototype.toJSON =
308+
Boolean.prototype.toJSON = function (key) {
309+
return this.valueOf();
310+
};
311+
}
312+
313+
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
314+
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
315+
gap,
316+
indent,
317+
meta = { // table of character substitutions
318+
'\b': '\\b',
319+
'\t': '\\t',
320+
'\n': '\\n',
321+
'\f': '\\f',
322+
'\r': '\\r',
323+
'"' : '\\"',
324+
'\\': '\\\\'
325+
},
326+
rep;
327+
328+
329+
function quote(string) {
330+
331+
// If the string contains no control characters, no quote characters, and no
332+
// backslash characters, then we can safely slap some quotes around it.
333+
// Otherwise we must also replace the offending characters with safe escape
334+
// sequences.
335+
336+
escapable.lastIndex = 0;
337+
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
338+
var c = meta[a];
339+
return typeof c === 'string'
340+
? c
341+
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
342+
}) + '"' : '"' + string + '"';
343+
}
344+
345+
346+
function str(key, holder) {
347+
348+
// Produce a string from holder[key].
349+
350+
var i, // The loop counter.
351+
k, // The member key.
352+
v, // The member value.
353+
length,
354+
mind = gap,
355+
partial,
356+
value = holder[key];
357+
358+
// If the value has a toJSON method, call it to obtain a replacement value.
359+
360+
if (value && typeof value === 'object' &&
361+
typeof value.toJSON === 'function') {
362+
value = value.toJSON(key);
363+
}
364+
365+
// If we were called with a replacer function, then call the replacer to
366+
// obtain a replacement value.
367+
368+
if (typeof rep === 'function') {
369+
value = rep.call(holder, key, value);
370+
}
371+
372+
// What happens next depends on the value's type.
373+
374+
switch (typeof value) {
375+
case 'string':
376+
return quote(value);
377+
378+
case 'number':
379+
380+
// JSON numbers must be finite. Encode non-finite numbers as null.
381+
382+
return isFinite(value) ? String(value) : 'null';
383+
384+
case 'boolean':
385+
case 'null':
386+
387+
// If the value is a boolean or null, convert it to a string. Note:
388+
// typeof null does not produce 'null'. The case is included here in
389+
// the remote chance that this gets fixed someday.
390+
391+
return String(value);
392+
393+
// If the type is 'object', we might be dealing with an object or an array or
394+
// null.
395+
396+
case 'object':
397+
398+
// Due to a specification blunder in ECMAScript, typeof null is 'object',
399+
// so watch out for that case.
400+
401+
if (!value) {
402+
return 'null';
403+
}
404+
405+
// Make an array to hold the partial results of stringifying this object value.
406+
407+
gap += indent;
408+
partial = [];
409+
410+
// Is the value an array?
411+
412+
if (Object.prototype.toString.apply(value) === '[object Array]') {
413+
414+
// The value is an array. Stringify every element. Use null as a placeholder
415+
// for non-JSON values.
416+
417+
length = value.length;
418+
for (i = 0; i < length; i += 1) {
419+
partial[i] = str(i, value) || 'null';
420+
}
421+
422+
// Join all of the elements together, separated with commas, and wrap them in
423+
// brackets.
424+
425+
v = partial.length === 0
426+
? '[]'
427+
: gap
428+
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
429+
: '[' + partial.join(',') + ']';
430+
gap = mind;
431+
return v;
432+
}
433+
434+
// If the replacer is an array, use it to select the members to be stringified.
435+
436+
if (rep && typeof rep === 'object') {
437+
length = rep.length;
438+
for (i = 0; i < length; i += 1) {
439+
if (typeof rep[i] === 'string') {
440+
k = rep[i];
441+
v = str(k, value);
442+
if (v) {
443+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
444+
}
445+
}
446+
}
447+
} else {
448+
449+
// Otherwise, iterate through all of the keys in the object.
450+
451+
for (k in value) {
452+
if (Object.prototype.hasOwnProperty.call(value, k)) {
453+
v = str(k, value);
454+
if (v) {
455+
partial.push(quote(k) + (gap ? ': ' : ':') + v);
456+
}
457+
}
458+
}
459+
}
460+
461+
// Join all of the member texts together, separated with commas,
462+
// and wrap them in braces.
463+
464+
v = partial.length === 0
465+
? '{}'
466+
: gap
467+
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
468+
: '{' + partial.join(',') + '}';
469+
gap = mind;
470+
return v;
471+
}
472+
}
473+
474+
// If the JSON object does not yet have a stringify method, give it one.
475+
476+
if (typeof JSON.stringify !== 'function') {
477+
JSON.stringify = function (value, replacer, space) {
478+
479+
// The stringify method takes a value and an optional replacer, and an optional
480+
// space parameter, and returns a JSON text. The replacer can be a function
481+
// that can replace values, or an array of strings that will select the keys.
482+
// A default replacer method can be provided. Use of the space parameter can
483+
// produce text that is more easily readable.
484+
485+
var i;
486+
gap = '';
487+
indent = '';
488+
489+
// If the space parameter is a number, make an indent string containing that
490+
// many spaces.
491+
492+
if (typeof space === 'number') {
493+
for (i = 0; i < space; i += 1) {
494+
indent += ' ';
495+
}
496+
497+
// If the space parameter is a string, it will be used as the indent string.
498+
499+
} else if (typeof space === 'string') {
500+
indent = space;
501+
}
502+
503+
// If there is a replacer, it must be a function or an array.
504+
// Otherwise, throw an error.
505+
506+
rep = replacer;
507+
if (replacer && typeof replacer !== 'function' &&
508+
(typeof replacer !== 'object' ||
509+
typeof replacer.length !== 'number')) {
510+
throw new Error('JSON.stringify');
511+
}
512+
513+
// Make a fake root object containing our value under the key of ''.
514+
// Return the result of stringifying the value.
515+
516+
return str('', {'': value});
517+
};
518+
}
519+
520+
521+
// If the JSON object does not yet have a parse method, give it one.
522+
523+
if (typeof JSON.parse !== 'function') {
524+
JSON.parse = function (text, reviver) {
525+
526+
// The parse method takes a text and an optional reviver function, and returns
527+
// a JavaScript value if the text is a valid JSON text.
528+
529+
var j;
530+
531+
function walk(holder, key) {
532+
533+
// The walk method is used to recursively walk the resulting structure so
534+
// that modifications can be made.
535+
536+
var k, v, value = holder[key];
537+
if (value && typeof value === 'object') {
538+
for (k in value) {
539+
if (Object.prototype.hasOwnProperty.call(value, k)) {
540+
v = walk(value, k);
541+
if (v !== undefined) {
542+
value[k] = v;
543+
} else {
544+
delete value[k];
545+
}
546+
}
547+
}
548+
}
549+
return reviver.call(holder, key, value);
550+
}
551+
552+
553+
// Parsing happens in four stages. In the first stage, we replace certain
554+
// Unicode characters with escape sequences. JavaScript handles many characters
555+
// incorrectly, either silently deleting them, or treating them as line endings.
556+
557+
text = String(text);
558+
cx.lastIndex = 0;
559+
if (cx.test(text)) {
560+
text = text.replace(cx, function (a) {
561+
return '\\u' +
562+
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
563+
});
564+
}
565+
566+
// In the second stage, we run the text against regular expressions that look
567+
// for non-JSON patterns. We are especially concerned with '()' and 'new'
568+
// because they can cause invocation, and '=' because it can cause mutation.
569+
// But just to be safe, we want to reject all unexpected forms.
570+
571+
// We split the second stage into 4 regexp operations in order to work around
572+
// crippling inefficiencies in IE's and Safari's regexp engines. First we
573+
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
574+
// replace all simple value tokens with ']' characters. Third, we delete all
575+
// open brackets that follow a colon or comma or that begin the text. Finally,
576+
// we look to see that the remaining characters are only whitespace or ']' or
577+
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
578+
579+
if (/^[\],:{}\s]*$/
580+
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
581+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
582+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
583+
584+
// In the third stage we use the eval function to compile the text into a
585+
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
586+
// in JavaScript: it can begin a block or an object literal. We wrap the text
587+
// in parens to eliminate the ambiguity.
588+
589+
j = eval('(' + text + ')');
590+
591+
// In the optional fourth stage, we recursively walk the new structure, passing
592+
// each name/value pair to a reviver function for possible transformation.
593+
594+
return typeof reviver === 'function'
595+
? walk({'': j}, '')
596+
: j;
597+
}
598+
599+
// If the text is not JSON parseable, then a SyntaxError is thrown.
600+
601+
throw new SyntaxError('JSON.parse');
602+
};
603+
}
604+
}());
605+
274606
var userAgent = navigator.userAgent;
275607
var vendor = navigator.vendor;
276608
var platform = navigator.platform;

0 commit comments

Comments
 (0)