{"id":13410958,"url":"https://github.com/paulmillr/es6-shim","last_synced_at":"2025-05-12T05:34:29.224Z","repository":{"id":2105099,"uuid":"3046507","full_name":"paulmillr/es6-shim","owner":"paulmillr","description":"ECMAScript 6 compatibility shims for legacy JS engines","archived":false,"fork":false,"pushed_at":"2024-12-30T22:38:32.000Z","size":4483,"stargazers_count":3118,"open_issues_count":33,"forks_count":384,"subscribers_count":101,"default_branch":"master","last_synced_at":"2025-05-08T18:05:28.827Z","etag":null,"topics":["ecmascript","ecmascript6","es6","shim"],"latest_commit_sha":null,"homepage":"http://paulmillr.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"lispcast/lab-notebook","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulmillr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-12-25T00:29:07.000Z","updated_at":"2025-05-04T06:12:08.000Z","dependencies_parsed_at":"2024-06-18T10:54:00.898Z","dependency_job_id":"235dbebe-f11b-4b2c-8916-f946c5095773","html_url":"https://github.com/paulmillr/es6-shim","commit_stats":{"total_commits":1319,"total_committers":47,"mean_commits":28.06382978723404,"dds":"0.31311599696739956","last_synced_commit":"f3d5fedaa29aa9eac38abec4cd0055c8e5e887f9"},"previous_names":[],"tags_count":94,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fes6-shim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fes6-shim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fes6-shim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmillr%2Fes6-shim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulmillr","download_url":"https://codeload.github.com/paulmillr/es6-shim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253124242,"owners_count":21857614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecmascript","ecmascript6","es6","shim"],"created_at":"2024-07-30T20:01:10.455Z","updated_at":"2025-05-08T18:05:57.490Z","avatar_url":"https://github.com/paulmillr.png","language":"JavaScript","funding_links":[],"categories":["Packages","JavaScript","目录","Polyfills","Web 前端","Polyfill","Number","\u003e 1K ⭐️"],"sub_categories":["Polyfills","浏览器兼容方案","Brunch Plugins","React Components","Polyfills JavaScript"],"readme":"# ES6 Shim \u003csup\u003e[![Version Badge][npm-version-svg]][package-url]\u003c/sup\u003e\nProvides compatibility shims so that legacy JavaScript engines behave as\nclosely as possible to ECMAScript 6 (Harmony).\n\n[![github actions][actions-image]][actions-url]\n[![coverage][codecov-image]][codecov-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\n[![npm badge][npm-badge-png]][package-url]\n\n\u003c!-- [![browser support](https://ci.testling.com/paulmillr/es6-shim.png)](https://ci.testling.com/paulmillr/es6-shim) --\u003e\n\n\u003c!-- [![Sauce Test Status](https://saucelabs.com/browser-matrix/es6-shim.svg)](https://saucelabs.com/u/es6-shim) --\u003e\n\n[HTML version of the final ECMAScript 6 spec][spec-html-url]\n\n## Installation\nIf you want to use it in browser:\n\n* Just include `es6-shim` before your scripts.\n* Include [es5-shim][es5-shim-url] especially if your browser doesn't support ECMAScript 5 - but every JS engine requires the `es5-shim` to correct broken implementations, so it's strongly recommended to always include it. Additionally, `es5-shim` should be loaded before `es6-shim`.\n\nFor `node.js`, `io.js`, or any `npm`-managed workflow (this is the recommended method):\n\n    npm install es6-shim\n\nAlternative methods:\n* `component install paulmillr/es6-shim` if you’re using [component(1)](https://github.com/componentjs/component).\n* `bower install es6-shim` if you’re using [Bower](http://bower.io/).\n\nIn both browser and node you may also want to include `unorm`; see the [`String.prototype.normalize`](#stringprototypenormalize) section for details.\n\n## Safe shims\n\n* `Map` (requires ES5 property descriptor support) ([a standalone shim is also available](https://npmjs.com/es-map))\n* `Set` (requires ES5 property descriptor support) ([a standalone shim is also available](https://npmjs.com/es-set))\n* `Promise`\n* `String`:\n    * `fromCodePoint()` ([a standalone shim is also available](https://npmjs.com/string.fromcodepoint))\n    * `raw()` ([a stanadlone shim is also available](https://www.npmjs.com/package/string.raw))\n* `String.prototype`:\n    * `codePointAt()` ([a standalone shim is also available](https://npmjs.com/string.prototype.codepointat))\n    * `endsWith()` ([a standalone shim is also available](https://npmjs.com/string.prototype.endswith))\n    * `includes()` ([a standalone shim is also available](https://npmjs.com/string.prototype.includes))\n    * `repeat()` ([a standalone shim is also available](https://npmjs.com/string.prototype.repeat))\n    * `startsWith()` ([a standalone shim is also available](https://npmjs.com/string.prototype.startswith))\n* `RegExp`:\n    * `new RegExp`, when given a RegExp as the pattern, will no longer throw when given a \"flags\" string argument. (requires ES5)\n* `RegExp.prototype`:\n    * `flags` (requires ES5) ([a standalone shim is also available](https://npmjs.com/regexp.prototype.flags))\n    * `[Symbol.match]` (requires native `Symbol`s)\n    * `[Symbol.replace]` (requires native `Symbol`s)\n    * `[Symbol.search]` (requires native `Symbol`s)\n    * `[Symbol.split]` (requires native `Symbol`s)\n    * `toString`\n* `Number`:\n    * binary and octal literals: `Number('0b1')` and `Number('0o7')`\n    * `EPSILON` ([a standalone shim is also available](https://www.npmjs.com/package/es-constants))\n    * `MAX_SAFE_INTEGER` ([a standalone shim is also available](https://www.npmjs.com/package/es-constants))\n    * `MIN_SAFE_INTEGER` ([a standalone shim is also available](https://www.npmjs.com/package/es-constants))\n    * `isNaN()` ([a standalone shim is also available](https://www.npmjs.com/package/is-nan))\n    * `isInteger()` ([a standalone shim is also available](https://www.npmjs.com/package/number.isinteger))\n    * `isSafeInteger()`([a standalone shim is also available](https://www.npmjs.com/package/number.issafeinteger))\n    * `isFinite()` ([a standalone shim is also available](https://www.npmjs.com/package/number.isfinite))\n    * `parseInt()` ([a standalone shim is also available](https://www.npmjs.com/package/parseint))\n    * `parseFloat()`\n* `Array`:\n    * `from()` ([a standalone shim is also available](https://www.npmjs.com/package/array.from))\n    * `of()` ([a standalone shim is also available](https://www.npmjs.com/package/array.of))\n* `Array.prototype`:\n    * `copyWithin()` ([a standalone shim is also available](https://npmjs.com/array.prototype.copywithin))\n    * `entries()` ([a standalone shim is also available](https://npmjs.com/array.prototype.entries))\n    * `fill()`\n    * `find()` ([a standalone shim is also available](https://npmjs.com/array.prototype.find))\n    * `findIndex()` ([a standalone shim is also available](https://npmjs.com/array.prototype.findindex))\n    * `keys()` ([a standalone shim is also available](https://npmjs.com/array.prototype.keys))\n    * `values()` ([a standalone shim is also available](https://npmjs.com/array.prototype.values))\n    * `indexOf()` (ES6 errata) ([a standalone shim is also available](https://npmjs.com/array.prototype.indexof))\n* `Object`:\n    * `assign()` ([a standalone shim is also available](https://npmjs.com/object.assign))\n    * `is()` ([a standalone shim is also available](https://npmjs.com/object-is))\n    * `keys()` (in ES5, but no longer throws on non-object non-null/undefined values in ES6) ([a standalone shim is also available](https://npmjs.com/object-keys)\n    * `setPrototypeOf()` (IE \u003e= 11)\n* `Function.prototype`:\n    * `name` (es6-sham, covers IE 9-11) ([a standalone shim is also available](https://npmjs.com/function.prototype.name)\n* `Math`:\n    * `acosh()` ([a standalone shim is also available](https://npmjs.com/math.acosh))\n    * `asinh()`\n    * `atanh()` ([a standalone shim is also available](https://npmjs.com/math.atanh))\n    * `cbrt()` ([a standalone shim is also available](https://npmjs.com/math.cbrt))\n    * `clz32()` ([a standalone shim is also available](https://npmjs.com/math.clz32))\n    * `cosh()`\n    * `expm1()`\n    * `fround()` ([a standalone shim is also available](https://npmjs.com/math.fround))\n    * `hypot()`\n    * `imul()` ([a standalone shim is also available](https://npmjs.com/math.imul))\n    * `log10()` ([a standalone shim is also available](https://npmjs.com/math.log10))\n    * `log1p()` ([a standalone shim is also available](https://npmjs.com/math.log1p))\n    * `log2()`\n    * `sign()` ([a standalone shim is also available](https://npmjs.com/math.sign))\n    * `sinh()`\n    * `tanh()`\n    * `trunc()`\n\nMath functions’ accuracy is 1e-11.\n\n* `Reflect`\n    * `apply()` ([a standalone shim is also available](https://npmjs.com/reflect.apply))\n    * `construct()`\n    * `defineProperty()`\n    * `deleteProperty()`\n    * `get()`\n    * `getOwnPropertyDescriptor()`\n    * `getPrototypeOf()` ([a standalone shim is also available](https://npmjs.com/reflect.getprototypeof))\n    * `has()`\n    * `isExtensible()`\n    * `ownKeys()` ([a standalone shim is also available](https://npmjs.com/reflect.ownkeys))\n    * `preventExtensions()`\n    * `set()`\n    * `setPrototypeOf()`\n\n* `Symbol` (only if it already exists)\n    * `match` (and corresponding `String#match`, `String#startsWith`, `String#endsWith`, `String#includes`, `RegExp` support)\n    * `replace` (and corresponding `String#replace` support)\n    * `search` (and corresponding `String#search` support)\n    * `split` (and corresponding `String#split` support)\n\nWell-known symbols will only be provided if the engine already has `Symbol` support.\n\n* `String.prototype` Annex B HTML methods ([a standalone shim is also available](https://www.npmjs.com/package/es-string-html-methods))\n    * `anchor()`\n    * `big()`\n    * `blink()`\n    * `bold()`\n    * `fixed()`\n    * `fontcolor()`\n    * `fontsize()`\n    * `italics()`\n    * `link()`\n    * `small()`\n    * `strike()`\n    * `sub()`\n    * `sup()`\n\nThese methods are part of \"Annex B\", which means that although they are a defacto standard, you shouldn't use them. None the less, the `es6-shim` provides them and normalizes their behavior across browsers.\n\n## Subclassing\nThe `Map`, `Set`, and `Promise` implementations are subclassable.\nYou should use the following pattern to create a subclass in ES5 which will continue to work in ES6:\n```javascript\nrequire('es6-shim');\n\nfunction MyPromise(exec) {\n  var promise = new Promise(exec);\n  Object.setPrototypeOf(promise, MyPromise.prototype);\n  // ...\n  return promise;\n}\nObject.setPrototypeOf(MyPromise, Promise);\nMyPromise.prototype = Object.create(Promise.prototype, {\n  constructor: { value: MyPromise }\n});\n```\n\n## String.prototype.normalize\nIncluding a proper shim for `String.prototype.normalize` would increase the size of this library by a factor of more than 4.\nSo instead we recommend that you install the [`unorm`](https://github.com/walling/unorm) package alongside `es6-shim` if you need `String.prototype.normalize`.\nSee https://github.com/paulmillr/es6-shim/issues/134 for more discussion.\n\n\n## WeakMap shim\nIt is not possible to implement WeakMap in pure javascript.\nThe [es6-collections](https://github.com/WebReflection/es6-collections) implementation doesn't hold values strongly, which is critical for the collection. `es6-shim` decided to not include an incorrect shim.\n\n`WeakMap` has very unusual use-cases, so you probably won't need it at all (use simple `Map` instead).\n\n## Getting started\n\n```javascript\nrequire('es6-shim');\nvar assert = require('assert');\n\nassert.equal(true, 'abc'.startsWith('a'));\nassert.equal(false, 'abc'.endsWith('a'));\nassert.equal(true, 'john alice'.includes('john'));\nassert.equal('123'.repeat(2), '123123');\n\nassert.equal(false, NaN === NaN);\nassert.equal(true, Object.is(NaN, NaN));\nassert.equal(true, -0 === 0);\nassert.equal(false, Object.is(-0, 0));\n\nvar result = Object.assign({ a: 1 }, { b: 2 });\nassert.deepEqual(result, { a: 1, b: 2 });\n\nassert.equal(true, isNaN('a'));\nassert.equal(false, Number.isNaN('a'));\nassert.equal(true, Number.isNaN(NaN));\n\nassert.equal(true, isFinite('123'));\nassert.equal(false, Number.isFinite('123'));\nassert.equal(false, Number.isFinite(Infinity));\n\n// Tests if value is a number, finite,\n// \u003e= -9007199254740992 \u0026\u0026 \u003c= 9007199254740992 and floor(value) === value\nassert.equal(false, Number.isInteger(2.4));\n\nassert.equal(1, Math.sign(400));\nassert.equal(0, Math.sign(0));\nassert.equal(-1, Math.sign(-400));\n\nvar found = [5, 10, 15, 10].find(function (item) { return item / 2 === 5; });\nassert.equal(10, found);\n\nvar foundIndex = [5, 10, 15, 10].findIndex(function (item) { return item / 2 === 5; });\nassert.equal(1, foundIndex);\n\n// Replacement for `{}` key-value storage.\n// Keys can be anything.\nvar map = new Map([['Bob', 42], ['Foo', 'bar']]);\nmap.set('John', 25);\nmap.set('Alice', 400);\nmap.set(['meh'], 555);\nassert.equal(undefined, map.get(['meh'])); // undefined because you need to use exactly the same object.\nmap.delete('Alice');\nmap.keys();\nmap.values();\nassert.equal(4, map.size);\n\n// Useful for storing unique items.\nvar set = new Set([0, 1]);\nset.add(2);\nset.add(5);\nassert.equal(true, set.has(0));\nassert.equal(true, set.has(1));\nassert.equal(true, set.has(2));\nassert.equal(false, set.has(4));\nassert.equal(true, set.has(5));\nset.delete(5);\nassert.equal(false, set.has(5));\n\n// Promises, see\n// http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript\n// https://github.com/petkaantonov/bluebird/#what-are-promises-and-why-should-i-use-them\nPromise.resolve(5).then(function (value) {\n  assert.equal(value, 5);\n  if (value) throw new Error('whoops!');\n  // do some stuff\n  return anotherPromise();\n}).catch(function (e) {\n  assert.equal(e.message, 'whoops!');\n  assert.equal(true, e instanceof Error);\n  // any errors thrown asynchronously end up here\n});\n```\n\n## Caveats\n\n - `Object.setPrototypeOf` / `Reflect.setPrototypeOf`\n   - Note that null objects (`Object.create(null)`, eg, an object with `null` as its `[[Prototype]]`) can not have their `[[Prototype]]` changed except via a native `Object.setPrototypeOf`.\n - Well-known `Symbol`s\n   - In order to make them work cross-realm, these are created with the global `Symbol` registry via `Symbol.for`. This does not violate the spec, but it does mean that `Symbol.for('Symbol.search') === Symbol.search` will be `true`, which it would not by default in a fresh compliant realm.\n\n## [License][license-url]\n\nThe project was initially based on es6-shim by Axel Rauschmayer.\n\n[license-url]: https://github.com/paulmillr/es6-shim/blob/master/LICENSE\n[spec-html-url]: http://www.ecma-international.org/ecma-262/6.0/\n[es5-shim-url]: https://github.com/es-shims/es5-shim\n\n[package-url]: https://npmjs.org/package/es6-shim\n[npm-version-svg]: https://versionbadg.es/paulmillr/es6-shim.svg\n[deps-svg]: https://david-dm.org/paulmillr/es6-shim.svg\n[deps-url]: https://david-dm.org/paulmillr/es6-shim\n[dev-deps-svg]: https://david-dm.org/paulmillr/es6-shim/dev-status.svg\n[dev-deps-url]: https://david-dm.org/paulmillr/es6-shim#info=devDependencies\n[npm-badge-png]: https://nodei.co/npm/es6-shim.png?downloads=true\u0026stars=true\n[license-image]: https://img.shields.io/npm/l/es6-shim.svg\n[license-url]: LICENSE\n[downloads-image]: https://img.shields.io/npm/dm/es6-shim.svg\n[downloads-url]: https://npm-stat.com/charts.html?package=es6-shim\n[codecov-image]: https://codecov.io/gh/paulmillr/es6-shim/branch/main/graphs/badge.svg\n[codecov-url]: https://app.codecov.io/gh/paulmillr/es6-shim/\n[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/paulmillr/es6-shim\n[actions-url]: https://github.com/paulmillr/es6-shim/actions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fes6-shim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulmillr%2Fes6-shim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmillr%2Fes6-shim/lists"}