• Stars
    star
    7,118
  • Rank 5,475 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 14 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines

es5-shim Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

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. Note: As es5-shim.js is designed to patch the native Javascript engine, it should be the library that is loaded first.

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. Note: es5-sham.js requires es5-shim.js to be able to work properly.

Tests

The tests are written with the Jasmine BDD test framework. To run the tests, navigate to /tests/ , or, simply npm install and npm test.

Shims

Complete tests

Shams

  • ⚠️ Object.create

    For the case of simply "begetting" an object that inherits prototypically from another, this should work fine across legacy engines.

    ⚠️ The second argument is passed to Object.defineProperties which will probably fail either silently or with extreme prejudice.

  • ⚠️ 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.

  • ⚠️ 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

  • ⚠️ 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.

  • ⚠️ 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

  • ⚠️ Object.defineProperties

    This uses the Object.defineProperty shim.

  • 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.

Example of applying ES compatibility shims in a browser project

<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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<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>
<script src="other-libs.js"></script>

More Repositories

1

es7-shim

ECMAScript 7 compatibility shims for legacy JavaScript engines
JavaScript
172
star
2

globalThis

ECMAScript spec-compliant polyfill/shim for `globalThis`.
JavaScript
154
star
3

Promise.prototype.finally

ES Proposal spec-compliant shim for Promise.prototype.finally
JavaScript
138
star
4

Promise.allSettled

ES Proposal spec-compliant shim for Promise.allSettled
JavaScript
95
star
5

Object.values

ES2017 spec-compliant shim for Object.values
JavaScript
72
star
6

es-shim-api

API contract, and rudimentary tests, for modules that conform to the *es-shim API*
JavaScript
71
star
7

Object.entries

ES2017 spec-compliant shim for Object.entries
JavaScript
56
star
8

array-includes

Array.prototype.includes spec-compliant polyfill
JavaScript
43
star
9

regexp.escape

An ESnext spec-compliant `RegExp.escape` shim/polyfill/replacement that works as far down as ES3.
JavaScript
35
star
10

Object.fromEntries

Official ES spec-compliant polyfill for Object.fromEntries
JavaScript
34
star
11

String.prototype.replaceAll

Spec-compliant polyfill for String.prototype.replaceAll ESnext proposal.
JavaScript
33
star
12

Array.prototype.flatMap

An ESnext spec-compliant `Array.prototype.flatMap` shim/polyfill/replacement that works as far down as ES3.
JavaScript
32
star
13

Array.prototype.flat

An ESnext spec-compliant `Array.prototype.flat` shim/polyfill/replacement that works as far down as ES3.
JavaScript
31
star
14

Array.prototype.group

An ESnext spec-compliant `Array.prototype.group` shim/polyfill/replacement that works as far down as ES3.
JavaScript
27
star
15

Array.prototype.at

An ES-spec-compliant (proposed) `Array.prototype.at`shim/polyfill/replacement that works as far down as ES3.
JavaScript
27
star
16

object-is

ES6-compliant shim for Object.is - differentiates between -0 and +0, and can compare to NaN.
JavaScript
25
star
17

get-own-property-symbols

ES6 Object.getOwnPropertySymbols partial polyfill
JavaScript
23
star
18

String.prototype.padStart

ES spec-compliant shim for String.prototype.padStart
JavaScript
22
star
19

Object.getOwnPropertyDescriptors

Spec-compliant shim for `Object.getOwnPropertyDescriptors` that works in ES5.
JavaScript
20
star
20

error-cause

An ES-spec-compliant shim/polyfill/replacement for the `.cause` property on all Error types that works as far down as ES3
JavaScript
17
star
21

String.prototype.matchAll

Spec-compliant polyfill for String.prototype.matchAll, in ES2020
JavaScript
17
star
22

Promise.any

ES spec-compliant shim for Promise.any
JavaScript
15
star
23

AggregateError

ES Proposal spec-compliant shim for AggregateError
JavaScript
14
star
24

String.prototype.trim

ES5 spec-compliant shim for String.prototype.trim
JavaScript
13
star
25

is-nan

ES6-compliant shim for Number.isNaN - the global isNaN returns false positives.
JavaScript
12
star
26

Function.prototype.name

ES6 spec-compliant Function.prototype.name sham.
JavaScript
10
star
27

String.prototype.padEnd

ES spec-compliant shim for String.prototype.padEnd
JavaScript
10
star
28

DisposableStack

An ESnext spec-compliant `DisposableStack`, `AsyncDisposableStack`, `Symbol.dispose`, and `Symbol.asyncDispose` shim/polyfill/replacement that works as far down as ES3.
JavaScript
10
star
29

Object.hasOwn

Official ES spec-compliant polyfill for Object.hasOwn
JavaScript
10
star
30

obsolete-string-at

A robust & optimized ES3-compatible polyfill for the `String.prototype.at` proposal for ECMAScript - based on https://mths.be/at
JavaScript
9
star
31

Array.prototype.groupToMap

An ESnext spec-compliant `Array.prototype.groupToMap` shim/polyfill/replacement that works as far down as ES3.
JavaScript
9
star
32

RegExp.prototype.flags

ES6 spec-compliant RegExp.prototype.flags shim
JavaScript
8
star
33

String.prototype.trimRight

ES2019 spec-compliant shim for String.prototype.trimRight.
JavaScript
8
star
34

array-from-async

An implementation for Array.fromAsync, which dumps an async iterator into an array.
JavaScript
7
star
35

Array.prototype.toSorted

An ESnext spec-compliant `Array.prototype.toSorted` shim/polyfill/replacement that works as far down as ES3.
JavaScript
7
star
36

Array.prototype.every

Array.prototype.every spec-compliant polyfill
JavaScript
7
star
37

String.prototype.trimLeft

ES2019 spec-compliant shim for String.prototype.trimLeft.
JavaScript
7
star
38

es-shims

Everything required to ensure that a browser is as ECMAScript-compliant as possible.
JavaScript
7
star
39

Symbol.prototype.description

Spec-compliant shim for Symbol.prototype.description proposal.
JavaScript
6
star
40

String.prototype.trimEnd

ES2019 spec-compliant String.prototype.trimEnd shim
JavaScript
6
star
41

set-tojson

Rejected ESNext Proposal: Set#toJSON
JavaScript
6
star
42

Reflect.ownKeys

ES2015 spec-compliant shim for Reflect.ownKeys
JavaScript
6
star
43

Promise.withResolvers

ES Proposal spec-compliant shim for Promise.withResolvers
JavaScript
5
star
44

Array.prototype.indexOf

An ES2015 spec-compliant `Array.prototype.indexOf` shim/polyfill/replacement that works as far down as ES3.
JavaScript
5
star
45

String.prototype.trimStart

ES2019 spec-compliant String.prototype.trimStart shim
JavaScript
5
star
46

map-tojson

Rejected ESNext Proposal: Map#toJSON
JavaScript
5
star
47

Number.isInteger

An ES-spec-compliant Number.isInteger shim/polyfill/replacement that works as far down as ES3
JavaScript
4
star
48

Array.prototype.filter

Simple ES6 Array.prototype.filter polyfill for older environments.
JavaScript
4
star
49

Math.fround

An ES-spec-compliant Math.fround shim/polyfill/replacement that works as far down as ES3
JavaScript
4
star
50

Promise.try

ES Proposal spec-compliant shim for Promise.try
JavaScript
4
star
51

Math.sign

An ES-spec-compliant Math.sign shim/polyfill/replacement that works as far down as ES3
JavaScript
4
star
52

es-create-array-iterator

EcmaScript's CreateArrayIterator abstract operation
JavaScript
4
star
53

String.raw

An ES-spec-compliant String.raw shim/polyfill/replacement that works as far down as ES3
JavaScript
4
star
54

Array.prototype.map

Array.prototype.map spec-compliant polyfill
JavaScript
4
star
55

well-known-symbols

An ESnext spec-compliant shim/polyfill/replacement for all Well-Known Symbols that works in any environment with Symbols
JavaScript
4
star
56

Set.prototype.intersection

An ESnext spec-compliant `Set.prototype.intersection` shim/polyfill/replacement that works as far down as ES3
JavaScript
4
star
57

iterator-helpers

An ESnext spec-compliant iterator helpers shim/polyfill/replacement that works as far down as ES3.
JavaScript
4
star
58

Number.isNaN

A `Number.isNaN` polyfill, based on the ECMAScript 6 specification.
JavaScript
4
star
59

Math.imul

An ES-spec-compliant Math.imul shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
60

parseInt

An ES-spec-compliant `parseInt` shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
61

Set.prototype.difference

An ESnext spec-compliant `Set.prototype.difference` shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
62

Array.prototype.reduceRight

An ES5 spec-compliant `Array.prototype.reduceRight` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
63

Reflect.getPrototypeOf

An ES2015 mostly-spec-compliant `Reflect.getPrototypeOf` sham/polyfill/replacement that works in as many engines as possible.
JavaScript
3
star
64

Array.prototype.reduce

Array.prototype.reduce spec-compliant polyfill
JavaScript
3
star
65

Math.atanh

An ES-spec-compliant Math.atanh shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
66

Reflect.apply

An ES-spec-compliant Reflect.apply shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
67

Number.parseInt

An ES-spec-compliant Number.parseInt shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
68

TypedArray.prototype.slice

ES spec-compliant shim for TypedArray.prototype.slice
JavaScript
3
star
69

Array.prototype.lastIndexOf

An ES2015 spec-compliant `Array.prototype.lastIndexOf` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
70

Array.prototype.findLast

An ESnext spec-compliant `Array.prototype.findLast` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
71

Object.defineProperties

ES spec-compliant `Object.defineProperties` shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
72

Math.clz32

An ES-spec-compliant Math.clz32 shim/polyfill/replacement that works as far down as ES3
JavaScript
3
star
73

Array.prototype.keys

An ES2015 spec-compliant `Array.prototype.keys` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
74

Array.prototype.copyWithin

An ES2015 spec-compliant `Array.prototype.copyWithin` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
75

Array.prototype.findLastIndex

An ESnext spec-compliant `Array.prototype.findLastIndex` shim/polyfill/replacement that works as far down as ES3.
JavaScript
3
star
76

Number.isFinite

An ES-spec-compliant Number.isFinite shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
77

String.prototype.split

String.prototype.split spec-compliant polyfill
JavaScript
2
star
78

Set.prototype.isSupersetOf

An ESnext spec-compliant `Set.prototype.isSupersetOf` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
79

Array.prototype.entries

An ES2015 spec-compliant `Array.prototype.entries` shim/polyfill/replacement that works as far down as ES3.
JavaScript
2
star
80

Map

An ES2015 spec-compliant `Map` shim/polyfill/replacement that works as far down as ES3.
JavaScript
2
star
81

ArrayBuffer.prototype.detached

ES Proposal spec-compliant shim for ArrayBuffer.prototype.detached
JavaScript
2
star
82

Date

ES spec-compliant `Date` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
83

es-set-tostringtag

A helper to optimistically set Symbol.toStringTag, when possible.
JavaScript
2
star
84

Object.getPrototypeOf

An ES5 mostly-spec-compliant `Object.getPrototypeOf` sham/polyfill/replacement that works in as many engines as possible.
JavaScript
2
star
85

Set.prototype.isDisjointFrom

An ESnext spec-compliant `Set.prototype.isDisjointFrom` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
86

Math.f16round

An ES-spec-compliant Math.f16round shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
87

all

All of the es-shims, auto-installed into the environment
JavaScript
2
star
88

Set.prototype.symmetricDifference

An ESnext spec-compliant `Set.prototype.symmetricDifference` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
89

typedarray

JavaScript
2
star
90

Array.prototype.values

An ES2015 spec-compliant `Array.prototype.values` shim/polyfill/replacement that works as far down as ES3.
JavaScript
2
star
91

Math.cbrt

An ES-spec-compliant Math.cbrt shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
92

Array.prototype.forEach

Array.prototype.forEach spec-compliant polyfill
JavaScript
2
star
93

Set.prototype.isSubsetOf

An ESnext spec-compliant `Set.prototype.isSubsetOf` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
94

Number.parseFloat

An ES-spec-compliant Number.parseFloat shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
95

Set.prototype.union

An ESnext spec-compliant `Set.prototype.union` shim/polyfill/replacement that works as far down as ES3
JavaScript
2
star
96

Array.prototype.some

Array.prototype.some spec-compliant polyfill
JavaScript
2
star
97

SuppressedError

ES Proposal spec-compliant shim for SuppressedError
JavaScript
2
star
98

es-string-html-methods

An ES-spec-compliant shim/polyfill/replacement for the Annex B String.prototype HTML methods that works as far down as ES3
JavaScript
1
star
99

Set

An ESnext spec-compliant `Set` shim/polyfill/replacement that works as far down as ES3.
JavaScript
1
star
100

Math.acosh

An ES-spec-compliant Math.acosh shim/polyfill/replacement that works as far down as ES3
JavaScript
1
star