Skip to content

Commit 52eb25e

Browse files
committed
Consistently get global across platforms
1 parent dd89abe commit 52eb25e

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

javascript-stringify.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,10 @@
4747
*
4848
* @return {String}
4949
*/
50-
var getGlobalVariable = function () {
51-
return 'global';
50+
var getGlobalVariable = function (value, indent, stringify) {
51+
return 'Function(' + stringify('return this;') + ')()';
5252
};
5353

54-
/* istanbul ignore next */
55-
if (typeof window === 'object' && typeof window.document === 'object') {
56-
// Support browser environments.
57-
getGlobalVariable = function () {
58-
return 'window';
59-
};
60-
}
61-
62-
/* istanbul ignore next */
63-
if (typeof self === 'object' && typeof self.importScripts === 'function') {
64-
// Support web worker environments.
65-
getGlobalVariable = function () {
66-
return 'self';
67-
};
68-
}
69-
7054
/**
7155
* Convert JavaScript objects into strings.
7256
*

test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ describe('javascript-stringify', function () {
1414
it('number', test(10, '10'));
1515
it('array', test([1, 2, 3], '[1,2,3]'));
1616
it('object', test({ key: 'value', '-': 10 }, '{key:\'value\',\'-\':10}'));
17-
it('global', test(global, 'global'));
1817

1918
it('NaN', test(NaN, 'NaN'));
2019
it('Infinity', test(Infinity, 'Infinity'));
@@ -24,6 +23,10 @@ describe('javascript-stringify', function () {
2423
it('Number', test(new Number(10), 'new Number(10)'));
2524
it('String', test(new String('abc'), 'new String(\'abc\')'));
2625
it('Boolean', test(new Boolean(true), 'new Boolean(true)'));
26+
27+
it('global', function () {
28+
expect(eval(stringify(global))).to.equal(global);
29+
});
2730
});
2831

2932
describe('circular references', function () {

0 commit comments

Comments
 (0)