1 | # globalThis <sup>[![Version Badge][npm-version-svg]][npm-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![dependency status][deps-svg]][deps-url]
|
---|
6 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
7 | [![License][license-image]][license-url]
|
---|
8 | [![Downloads][downloads-image]][downloads-url]
|
---|
9 |
|
---|
10 | [![npm badge][npm-badge-png]][npm-url]
|
---|
11 |
|
---|
12 | An ECMAScript spec-compliant polyfill/shim for `globalThis`. Invoke its "shim" method to shim `globalThis` if it is unavailable.
|
---|
13 |
|
---|
14 | 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).
|
---|
15 |
|
---|
16 | Most common usage:
|
---|
17 | ```js
|
---|
18 | var globalThis = require('globalthis')(); // returns native globalThis if compliant
|
---|
19 | /* or */
|
---|
20 | var globalThis = require('globalthis/polyfill')(); // returns native globalThis if compliant
|
---|
21 | ```
|
---|
22 |
|
---|
23 | ## Example
|
---|
24 |
|
---|
25 | ```js
|
---|
26 | var assert = require('assert');
|
---|
27 |
|
---|
28 | // the below function is not CSP-compliant, but reliably gets the
|
---|
29 | // global object in sloppy mode in every engine.
|
---|
30 | var getGlobal = Function('return this');
|
---|
31 |
|
---|
32 | assert.equal(globalThis, getGlobal());
|
---|
33 | ```
|
---|
34 |
|
---|
35 | ```js
|
---|
36 | /* when `globalThis` is not present */
|
---|
37 | var shimmedGlobal = require('globalthis').shim();
|
---|
38 | /* or */
|
---|
39 | var shimmedGlobal = require('globalthis/shim')();
|
---|
40 |
|
---|
41 | assert.equal(shimmedGlobal, globalThis);
|
---|
42 | assert.equal(shimmedGlobal, getGlobal());
|
---|
43 | ```
|
---|
44 |
|
---|
45 | ```js
|
---|
46 | /* when `globalThis` is present */
|
---|
47 | var shimmedGlobal = require('globalthis').shim();
|
---|
48 |
|
---|
49 | assert.equal(shimmedGlobal, globalThis);
|
---|
50 | assert.equal(shimmedGlobal, getGlobal());
|
---|
51 | ```
|
---|
52 |
|
---|
53 | ## Tests
|
---|
54 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
55 |
|
---|
56 | [npm-url]: https://npmjs.org/package/globalthis
|
---|
57 | [npm-version-svg]: https://versionbadg.es/ljharb/globalThis.svg
|
---|
58 | [deps-svg]: https://david-dm.org/ljharb/globalThis.svg?theme=shields.io
|
---|
59 | [deps-url]: https://david-dm.org/ljharb/globalThis
|
---|
60 | [dev-deps-svg]: https://david-dm.org/ljharb/globalThis/dev-status.svg?theme=shields.io
|
---|
61 | [dev-deps-url]: https://david-dm.org/ljharb/globalThis#info=devDependencies
|
---|
62 | [npm-badge-png]: https://nodei.co/npm/globalthis.png?downloads=true&stars=true
|
---|
63 | [license-image]: https://img.shields.io/npm/l/globalthis.svg
|
---|
64 | [license-url]: LICENSE
|
---|
65 | [downloads-image]: https://img.shields.io/npm/dm/globalthis.svg
|
---|
66 | [downloads-url]: https://npm-stat.com/charts.html?package=globalthis
|
---|
67 | [codecov-image]: https://codecov.io/gh/es-shims/globalThis/branch/main/graphs/badge.svg
|
---|
68 | [codecov-url]: https://app.codecov.io/gh/es-shims/globalThis/
|
---|
69 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/globalThis
|
---|
70 | [actions-url]: https://github.com/es-shims/globalThis/actions
|
---|