Skip to content

Commit

Permalink
Rename System.global to global
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 27, 2016
1 parent 5026296 commit fa8503c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

[![browser support][testling-png]][testling-url]

An ECMAScript spec-compliant polyfill/shim for `System.global`. Invoke its "shim" method to shim System.global if it is unavailable.
An ECMAScript spec-compliant polyfill/shim for `global`. Invoke its "shim" method to shim `global` if it is unavailable.

This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec proposal](https://github.com/tc39/proposal-global).

Expand All @@ -34,23 +34,20 @@ assert.equal(global, getGlobal());
```

```js
/* when System or System.global is not present */
if (typeof System === 'object') { delete System.global; }
delete System;
/* when `global` is not present */
var shimmedGlobal = require('system.global').shim();
/* or */
var shimmedGlobal = require('system.global/shim')();

assert.equal(shimmedGlobal, global);
assert.equal(shimmedGlobal, System.global);
assert.equal(shimmedGlobal, getGlobal());
```

```js
/* when System.global is present */
/* when `global` is present */
var shimmedGlobal = require('system.global').shim();

assert.equal(shimmedGlobal, System.global);
assert.equal(shimmedGlobal, global);
assert.equal(shimmedGlobal, getGlobal());
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "system.global",
"version": "1.0.0",
"author": "Jordan Harband",
"description": "ECMAScript spec-compliant polyfill/shim for `System.global`",
"description": "ECMAScript spec-compliant polyfill/shim for `global`",
"license": "MIT",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
var implementation = require('./implementation');

module.exports = function getPolyfill() {
if (typeof System !== 'object' || !System.global || System.global.Math !== Math || System.global.Array !== Array) {
if (typeof global !== 'object' || !global || global.Math !== Math || global.Array !== Array) {
return implementation;
}
return System.global;
return global;
};
18 changes: 7 additions & 11 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
var define = require('define-properties');
var getPolyfill = require('./polyfill');

module.exports = function shimSystemAndGlobal() {
module.exports = function shimGlobal() {
var polyfill = getPolyfill();
define(
polyfill,
{ System: {} },
{ System: function () { return typeof System !== 'object'; } }
);
if (System.global !== polyfill) {
if (define.supportsDescriptors) {
Object.defineProperty(System, 'global', {
if (define.supportsDescriptors) {
var descriptor = Object.getOwnPropertyDescriptor(polyfill, 'global');
if (!descriptor || (descriptor.configurable && (descriptor.enumerable || descriptor.writable || global !== polyfill))) {
Object.defineProperty(polyfill, 'global', {
configurable: true,
enumerable: false,
value: polyfill,
writable: false
});
} else {
System.global = polyfill;
}
} else if (typeof global !== 'object' || global !== polyfill) {
polyfill.global = polyfill;
}
return polyfill;
};
8 changes: 4 additions & 4 deletions test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var isEnumerable = Object.prototype.propertyIsEnumerable;
var runTests = require('./tests');

test('native', function (t) {
t.equal(typeof System, 'object', 'System is an object');
t.equal(global in System, true, 'global is in System');
t.equal(typeof global, 'object', 'global is an object');
t.equal(global in global, true, 'global is in global');

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(System, 'global'), 'System.global is not enumerable');
et.equal(false, isEnumerable.call(global, 'global'), 'global is not enumerable');
et.end();
});

runTests(System.global, t);
runTests(global, t);

t.end();
});
12 changes: 6 additions & 6 deletions test/shimmed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ var isEnumerable = Object.prototype.propertyIsEnumerable;
var runTests = require('./tests');

test('shimmed', function (t) {
t.equal(typeof System, 'object', 'System is an object');
t.equal('global' in System, true, 'global is in System');
t.equal(typeof global, 'object', 'global is an object');
t.equal('global' in global, true, 'global is in global');

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(System, 'global'), 'System.global is not enumerable');
et.equal(false, isEnumerable.call(global, 'global'), 'global.global is not enumerable');
et.end();
});

t.test('writability', { skip: !defineProperties.supportsDescriptors }, function (wt) {
var desc = Object.getOwnPropertyDescriptor(System, 'global');
wt.equal(desc.writable, false, 'System.global is not writable');
var desc = Object.getOwnPropertyDescriptor(global, 'global');
wt.equal(desc.writable, false, 'global.global is not writable');
wt.end();
});

runTests(System.global, t);
runTests(global.global, t);

t.end();
});

0 comments on commit fa8503c

Please sign in to comment.