diff --git a/browserShim.js b/browserShim.js new file mode 100644 index 0000000..6028a9c --- /dev/null +++ b/browserShim.js @@ -0,0 +1,5 @@ +'use strict'; + +var systemGlobal = require('./'); + +module.exports = systemGlobal.shim(); diff --git a/implementation.js b/implementation.js new file mode 100644 index 0000000..511fc86 --- /dev/null +++ b/implementation.js @@ -0,0 +1,14 @@ +/* globals self, window, global */ +/* eslint no-negated-condition: 0, no-new-func: 0 */ + +'use strict'; + +if (typeof self !== 'undefined') { + module.exports = self; +} else if (typeof window !== 'undefined') { + module.exports = window; +} else if (typeof global !== 'undefined') { + module.exports = global; +} else { + module.exports = Function('return this')(); +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..7c73cef --- /dev/null +++ b/index.js @@ -0,0 +1,19 @@ +'use strict'; + +var defineProperties = require('define-properties'); + +var implementation = require('./implementation'); +var getPolyfill = require('./polyfill'); +var shim = require('./shim'); + +var polyfill = getPolyfill(); + +var getGlobal = function () { return polyfill; }; + +defineProperties(getGlobal, { + getPolyfill: getPolyfill, + implementation: implementation, + shim: shim +}); + +module.exports = getGlobal; diff --git a/polyfill.js b/polyfill.js new file mode 100644 index 0000000..725de1c --- /dev/null +++ b/polyfill.js @@ -0,0 +1,10 @@ +'use strict'; + +var implementation = require('./implementation'); + +module.exports = function getPolyfill() { + if (typeof System !== 'object' || !System.global || System.global.Math !== Math || System.global.Array !== Array) { + return implementation; + } + return System.global; +}; diff --git a/shim.js b/shim.js new file mode 100644 index 0000000..af7c671 --- /dev/null +++ b/shim.js @@ -0,0 +1,26 @@ +'use strict'; + +var define = require('define-properties'); +var getPolyfill = require('./polyfill'); + +module.exports = function shimSystemAndGlobal() { + var polyfill = getPolyfill(); + define( + polyfill, + { System: {} }, + { System: function () { return typeof System !== 'object'; } } + ); + if (System.global !== polyfill) { + if (define.supportsDescriptors) { + Object.defineProperty(System, 'global', { + configurable: true, + enumerable: false, + value: polyfill, + writable: false + }); + } else { + System.global = polyfill; + } + } + return polyfill; +};