1 | # es5-shim <sup>[![Version Badge][npm-version-svg]][package-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]][package-url]
|
11 |
|
12 | `es5-shim.js` and `es5-shim.min.js` monkey-patch a JavaScript context to
|
13 | contain all EcmaScript 5 methods that can be faithfully emulated with a
|
14 | legacy JavaScript engine.
|
15 | **Note:** As `es5-shim.js` is designed to patch the native Javascript
|
16 | engine, it should be the library that is loaded first.
|
17 |
|
18 | `es5-sham.js` and `es5-sham.min.js` monkey-patch other ES5 methods as
|
19 | closely as possible. For these methods, as closely as possible to ES5
|
20 | is not very close. Many of these shams are intended only to allow code
|
21 | to be written to ES5 without causing run-time errors in older engines.
|
22 | In many cases, this means that these shams cause many ES5 methods to
|
23 | silently fail. Decide carefully whether this is what you want.
|
24 | **Note:** `es5-sham.js` requires `es5-shim.js` to be able to work properly.
|
25 |
|
26 | ## Tests
|
27 |
|
28 | The tests are written with the Jasmine BDD test framework.
|
29 | To run the tests, navigate to <root-folder>/tests/ , or,
|
30 | simply `npm install` and `npm test`.
|
31 |
|
32 | ## Shims
|
33 |
|
34 | ### Complete tests ###
|
35 |
|
36 | * Array.prototype.every ([standalone shim](https://www.npmjs.com/package/array.prototype.every))
|
37 | * Array.prototype.filter ([standalone shim](https://www.npmjs.com/package/array.prototype.filter))
|
38 | * Array.prototype.forEach ([standalone shim](https://www.npmjs.com/package/array.prototype.foreach))
|
39 | * Array.prototype.indexOf ([standalone shim](https://www.npmjs.com/package/array.prototype.indexof))
|
40 | * Array.prototype.lastIndexOf ([standalone shim](https://www.npmjs.com/package/array.prototype.lastindexof))
|
41 | * Array.prototype.map ([standalone shim](https://www.npmjs.com/package/array.prototype.map))
|
42 | * Array.prototype.slice
|
43 | * Array.prototype.some ([standalone shim](https://www.npmjs.com/package/array.prototype.some))
|
44 | * Array.prototype.sort
|
45 | * Array.prototype.reduce ([standalone shim](https://www.npmjs.com/package/array.prototype.reduce))
|
46 | * Array.prototype.reduceRight ([standalone shim](https://www.npmjs.com/package/array.prototype.reduceright))
|
47 | * Array.prototype.push ([standalone shim](https://www.npmjs.com/package/array.prototype.push))
|
48 | * Array.prototype.join
|
49 | * Array.prototype.splice ([standalone shim](https://www.npmjs.com/package/array.prototype.splice))
|
50 | * Array.isArray
|
51 | * Date.now
|
52 | * Date.prototype.toJSON
|
53 | * Function.prototype.bind
|
54 | * :warning: Caveat: the bound function has a prototype property.
|
55 | * :warning: Caveat: bound functions do not try too hard to keep you
|
56 | from manipulating their ``arguments`` and ``caller`` properties.
|
57 | * :warning: Caveat: bound functions don't have checks in ``call`` and
|
58 | ``apply`` to avoid executing as a constructor.
|
59 | * Number.prototype.toExponential ([standalone shim](https://www.npmjs.com/package/number.prototype.toexponential))
|
60 | * Number.prototype.toFixed
|
61 | * Number.prototype.toPrecision
|
62 | * Object.keys ([standalone shim](https://www.npmjs.com/package/object-keys))
|
63 | * String.prototype.split ([standalone shim](https://www.npmjs.com/package/string.prototype.split))
|
64 | * String.prototype.trim ([standalone shim](https://www.npmjs.com/package/string.prototype.trim))
|
65 | * String.prototype.lastIndexOf ([standalone shim](https://string.prototype.lastindexof))
|
66 | * String.prototype.replace
|
67 | * Firefox (through v29) natively handles capturing groups incorrectly.
|
68 | * Date.parse (for ISO parsing)
|
69 | * Date.prototype.toISOString
|
70 | * parseInt
|
71 | * parseFloat
|
72 | * Error.prototype.toString
|
73 | * Error.prototype.name
|
74 | * Error.prototype.message
|
75 | * RegExp.prototype.toString
|
76 |
|
77 | ## Shams
|
78 |
|
79 | * :warning: Object.create
|
80 |
|
81 | For the case of simply "begetting" an object that inherits
|
82 | prototypically from another, this should work fine across legacy
|
83 | engines.
|
84 |
|
85 | :warning: The second argument is passed to Object.defineProperties
|
86 | which will probably fail either silently or with extreme prejudice.
|
87 |
|
88 | * :warning: Object.getPrototypeOf
|
89 |
|
90 | This will return "undefined" in some cases. It uses `__proto__` if
|
91 | it's available. Failing that, it uses constructor.prototype, which
|
92 | depends on the constructor property of the object's prototype having
|
93 | not been replaced. If your object was created like this, it won't
|
94 | work:
|
95 |
|
96 | function Foo() {
|
97 | }
|
98 | Foo.prototype = {};
|
99 |
|
100 | Because the prototype reassignment destroys the constructor
|
101 | property.
|
102 |
|
103 | This will work for all objects that were created using
|
104 | `Object.create` implemented with this library.
|
105 |
|
106 | * :warning: Object.getOwnPropertyNames
|
107 |
|
108 | This method uses Object.keys, so it will not be accurate on legacy
|
109 | engines.
|
110 |
|
111 | * Object.isSealed
|
112 |
|
113 | Returns "false" in all legacy engines for all objects, which is
|
114 | conveniently guaranteed to be accurate.
|
115 |
|
116 | * Object.isFrozen
|
117 |
|
118 | Returns "false" in all legacy engines for all objects, which is
|
119 | conveniently guaranteed to be accurate.
|
120 |
|
121 | * Object.isExtensible
|
122 |
|
123 | Works like a charm, by trying very hard to extend the object then
|
124 | redacting the extension.
|
125 |
|
126 | ### May fail
|
127 |
|
128 | * :warning: Object.getOwnPropertyDescriptor
|
129 |
|
130 | The behavior of this shim does not conform to ES5. It should
|
131 | probably not be used at this time, until its behavior has been
|
132 | reviewed and been confirmed to be useful in legacy engines.
|
133 |
|
134 | * :warning: Object.defineProperty
|
135 |
|
136 | In the worst of circumstances, IE 8 provides a version of this
|
137 | method that only works on DOM objects. This sham will not be
|
138 | installed. The given version of `defineProperty` will throw an
|
139 | exception if used on non-DOM objects.
|
140 |
|
141 | In slightly better circumstances, this method will silently fail to
|
142 | set "writable", "enumerable", and "configurable" properties.
|
143 |
|
144 | Providing a getter or setter with "get" or "set" on a descriptor
|
145 | will silently fail on engines that lack "__defineGetter__" and
|
146 | "__defineSetter__", which include all versions of IE.
|
147 |
|
148 | https://github.com/es-shims/es5-shim/issues#issue/5
|
149 |
|
150 | * :warning: Object.defineProperties
|
151 |
|
152 | This uses the Object.defineProperty shim.
|
153 |
|
154 | * Object.seal
|
155 |
|
156 | Silently fails on all legacy engines. This should be
|
157 | fine unless you are depending on the safety and security
|
158 | provisions of this method, which you cannot possibly
|
159 | obtain in legacy engines.
|
160 |
|
161 | * Object.freeze
|
162 |
|
163 | Silently fails on all legacy engines. This should be
|
164 | fine unless you are depending on the safety and security
|
165 | provisions of this method, which you cannot possibly
|
166 | obtain in legacy engines.
|
167 |
|
168 | * Object.preventExtensions
|
169 |
|
170 | Silently fails on all legacy engines. This should be
|
171 | fine unless you are depending on the safety and security
|
172 | provisions of this method, which you cannot possibly
|
173 | obtain in legacy engines.
|
174 |
|
175 | ### Example of applying ES compatibility shims in a browser project
|
176 |
|
177 | ```html
|
178 | <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.14/es5-shim.min.js"></script>
|
179 | <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.14/es5-sham.min.js"></script>
|
180 | <script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
|
181 | <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.5/es6-shim.min.js"></script>
|
182 | <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.5/es6-sham.min.js"></script>
|
183 | <script src="other-libs.js"></script>
|
184 | ```
|
185 |
|
186 | [package-url]: https://npmjs.org/package/es5-shim
|
187 | [npm-version-svg]: https://versionbadg.es/es-shims/es5-shim.svg
|
188 | [deps-svg]: https://david-dm.org/es-shims/es5-shim.svg
|
189 | [deps-url]: https://david-dm.org/es-shims/es5-shim
|
190 | [dev-deps-svg]: https://david-dm.org/es-shims/es5-shim/dev-status.svg
|
191 | [dev-deps-url]: https://david-dm.org/es-shims/es5-shim#info=devDependencies
|
192 | [npm-badge-png]: https://nodei.co/npm/es5-shim.png?downloads=true&stars=true
|
193 | [license-image]: https://img.shields.io/npm/l/es5-shim.svg
|
194 | [license-url]: LICENSE
|
195 | [downloads-image]: https://img.shields.io/npm/dm/es5-shim.svg
|
196 | [downloads-url]: https://npm-stat.com/charts.html?package=es5-shim
|
197 | [codecov-image]: https://codecov.io/gh/es-shims/es5-shim/branch/main/graphs/badge.svg
|
198 | [codecov-url]: https://app.codecov.io/gh/es-shims/es5-shim/
|
199 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/es5-shim
|
200 | [actions-url]: https://github.com/es-shims/es5-shim/actions
|