2022-08-02 15:24:19 +02:00
|
|
|
# es5-shim <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2022-08-02 15:24:19 +02:00
|
|
|
[![github actions][actions-image]][actions-url]
|
|
|
|
[![coverage][codecov-image]][codecov-url]
|
2016-05-16 13:33:49 +02:00
|
|
|
[![dependency status][deps-svg]][deps-url]
|
|
|
|
[![dev dependency status][dev-deps-svg]][dev-deps-url]
|
2022-08-02 15:24:19 +02:00
|
|
|
[![License][license-image]][license-url]
|
|
|
|
[![Downloads][downloads-image]][downloads-url]
|
|
|
|
|
|
|
|
[![npm badge][npm-badge-png]][package-url]
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
`es5-shim.js` and `es5-shim.min.js` monkey-patch a JavaScript context to
|
|
|
|
contain all EcmaScript 5 methods that can be faithfully emulated with a
|
|
|
|
legacy JavaScript engine.
|
2016-05-16 13:33:49 +02:00
|
|
|
**Note:** As `es5-shim.js` is designed to patch the native Javascript
|
|
|
|
engine, it should be the library that is loaded first.
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
`es5-sham.js` and `es5-sham.min.js` monkey-patch other ES5 methods as
|
|
|
|
closely as possible. For these methods, as closely as possible to ES5
|
|
|
|
is not very close. Many of these shams are intended only to allow code
|
|
|
|
to be written to ES5 without causing run-time errors in older engines.
|
|
|
|
In many cases, this means that these shams cause many ES5 methods to
|
|
|
|
silently fail. Decide carefully whether this is what you want.
|
2016-05-16 13:33:49 +02:00
|
|
|
**Note:** `es5-sham.js` requires `es5-shim.js` to be able to work properly.
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
|
|
|
The tests are written with the Jasmine BDD test framework.
|
2016-05-16 13:33:49 +02:00
|
|
|
To run the tests, navigate to <root-folder>/tests/ , or,
|
|
|
|
simply `npm install` and `npm test`.
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
## Shims
|
|
|
|
|
|
|
|
### Complete tests ###
|
|
|
|
|
2022-08-02 15:24:19 +02:00
|
|
|
* Array.prototype.every ([standalone shim](https://www.npmjs.com/package/array.prototype.every))
|
|
|
|
* Array.prototype.filter ([standalone shim](https://www.npmjs.com/package/array.prototype.filter))
|
|
|
|
* Array.prototype.forEach ([standalone shim](https://www.npmjs.com/package/array.prototype.foreach))
|
|
|
|
* Array.prototype.indexOf ([standalone shim](https://www.npmjs.com/package/array.prototype.indexof))
|
|
|
|
* Array.prototype.lastIndexOf ([standalone shim](https://www.npmjs.com/package/array.prototype.lastindexof))
|
|
|
|
* Array.prototype.map ([standalone shim](https://www.npmjs.com/package/array.prototype.map))
|
2016-05-16 13:33:49 +02:00
|
|
|
* Array.prototype.slice
|
2022-08-02 15:24:19 +02:00
|
|
|
* Array.prototype.some ([standalone shim](https://www.npmjs.com/package/array.prototype.some))
|
2016-05-16 13:33:49 +02:00
|
|
|
* Array.prototype.sort
|
2022-08-02 15:24:19 +02:00
|
|
|
* Array.prototype.reduce ([standalone shim](https://www.npmjs.com/package/array.prototype.reduce))
|
|
|
|
* Array.prototype.reduceRight ([standalone shim](https://www.npmjs.com/package/array.prototype.reduceright))
|
|
|
|
* Array.prototype.push ([standalone shim](https://www.npmjs.com/package/array.prototype.push))
|
2016-05-16 13:33:49 +02:00
|
|
|
* Array.prototype.join
|
2022-08-02 15:24:19 +02:00
|
|
|
* Array.prototype.splice ([standalone shim](https://www.npmjs.com/package/array.prototype.splice))
|
2014-05-12 20:08:19 +02:00
|
|
|
* Array.isArray
|
|
|
|
* Date.now
|
|
|
|
* Date.prototype.toJSON
|
|
|
|
* Function.prototype.bind
|
|
|
|
* :warning: Caveat: the bound function has a prototype property.
|
|
|
|
* :warning: Caveat: bound functions do not try too hard to keep you
|
|
|
|
from manipulating their ``arguments`` and ``caller`` properties.
|
|
|
|
* :warning: Caveat: bound functions don't have checks in ``call`` and
|
|
|
|
``apply`` to avoid executing as a constructor.
|
2022-08-02 15:24:19 +02:00
|
|
|
* Number.prototype.toExponential ([standalone shim](https://www.npmjs.com/package/number.prototype.toexponential))
|
2014-05-12 20:08:19 +02:00
|
|
|
* Number.prototype.toFixed
|
2016-05-16 13:33:49 +02:00
|
|
|
* Number.prototype.toPrecision
|
2022-08-02 15:24:19 +02:00
|
|
|
* Object.keys ([standalone shim](https://www.npmjs.com/package/object-keys))
|
|
|
|
* String.prototype.split ([standalone shim](https://www.npmjs.com/package/string.prototype.split))
|
|
|
|
* String.prototype.trim ([standalone shim](https://www.npmjs.com/package/string.prototype.trim))
|
|
|
|
* String.prototype.lastIndexOf ([standalone shim](https://string.prototype.lastindexof))
|
2014-05-12 20:08:19 +02:00
|
|
|
* String.prototype.replace
|
|
|
|
* Firefox (through v29) natively handles capturing groups incorrectly.
|
|
|
|
* Date.parse (for ISO parsing)
|
|
|
|
* Date.prototype.toISOString
|
|
|
|
* parseInt
|
2016-05-16 13:33:49 +02:00
|
|
|
* parseFloat
|
|
|
|
* Error.prototype.toString
|
|
|
|
* Error.prototype.name
|
|
|
|
* Error.prototype.message
|
|
|
|
* RegExp.prototype.toString
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
## Shams
|
|
|
|
|
|
|
|
* :warning: Object.create
|
|
|
|
|
|
|
|
For the case of simply "begetting" an object that inherits
|
|
|
|
prototypically from another, this should work fine across legacy
|
|
|
|
engines.
|
|
|
|
|
|
|
|
:warning: The second argument is passed to Object.defineProperties
|
2016-05-16 13:33:49 +02:00
|
|
|
which will probably fail either silently or with extreme prejudice.
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
* :warning: Object.getPrototypeOf
|
|
|
|
|
|
|
|
This will return "undefined" in some cases. It uses `__proto__` if
|
|
|
|
it's available. Failing that, it uses constructor.prototype, which
|
|
|
|
depends on the constructor property of the object's prototype having
|
|
|
|
not been replaced. If your object was created like this, it won't
|
|
|
|
work:
|
|
|
|
|
|
|
|
function Foo() {
|
|
|
|
}
|
|
|
|
Foo.prototype = {};
|
|
|
|
|
|
|
|
Because the prototype reassignment destroys the constructor
|
|
|
|
property.
|
|
|
|
|
|
|
|
This will work for all objects that were created using
|
|
|
|
`Object.create` implemented with this library.
|
|
|
|
|
|
|
|
* :warning: Object.getOwnPropertyNames
|
|
|
|
|
|
|
|
This method uses Object.keys, so it will not be accurate on legacy
|
|
|
|
engines.
|
|
|
|
|
|
|
|
* Object.isSealed
|
|
|
|
|
|
|
|
Returns "false" in all legacy engines for all objects, which is
|
|
|
|
conveniently guaranteed to be accurate.
|
|
|
|
|
|
|
|
* Object.isFrozen
|
|
|
|
|
|
|
|
Returns "false" in all legacy engines for all objects, which is
|
|
|
|
conveniently guaranteed to be accurate.
|
|
|
|
|
|
|
|
* Object.isExtensible
|
|
|
|
|
|
|
|
Works like a charm, by trying very hard to extend the object then
|
|
|
|
redacting the extension.
|
|
|
|
|
|
|
|
### May fail
|
|
|
|
|
|
|
|
* :warning: Object.getOwnPropertyDescriptor
|
|
|
|
|
|
|
|
The behavior of this shim does not conform to ES5. It should
|
|
|
|
probably not be used at this time, until its behavior has been
|
|
|
|
reviewed and been confirmed to be useful in legacy engines.
|
|
|
|
|
|
|
|
* :warning: Object.defineProperty
|
|
|
|
|
|
|
|
In the worst of circumstances, IE 8 provides a version of this
|
|
|
|
method that only works on DOM objects. This sham will not be
|
|
|
|
installed. The given version of `defineProperty` will throw an
|
|
|
|
exception if used on non-DOM objects.
|
|
|
|
|
|
|
|
In slightly better circumstances, this method will silently fail to
|
|
|
|
set "writable", "enumerable", and "configurable" properties.
|
|
|
|
|
|
|
|
Providing a getter or setter with "get" or "set" on a descriptor
|
|
|
|
will silently fail on engines that lack "__defineGetter__" and
|
|
|
|
"__defineSetter__", which include all versions of IE.
|
|
|
|
|
|
|
|
https://github.com/es-shims/es5-shim/issues#issue/5
|
|
|
|
|
|
|
|
* :warning: Object.defineProperties
|
|
|
|
|
2016-05-16 13:33:49 +02:00
|
|
|
This uses the Object.defineProperty shim.
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
* Object.seal
|
|
|
|
|
|
|
|
Silently fails on all legacy engines. This should be
|
|
|
|
fine unless you are depending on the safety and security
|
|
|
|
provisions of this method, which you cannot possibly
|
|
|
|
obtain in legacy engines.
|
|
|
|
|
|
|
|
* Object.freeze
|
|
|
|
|
|
|
|
Silently fails on all legacy engines. This should be
|
|
|
|
fine unless you are depending on the safety and security
|
|
|
|
provisions of this method, which you cannot possibly
|
|
|
|
obtain in legacy engines.
|
|
|
|
|
|
|
|
* Object.preventExtensions
|
|
|
|
|
|
|
|
Silently fails on all legacy engines. This should be
|
|
|
|
fine unless you are depending on the safety and security
|
|
|
|
provisions of this method, which you cannot possibly
|
|
|
|
obtain in legacy engines.
|
|
|
|
|
2022-08-02 15:24:19 +02:00
|
|
|
### Example of applying ES compatibility shims in a browser project
|
2016-05-16 13:33:49 +02:00
|
|
|
|
|
|
|
```html
|
2022-08-02 15:24:19 +02:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.14/es5-shim.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.14/es5-sham.min.js"></script>
|
2016-05-16 13:33:49 +02:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
|
2022-08-02 15:24:19 +02:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.5/es6-shim.min.js"></script>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.5/es6-sham.min.js"></script>
|
2016-05-16 13:33:49 +02:00
|
|
|
<script src="other-libs.js"></script>
|
|
|
|
```
|
2022-08-02 15:24:19 +02:00
|
|
|
|
|
|
|
[package-url]: https://npmjs.org/package/es5-shim
|
|
|
|
[npm-version-svg]: https://versionbadg.es/es-shims/es5-shim.svg
|
2016-05-16 13:33:49 +02:00
|
|
|
[deps-svg]: https://david-dm.org/es-shims/es5-shim.svg
|
|
|
|
[deps-url]: https://david-dm.org/es-shims/es5-shim
|
|
|
|
[dev-deps-svg]: https://david-dm.org/es-shims/es5-shim/dev-status.svg
|
|
|
|
[dev-deps-url]: https://david-dm.org/es-shims/es5-shim#info=devDependencies
|
|
|
|
[npm-badge-png]: https://nodei.co/npm/es5-shim.png?downloads=true&stars=true
|
2022-08-02 15:24:19 +02:00
|
|
|
[license-image]: https://img.shields.io/npm/l/es5-shim.svg
|
|
|
|
[license-url]: LICENSE
|
|
|
|
[downloads-image]: https://img.shields.io/npm/dm/es5-shim.svg
|
|
|
|
[downloads-url]: https://npm-stat.com/charts.html?package=es5-shim
|
|
|
|
[codecov-image]: https://codecov.io/gh/es-shims/es5-shim/branch/main/graphs/badge.svg
|
|
|
|
[codecov-url]: https://app.codecov.io/gh/es-shims/es5-shim/
|
|
|
|
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/es5-shim
|
|
|
|
[actions-url]: https://github.com/es-shims/es5-shim/actions
|