### Prepare Monorepo and Install Dependencies Source: https://github.com/zloirock/core-js/blob/master/CONTRIBUTING.md Run this command to set up the monorepo and install all necessary dependencies before testing. ```sh npm run prepare-monorepo ``` -------------------------------- ### WeakMap Usage Example Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates basic WeakMap operations like setting, getting, checking for keys, and deleting entries. Also shows how to use WeakMap for private properties. ```javascript let a = [1]; let b = [2]; let c = [3]; let weakmap = new WeakMap([[a, 1], [b, 2]]); weakmap.set(c, 3).set(b, 4); console.log(weakmap.has(a)); // => true console.log(weakmap.has([1])); // => false console.log(weakmap.get(a)); // => 1 weakmap.delete(a); console.log(weakmap.get(a)); // => undefined // Private properties store: let Person = (() => { let names = new WeakMap(); return class { constructor(name) { names.set(this, name); } getName() { return names.get(this); } }; })(); let person = new Person('Vasya'); console.log(person.getName()); // => 'Vasya' for (let key in person) console.log(key); // => only 'getName' ``` -------------------------------- ### core-js Examples in Deno Source: https://github.com/zloirock/core-js/blob/master/deno/corejs/README.md Demonstrates the usage of various core-js features within a Deno environment after importing the library. Includes examples for Object.hasOwn, Array.prototype.at, Array.prototype.group, Promise.any, and iterator helpers. ```javascript Object.hasOwn({ foo: 42 }, 'foo'); // => true [1, 2, 3, 4, 5, 6, 7].at(-3); // => 5 [1, 2, 3, 4, 5].group(it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] } Promise.any([ Promise.resolve(1), Promise.reject(2), Promise.resolve(3), ]).then(console.log); // => 1 (function * (i) { while (true) yield i++; })(1) .drop(1).take(5) .filter(it => it % 2) .map(it => it ** 2) .toArray(); // => [9, 25] ``` -------------------------------- ### Map Usage Examples Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/collections.md Demonstrates common Map operations including initialization, setting and getting values, checking for keys, iteration, and deletion. Also shows the use of `Map.groupBy` for grouping elements. ```javascript let array = [1]; let map = new Map([['a', 1], [42, 2]]); map.set(array, 3).set(true, 4); console.log(map.size); // => 4 console.log(map.has(array)); // => true console.log(map.has([1])); // => false console.log(map.get(array)); // => 3 map.forEach((val, key) => { console.log(val); // => 1, 2, 3, 4 console.log(key); // => 'a', 42, [1], true }); map.delete(array); console.log(map.size); // => 3 console.log(map.get(array)); // => undefined console.log(Array.from(map)); // => [['a', 1], [42, 2], [true, 4]] map = new Map([['a', 1], ['b', 2], ['c', 3]]); for (let [key, value] of map) { console.log(key); // => 'a', 'b', 'c' console.log(value); // => 1, 2, 3 } for (let value of map.values()) console.log(value); // => 1, 2, 3 for (let key of map.keys()) console.log(key); // => 'a', 'b', 'c' for (let [key, value] of map.entries()) { console.log(key); // => 'a', 'b', 'c' console.log(value); // => 1, 2, 3 } map = Map.groupBy([1, 2, 3, 4, 5], it => it % 2); map.get(1); // => [1, 3, 5] map.get(0); // => [2, 4] map = new Map([['a', 1]]); map.getOrInsert('a', 2); // => 1 map.getOrInsert('b', 3); // => 3 map.getOrInsertComputed('a', key => key); // => 1 map.getOrInsertComputed('c', key => key); // => 'c' console.log(map); // => Map { 'a': 1, 'b': 3, 'c': 'c' } ``` -------------------------------- ### Map Basic Usage Example Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates fundamental Map operations including setting, getting, checking for keys, deleting, and iterating over entries. Shows how Map handles different key types. ```javascript let array = [1]; let map = new Map([['a', 1], [42, 2]]); map.set(array, 3).set(true, 4); console.log(map.size); // => 4 console.log(map.has(array)); // => true console.log(map.has([1])); // => false console.log(map.get(array)); // => 3 map.forEach((val, key) => { console.log(val); // => 1, 2, 3, 4 console.log(key); // => 'a', 42, [1], true }); map.delete(array); console.log(map.size); // => 3 console.log(map.get(array)); // => undefined console.log(Array.from(map)); // => [['a', 1], [42, 2], [true, 4]] ``` -------------------------------- ### Install core-js Versions Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md Install core-js using npm. Choose between the global, pure (no global pollution), or bundled versions. ```sh // global version npm install --save core-js@3.49.0 // version without global namespace pollution npm install --save core-js-pure@3.49.0 // bundled global version npm install --save core-js-bundle@3.49.0 ``` -------------------------------- ### Basic Observable Usage Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/observable.md Demonstrates how to create and subscribe to an Observable, emitting values and completing. ```javascript new Observable(observer => { observer.next('hello'); observer.next('world'); observer.complete(); }).subscribe({ next(it) { console.log(it); }, complete() { console.log('!'); }, }); ``` -------------------------------- ### Promise All Example Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates `Promise.all` for running multiple promises concurrently and waiting for all to complete. ```javascript Promise.all([ 'foo', sleepRandom(5), sleepRandom(15), sleepRandom(10), // after 15 sec: ]).then(x => console.log(x)); // => ['foo', 956, 85, 382] ``` -------------------------------- ### Set.of Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/of-and-from-methods-on-collections.md Demonstrates the usage of the Set.of static method to create a new Set with unique values from provided arguments. ```javascript Set.of(1, 2, 3, 2, 1); // => Set {1, 2, 3} ``` -------------------------------- ### String PadStart and PadEnd Methods Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/string-regexp.md Demonstrates padding a string with another string until it reaches a specified length, either at the start or end. ```javascript 'hello'.padStart(10); // => ' hello' 'hello'.padStart(10, '1234'); // => '12341hello' 'hello'.padEnd(10); // => 'hello ' 'hello'.padEnd(10, '1234'); // => 'hello12341' ``` -------------------------------- ### Install core-js and regenerator-runtime dependencies Source: https://github.com/zloirock/core-js/blob/master/docs/2019-03-19-core-js-3-babel-and-a-look-into-the-future.md Install the necessary core-js and regenerator-runtime packages as direct dependencies for your project. ```bash npm i --save core-js regenerator-runtime ``` -------------------------------- ### core-js-compat Targets Option Examples Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Demonstrates how to specify browser targets using either a browserslist query string or a detailed targets object. ```javascript // browserslist query: 'defaults, not IE 11, maintained node versions'; ``` ```javascript // object (sure, all those fields optional): ({ android: '4.0', // Android WebView version bun: '0.1.2', // Bun version chrome: '38', // Chrome version 'chrome-android': '18', // Chrome for Android version deno: '1.12', // Deno version edge: '13', // Edge version electron: '5.0', // Electron framework version firefox: '15', // Firefox version 'firefox-android': '4', // Firefox for Android version hermes: '0.11', // Hermes version ie: '8', // Internet Explorer version ios: '13.0', // iOS Safari version node: 'current', // NodeJS version, you can use 'current' for set it to currently used opera: '12', // Opera version 'opera-android': '7', // Opera for Android version phantom: '1.9', // PhantomJS headless browser version quest: '5.0', // Meta Quest Browser version 'react-native': '0.70', // React Native version (default Hermes engine) rhino: '1.7.13', // Rhino engine version safari: '14.0', // Safari version samsung: '14.0', // Samsung Internet version /** * true option set target to minimum supporting ES Modules versions of all browsers, ignoring `browsers` target. * 'intersect' option intersects the `browsers` target and `browserslist`'s targets. The maximum version will be used. */ esmodules: true | 'intersect', browsers: '> 0.25%', // Browserslist query or object with target browsers }); ``` -------------------------------- ### Promise Resolve and Reject Example Source: https://github.com/zloirock/core-js/blob/master/README.md Illustrates how to use `Promise.resolve` and `Promise.reject` to create already settled promises. ```javascript /* eslint-disable promise/prefer-await-to-callbacks -- example */ Promise.resolve(42).then(x => console.log(x)); // => 42 Promise.reject(42).catch(error => console.log(error)); // => 42 Promise.resolve($.getJSON('/data.json')); // => ES promise ``` -------------------------------- ### Math.sumPrecise Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/math.md Demonstrates the usage of Math.sumPrecise to achieve precise summation of numbers. ```APIDOC ## Examples ### Precise Summation ```js // Standard floating-point addition can lead to inaccuracies 1e20 + 0.1 + -1e20; // => 0 // Math.sumPrecise provides a more accurate result Math.sumPrecise([1e20, 0.1, -1e20]); // => 0.1 ``` ``` -------------------------------- ### Full core-js Import Example Source: https://github.com/zloirock/core-js/blob/master/packages/core-js/README.md Demonstrates the usage of various core-js features with a full import. This method pollutes the global namespace. ```javascript import 'core-js/actual'; Promise.try(() => 42).then(it => console.log(it)); // => 42 Array.from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5] [1, 2].flatMap(it => [it, it]); // => [1, 1, 2, 2] Iterator.concat([1, 2], function * (i) { while (true) yield i++; }(3)) .drop(1).take(5) .filter(it => it % 2) .map(it => it ** 2) .toArray(); // => [9, 25] structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) ``` -------------------------------- ### Using console.log for Output Source: https://github.com/zloirock/core-js/blob/master/website/src/playground.html Demonstrates how to use console.log() to display output from core-js examples. This is a standard method for debugging and observing results. ```javascript console.log() ``` -------------------------------- ### Map Upsert Examples Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/map-upsert.md Demonstrates the usage of `getOrInsert` and `getOrInsertComputed` methods on a `Map` object. Shows how to insert or retrieve existing values. ```javascript const map = new Map([['a', 1]]); map.getOrInsert('a', 2); // => 1 map.getOrInsert('b', 3); // => 3 map.getOrInsertComputed('a', key => key); // => 1 map.getOrInsertComputed('c', key => key); // => 'c' console.log(map); // => Map { 'a': 1, 'b': 3, 'c': 'c' } ``` -------------------------------- ### Using Reflect.defineMetadata and Reflect.getOwnMetadata Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/reflect-metadata.md Demonstrates how to define and retrieve metadata on an object using Reflect.defineMetadata and Reflect.getOwnMetadata. This example shows that ownKeys does not include metadata keys. ```javascript let object = {}; Reflect.defineMetadata('foo', 'bar', object); Reflect.ownKeys(object); // => [] Reflect.getOwnMetadataKeys(object); // => ['foo'] Reflect.getOwnMetadata('foo', object); // => 'bar' ``` -------------------------------- ### core-js-compat Full Modules List Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Shows how to get a flat array containing all available module names within core-js. ```javascript // full list of modules: require('core-js-compat/modules'); // => Array ``` ```javascript // or require('core-js-compat').modules; // => Array ``` -------------------------------- ### Math.sumPrecise Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/math.md Demonstrates the usage of Math.sumPrecise for accurate summation of numbers, contrasting with standard addition which can lose precision. ```javascript 1e20 + 0.1 + -1e20; // => 0 Math.sumPrecise([1e20, 0.1, -1e20]); // => 0.1 ``` -------------------------------- ### Import core-js for Deno Source: https://github.com/zloirock/core-js/blob/master/deno/corejs/README.md Import the core-js library at the top of your Deno entry point to enable polyfills. This example uses a specific version of core-js. ```javascript import 'https://deno.land/x/corejs@v3.49.0/index.js'; ``` -------------------------------- ### Iterator Chunking and Windowing Examples Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates the usage of Iterator.chunks and Iterator.windows with different parameters. Shows how to create chunks and sliding windows, including handling undersized windows. ```javascript const digits = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].values(); let chunks = Array.from(digits().chunks(2)); // [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] let windows = Array.from(digits().windows(2)); // [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]] let windowsPartial = Array.from([0, 1].values().windows(3, 'allow-partial')); // [[0, 1]] let windowsFull = Array.from([0, 1].values().windows(3)); // [] ``` -------------------------------- ### core-js-compat compat() Method Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Shows how to use the `compat()` method to get a list of required core-js modules for specified targets and optionally filter by modules and version. ```javascript // equals of of the method from the example above require('core-js-compat/compat')({ targets, modules, version }); // => { list: Array, targets: { [ModuleName]: { [EngineName]: EngineVersion } } } ``` ```javascript // or require('core-js-compat').compat({ targets, modules, version }); // => { list: Array, targets: { [ModuleName]: { [EngineName]: EngineVersion } } } ``` -------------------------------- ### SWC Configuration for Core-JS Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md Example of configuring SWC's environment settings, including target browsers, mode, and core-js version, within a `.swcrc` file. ```json { "env": { "targets": "> 0.25%, not dead", "mode": "entry", "coreJs": "3.49" } } ``` -------------------------------- ### Reflect.construct Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/reflect.md Shows how Reflect.construct() is used to invoke a constructor with arguments, creating a new instance. ```javascript function C(a, b) { this.c = a + b; } let instance = Reflect.construct(C, [20, 22]); instance.c; // => 42 ``` -------------------------------- ### Joint Iteration Examples Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates the usage of Iterator.zip and Iterator.zipKeyed with different modes and padding options. Shows how to iterate over multiple iterables concurrently. ```javascript Iterator.zip([ [0, 1, 2], [3, 4, 5], ]).toArray(); // => [[0, 3], [1, 4], [2, 5]] Iterator.zipKeyed({ a: [0, 1, 2], b: [3, 4, 5, 6], c: [7, 8, 9], }, { mode: 'longest', padding: { c: 10 }, }).toArray(); /* [ { a: 0, b: 3, c: 7 }, { a: 1, b: 4, c: 8 }, { a: 2, b: 5, c: 9 }, { a: undefined, b: 6, c: 10 }, ]; */ ``` -------------------------------- ### String Includes, StartsWith, and EndsWith Methods Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/string-regexp.md Shows how to check for the presence of substrings, their starting positions, and ending positions within a string. ```javascript 'foobarbaz'.includes('bar'); // => true 'foobarbaz'.includes('bar', 4); // => false 'foobarbaz'.startsWith('foo'); // => true 'foobarbaz'.startsWith('bar', 3); // => true 'foobarbaz'.endsWith('baz'); // => true 'foobarbaz'.endsWith('bar', 6); // => true ``` -------------------------------- ### Pure core-js Import Example (No Global Pollution) Source: https://github.com/zloirock/core-js/blob/master/packages/core-js/README.md Demonstrates importing core-js features without polluting the global namespace, using named imports from 'core-js-pure'. ```javascript import Promise from 'core-js-pure/actual/promise'; import Set from 'core-js-pure/actual/set'; import Iterator from 'core-js-pure/actual/iterator'; import from from 'core-js-pure/actual/array/from'; import flatMap from 'core-js-pure/actual/array/flat-map'; import structuredClone from 'core-js-pure/actual/structured-clone'; Promise.try(() => 42).then(it => console.log(it)); // => 42 from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5] flatMap([1, 2], it => [it, it]); // => [1, 1, 2, 2] Iterator.concat([1, 2], function * (i) { while (true) yield i++; }(3)) .drop(1).take(5) .filter(it => it % 2) .map(it => it ** 2) .toArray(); // => [9, 25] structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) ``` -------------------------------- ### Promise.withResolvers Example Source: https://github.com/zloirock/core-js/blob/master/README.md Shows how to use `Promise.withResolvers` to create a promise with explicit resolve and reject methods. The promise is resolved with a value, and the result is logged. ```javascript const d = Promise.withResolvers(); d.resolve(42); d.promise.then(console.log); // => 42 ``` -------------------------------- ### Using Iteration Helpers with Arguments Object Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/iteration-helpers.md Demonstrates how to use isIterable, getIterator, and getIteratorMethod with the arguments object. Ensure the 'actual' path is imported for these examples. ```javascript import isIterable from 'core-js-pure/actual/is-iterable'; import getIterator from 'core-js-pure/actual/get-iterator'; import getIteratorMethod from 'core-js-pure/actual/get-iterator-method'; let list = (function () { // eslint-disable-next-line prefer-rest-params -- example return arguments; })(1, 2, 3); console.log(isIterable(list)); // true; let iterator = getIterator(list); console.log(iterator.next().value); // 1 console.log(iterator.next().value); // 2 console.log(iterator.next().value); // 3 console.log(iterator.next().value); // undefined getIterator({}); // TypeError: [object Object] is not iterable! let method = getIteratorMethod(list); console.log(typeof method); // 'function' iterator = method.call(list); console.log(iterator.next().value); // 1 console.log(iterator.next().value); // 2 console.log(iterator.next().value); // 3 console.log(iterator.next().value); // undefined console.log(getIteratorMethod({})); // undefined ``` -------------------------------- ### Babel Preset Env: 'usage' mode example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md With `useBuiltIns: 'usage'`, polyfills for features used in a file and not supported by the target environment are automatically imported at the top of the file. ```typescript // first file: let set = new Set([1, 2, 3]); ``` ```typescript // second file: let array = Array.of(1, 2, 3); ``` ```typescript // first file: import 'core-js/modules/es.array.iterator'; import 'core-js/modules/es.object.to-string'; import 'core-js/modules/es.set'; var set = new Set([1, 2, 3]); ``` ```typescript // second file: import 'core-js/modules/es.array.of'; var array = Array.of(1, 2, 3); ``` -------------------------------- ### Selective core-js Import Example Source: https://github.com/zloirock/core-js/blob/master/packages/core-js/README.md Shows how to import specific features from core-js to reduce bundle size. This method also pollutes the global namespace. ```javascript import 'core-js/actual/promise'; import 'core-js/actual/set'; import 'core-js/actual/iterator'; import 'core-js/actual/array/from'; import 'core-js/actual/array/flat-map'; import 'core-js/actual/structured-clone'; Promise.try(() => 42).then(it => console.log(it)); // => 42 Array.from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5] [1, 2].flatMap(it => [it, it]); // => [1, 1, 2, 2] Iterator.concat([1, 2], function * (i) { while (true) yield i++; }(3)) .drop(1).take(5) .filter(it => it % 2) .map(it => it ** 2) .toArray(); // => [9, 25] structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) ``` -------------------------------- ### Global Symbol Registry with Symbol.for and Symbol.keyFor Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/symbol.md Shows how to use `Symbol.for` to create or retrieve a symbol from a global registry and `Symbol.keyFor` to get its associated string key. ```javascript let symbol = Symbol.for('key'); symbol === Symbol.for('key'); // => true Symbol.keyFor(symbol); // => 'key' ``` -------------------------------- ### Babel Preset Env: 'entry' mode example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md When using `useBuiltIns: 'entry'`, imports of `core-js/stable` are replaced with specific module imports required for the target environment. ```typescript import 'core-js/stable'; ``` ```typescript import 'core-js/modules/es.array.unscopables.flat'; import 'core-js/modules/es.array.unscopables.flat-map'; import 'core-js/modules/es.object.from-entries'; import 'core-js/modules/web.immediate'; ``` -------------------------------- ### Basic Structured Clone Examples Source: https://github.com/zloirock/core-js/blob/master/README.md Illustrates basic deep cloning of an array of objects and a circular object using structuredClone. Verifies that the cloned object and its properties are distinct from the original. ```javascript const structured = [{ a: 42 }]; const sclone = structuredClone(structured); console.log(sclone); console.log(structured !== sclone); console.log(structured[0] !== sclone[0]); ``` ```javascript const circular = {}; circular.circular = circular; const cclone = structuredClone(circular); console.log(cclone.circular === cclone); ``` -------------------------------- ### Array.prototype.fill() Method Source: https://github.com/zloirock/core-js/blob/master/README.md Fills all the elements of an array from a start index to an end index with a static value. Supports optional start and end parameters. ```typescript fill(value: any, start?: number, end?: number): this; ``` -------------------------------- ### core-js-compat Modules by Entry Point Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Illustrates how to retrieve a map of modules categorized by their core-js entry points. ```javascript // map of modules by `core-js` entry points: require('core-js-compat/entries'); // => { [EntryPoint]: Array } ``` ```javascript // or require('core-js-compat').entries; // => { [EntryPoint]: Array } ``` -------------------------------- ### Function.isCallable and Function.isConstructor Examples Source: https://github.com/zloirock/core-js/blob/master/README.md Provides examples for checking if a value is a callable function or a constructible function (class or constructor function). Useful for runtime type checking and validation. ```javascript /* eslint-disable prefer-arrow-callback -- example */ Function.isCallable(null); // => false Function.isCallable({}); // => false Function.isCallable(function () { /* empty */ }); // => true Function.isCallable(() => { /* empty */ }); // => true Function.isCallable(class { /* empty */ }); // => false Function.isConstructor(null); // => false Function.isConstructor({}); // => false Function.isConstructor(function () { /* empty */ }); // => true Function.isConstructor(() => { /* empty */ }); // => false Function.isConstructor(class { /* empty */ }); // => true ``` -------------------------------- ### String#matchAll Entry Point Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/string-matchall.md Shows the import path for the String#matchAll proposal when using core-js. ```plaintext core-js/proposals/string-match-all ``` -------------------------------- ### Promise All Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/promise.md Demonstrates Promise.all() which takes an iterable of Promises and returns a single Promise that resolves when all of the Promises in the iterable have resolved, or rejects with the reason of the first Promise that rejects. ```js function sleepRandom(time) { return new Promise(resolve => setTimeout(resolve, time * 1e3, 0 | Math.random() * 1e3)); } Promise.all([ 'foo', sleepRandom(5), sleepRandom(15), sleepRandom(10), // after 15 sec: ]).then(x => console.log(x)); // -> ['foo', 956, 85, 382] ``` -------------------------------- ### Web Standard 'self' Usage Example Source: https://github.com/zloirock/core-js/blob/master/README.md Illustrates the usage of the `self` global object to access built-in global properties, such as `Array`. This example verifies that `self.Array` is equivalent to the global `Array`. ```javascript // eslint-disable-next-line no-restricted-globals -- example self.Array === Array; // => true ``` -------------------------------- ### Reflect.getPrototypeOf Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/reflect.md Gets the prototype of a specified object. ```APIDOC ## Reflect.getPrototypeOf ### Description Gets the prototype of a specified object. ### Method `Reflect.getPrototypeOf(target)` ### Parameters - **target** (Object) - The object whose prototype to retrieve. ### Response #### Success Response - Returns the prototype of the given object, or `null` if the object has no prototype. ``` -------------------------------- ### Core-JS Entry Point for Array flat/flatMap Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/array-prototype-flat-flatmap.md Import path for using Array.prototype.flat and Array.prototype.flatMap via core-js. ```plaintext core-js/proposals/array-flat-map ``` -------------------------------- ### Reflect.get Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/reflect.md Gets the value of a property on an object. ```APIDOC ## Reflect.get ### Description Gets the value of a property on an object. ### Method `Reflect.get(target, propertyKey, receiver)` ### Parameters - **target** (Object) - The object to retrieve the property from. - **propertyKey** (PropertyKey) - The name of the property to retrieve. - **receiver** (any, optional) - The value to use as `this` when retrieving the property's value (useful for getters). ### Response #### Success Response - Returns the value of the property. ``` -------------------------------- ### Get Required core-js Modules Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Import and use the compat function to get a list of required core-js modules and their target environment versions based on a browserslist query. You can filter modules by inclusion or exclusion and specify the core-js version. ```javascript import compat from 'core-js-compat'; const { list, targets, } = compat({ targets: '> 1%', modules: [ 'core-js/actual', 'esnext.array.unique-by', /^web\./, ], exclude: [ 'web.atob', ], version: '3.49', inverse: false, }); console.log(targets); ``` -------------------------------- ### Entry Point for Object.getOwnPropertyDescriptors Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/object-getownpropertydescriptors.md Specifies the import path for using the Object.getOwnPropertyDescriptors polyfill from core-js. ```plaintext core-js/proposals/object-getownpropertydescriptors ``` -------------------------------- ### Reflect.getOwnPropertyDescriptor Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/reflect.md Gets the own property descriptor of a specified property on an object. ```APIDOC ## Reflect.getOwnPropertyDescriptor ### Description Gets the own property descriptor of a specified property on an object. ### Method `Reflect.getOwnPropertyDescriptor(target, propertyKey)` ### Parameters - **target** (Object) - The object to retrieve the property descriptor from. - **propertyKey** (PropertyKey) - The name of the property to get the descriptor for. ### Response #### Success Response - Returns a property descriptor object for the given property if it exists on the object, otherwise returns `undefined`. ``` -------------------------------- ### AsyncIterator Chaining Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/asynciterator-helpers.md Demonstrates chaining multiple AsyncIterator helper methods: drop, take, filter, map, and toArray. This example shows how to process an async iterable by skipping elements, limiting the count, filtering based on a condition, transforming values, and collecting the results into an array. ```javascript await AsyncIterator.from([1, 2, 3, 4, 5, 6, 7]) .drop(1) .take(5) .filter(it => it % 2) .map(it => it ** 2) .toArray(); // => [9, 25] ``` -------------------------------- ### Get Function Name Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/function.md Retrieves the name of a function. Useful for debugging or introspection. ```javascript (function foo() { /* empty */ }).name; // => 'foo' ``` -------------------------------- ### Entry Point for Array.prototype.includes Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/array-prototype-includes.md Specifies the import path for the Array.prototype.includes proposal in core-js. ```plaintext core-js/proposals/array-includes ``` -------------------------------- ### Array.prototype.fill Source: https://github.com/zloirock/core-js/blob/master/README.md Fills all the elements of an array from a start index to an end index with a static value. ```APIDOC ## Array.prototype.fill ### Description Fills all the elements of an array from a start index to an end index with a static value. ### Method `fill(value: any, start?: number, end?: number): this` ### Parameters #### Path Parameters - **value** (any) - Required - The value to fill the array with. - **start** (number) - Optional - The index to start filling from (inclusive). - **end** (number) - Optional - The index to stop filling at (exclusive). ### Response #### Success Response (200) - **this** - The modified array. ``` -------------------------------- ### Entry Points for Composite Key and Symbol Modules Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/compositekey-compositesymbol.md Lists the available entry points for importing the `keys-composition` proposal, including full and pure versions for `composite-key` and `composite-symbol`. ```plaintext core-js/proposals/keys-composition core-js(-pure)/full/composite-key core-js(-pure)/full/composite-symbol ``` -------------------------------- ### Entry Point for Math.sumPrecise Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/math-sumprecise.md Specifies the import path for using the Math.sumPrecise proposal within core-js projects. ```plaintext core-js/proposals/math-sum ``` -------------------------------- ### Reflect.ownKeys Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/reflect.md Demonstrates how Reflect.ownKeys() retrieves all own property keys of an object, including symbols. ```javascript let object = { a: 1 }; Object.defineProperty(object, 'b', { value: 2 }); object[Symbol('c')] = 3; Reflect.ownKeys(object); // => ['a', 'b', Symbol(c)] ``` -------------------------------- ### Map Upsert Source: https://github.com/zloirock/core-js/blob/master/README.md Enables getting or inserting elements into Map and WeakMap with computed values. ```APIDOC ## Map Upsert ### Description Enables getting or inserting elements into Map and WeakMap, with options for computed values. ### Class `Map` ### Methods - `getOrInsert(key: any, value: any): any` - `getOrInsertComputed(key: any, (key: any) => value: any): any` ### Class `WeakMap` ### Methods - `getOrInsert(key: object | symbol, value: any): any` - `getOrInsertComputed(key: object | symbol, (key: any) => value: any): any` ``` -------------------------------- ### JSON stringify example with special characters Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates JSON.stringify with a character that requires special encoding. ```javascript JSON.stringify({ '𠮷': ['\uDF06\uD834'] }); // => '{"𠮷":["\\udf06\\ud834"]}' ``` -------------------------------- ### RegExp.prototype.toString Call Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/string-regexp.md Demonstrates how to get the string representation of a RegExp object, including its source and flags. ```javascript RegExp.prototype.toString.call({ source: 'foo', flags: 'bar' }); // => '/foo/bar' ``` -------------------------------- ### Babel Preset Env: 'entry' mode with multiple entry points Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md Demonstrates how `useBuiltIns: 'entry'` handles multiple core-js entry points, replacing them with specific module imports for the target environment. ```typescript import 'core-js/es'; import 'core-js/proposals/set-methods'; import 'core-js/full/set/map'; ``` ```typescript import 'core-js/modules/es.array.unscopables.flat'; import 'core-js/modules/es.array.unscopables.flat-map'; import 'core-js/modules/es.object.from-entries'; import 'core-js/modules/esnext.set.difference'; import 'core-js/modules/esnext.set.intersection'; import 'core-js/modules/esnext.set.is-disjoint-from'; import 'core-js/modules/esnext.set.is-subset-of'; import 'core-js/modules/esnext.set.is-superset-of'; import 'core-js/modules/esnext.set.map'; import 'core-js/modules/esnext.set.symmetric-difference'; import 'core-js/modules/esnext.set.union'; ``` -------------------------------- ### Basic Iterator.range Usage Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/iterator-range.md Iterates from a start number up to (but not including) an end number. Use this for simple sequential iteration. ```javascript for (const i of Iterator.range(1, 10)) { console.log(i); // => 1, 2, 3, 4, 5, 6, 7, 8, 9 } ``` -------------------------------- ### Symbol Basics and Properties Source: https://github.com/zloirock/core-js/blob/master/README.md Demonstrates basic Symbol usage, including accessing properties and their non-enumerable nature. ```javascript console.log(person.getName()); // => 'Vasya' console.log(person.name); // => undefined console.log(person[Symbol('name')]); // => undefined, symbols are uniq for (let key in person) console.log(key); // => nothing, symbols are not enumerable ``` -------------------------------- ### Build Packages and Test Bundles Source: https://github.com/zloirock/core-js/blob/master/CONTRIBUTING.md Prepares packages and test bundles, necessary for running tests in specific browsers. ```sh npx run-s prepare bundle ``` -------------------------------- ### core-js-compat Modules List for Target Version Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-compat/README.md Demonstrates how to obtain a list of core-js modules compatible with a specific core-js version. ```javascript // the subset of modules which available in the passed `core-js` version: require('core-js-compat/get-modules-list-for-target-version')('3.49'); // => Array ``` ```javascript // or require('core-js-compat').getModulesListForTargetVersion('3.49'); // => Array ``` -------------------------------- ### Importing Set Methods with core-js Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/set-methods.md This shows the entry point for importing the Set methods proposal (v2) when using the core-js library. Ensure you use the correct path for your project. ```plaintext core-js/proposals/set-methods-v2 ``` -------------------------------- ### Import Specific Core-JS Features (Set Example) Source: https://github.com/zloirock/core-js/blob/master/README.md Import specific features like 'Set' or its related methods, with options for including proposals or only stable features. Also shows how to import without global namespace pollution. ```ts // if you want to polyfill `Set`: // all `Set`-related features, with early-stage ES proposals: import "core-js/full/set"; ``` ```ts // stable required for `Set` ES features, features from web standards and stage 3 ES proposals: import "core-js/actual/set"; ``` ```ts // stable required for `Set` ES features and features from web standards // (DOM collections iterator in this case): import "core-js/stable/set"; ``` ```ts // only stable ES features required for `Set`: import "core-js/es/set"; ``` ```ts // the same without global namespace pollution: import Set from "core-js-pure/full/set"; ``` ```ts import Set from "core-js-pure/actual/set"; ``` ```ts import Set from "core-js-pure/stable/set"; ``` ```ts import Set from "core-js-pure/es/set"; ``` -------------------------------- ### Entry Point for Well-formed Stringify Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/well-formed-jsonstringify.md This is the import path for using the well-formed JSON.stringify proposal from the core-js library. ```plaintext core-js/proposals/well-formed-stringify ``` -------------------------------- ### Symbol Predicates Usage Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/proposals/symbol-predicates.md Demonstrates how to use Symbol.isRegisteredSymbol to check if a symbol is globally registered and Symbol.isWellKnownSymbol to check for well-known symbols. ```javascript Symbol.isRegisteredSymbol(Symbol.for('key')); // => true Symbol.isRegisteredSymbol(Symbol('key')); // => false Symbol.isWellKnownSymbol(Symbol.iterator); // => true Symbol.isWellKnownSymbol(Symbol('key')); // => false ``` -------------------------------- ### Build Custom core-js Bundle Source: https://github.com/zloirock/core-js/blob/master/packages/core-js-builder/README.md Demonstrates how to use the core-js-builder to create a custom bundle. Configure included modules, exclusions, and target environments. The output can be formatted as a bundle, CJS, or ESM, and saved to a specified filename. ```javascript import builder from 'core-js-builder'; const bundle = await builder({ // entry / module / namespace / an array of them, by default - all `core-js` modules modules: ['core-js/actual', /^esnext\.reflect\./], // a blacklist of entries / modules / namespaces, by default - empty list exclude: [/^es\.math\./, 'es.number.constructor'], // optional browserslist or core-js-compat format query targets: '> 0.5%, not dead, ie 9-11', // shows summary for the bundle, disabled by default summary: { // in the console, you could specify required parts or set `true` for enable all of them console: { size: true, modules: false }, // in the comment in the target file, similarly to `summary.console` comment: { size: false, modules: true }, }, // output format, 'bundle' by default, can be 'cjs' or 'esm', and in this case // the result will not be bundled and will contain imports of required modules format: 'bundle', // optional target filename, if it's missed a file will not be created filename: PATH_TO_MY_COREJS_BUNDLE, }); ``` -------------------------------- ### Basic structuredClone Usage Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/web-standards/structured-clone.md Demonstrates the basic usage of structuredClone for cloning an array of objects. Shows that the clone is a deep copy. ```js const structured = [{ a: 42 }]; const sclone = structuredClone(structured); console.log(sclone); console.log(structured !== sclone); console.log(structured[0] !== sclone[0]); ``` -------------------------------- ### Promise Finally Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/promise.md Shows how Promise.finally() executes a callback regardless of whether the Promise is fulfilled or rejected, useful for cleanup operations. ```js Promise.resolve(42).finally(() => console.log('You will see it anyway')); Promise.reject(42).finally(() => console.log('You will see it anyway')); ``` -------------------------------- ### Promise Resolve and Reject Examples Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/promise.md Illustrates using Promise.resolve() to create a resolved Promise with a value and Promise.reject() to create a rejected Promise. Also shows resolving a Promise with another Promise. ```js const getJson = () => new Promise(resolve => { resolve({ message: "Hello, world!", success: true }); }); Promise.resolve(42).then(x => console.log(x)); // -> 42 Promise.reject(42).catch(error => console.log(error)); // -> 42 Promise.resolve(getJson()); // => ES promise ``` -------------------------------- ### Bind Console Method Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/function.md Binds the 'log' method of the console object with a pre-filled argument. This example demonstrates partial application of a method. ```javascript console.log.bind(console, 42)(43); // -> 42 43 ``` -------------------------------- ### Array.prototype.copyWithin() Method Source: https://github.com/zloirock/core-js/blob/master/README.md Copies a sequence of elements within an array to another position in the same array. Supports target, start, and optional end parameters. ```typescript copyWithin(target: number, start: number, end?: number): this; ``` -------------------------------- ### Using Promise.withResolvers Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/promise.md Demonstrates the creation of a Promise with explicit resolve and reject methods exposed. ```javascript const d = Promise.withResolvers(); d.resolve(42); d.promise.then(console.log); // -> 42 ``` -------------------------------- ### Fill Array with a Static Value Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/array.md The fill method changes all elements in an array to a static value, from a start index up to (but not including) an end index. ```javascript Array(5).fill(42); // => [42, 42, 42, 42, 42] ``` -------------------------------- ### Basic Promise Chaining Example Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/features/ecmascript/promise.md Demonstrates creating a Promise that resolves after a delay and chaining multiple .then() calls to handle asynchronous operations sequentially. Includes error handling with .catch(). ```js function sleepRandom(time) { return new Promise((resolve, reject) => { setTimeout(resolve, time * 1e3, 0 | Math.random() * 1e3); }); } console.log('Run'); // -> Run sleepRandom(5).then(result => { console.log(result); // -> 869, after 5 sec. return sleepRandom(10); }).then(result => { console.log(result); // -> 202, after 10 sec. }).then(() => { console.log('immediately after'); // -> immediately after throw new Error('Irror!'); }).then(() => { console.log('will not be displayed'); }).catch(error => console.log(error)); // -> Error: Irror! ``` -------------------------------- ### Map Iteration Example Source: https://github.com/zloirock/core-js/blob/master/README.md Illustrates various ways to iterate over a Map, including using `for...of` loops with entries, values, keys, and explicitly with `map.entries()`. Shows how to access both keys and values during iteration. ```javascript map = new Map([['a', 1], ['b', 2], ['c', 3]]); for (let [key, value] of map) { console.log(key); // => 'a', 'b', 'c' console.log(value); // => 1, 2, 3 } for (let value of map.values()) console.log(value); // => 1, 2, 3 for (let key of map.keys()) console.log(key); // => 'a', 'b', 'c' for (let [key, value] of map.entries()) { console.log(key); // => 'a', 'b', 'c' console.log(value); // => 1, 2, 3 } ``` -------------------------------- ### Transpiled Array Includes with Babel Runtime CoreJS3 Source: https://github.com/zloirock/core-js/blob/master/docs/2019-03-19-core-js-3-babel-and-a-look-into-the-future.md This example shows how `array.includes(something)` is transpiled using `@babel/runtime-corejs3` to support instance methods. ```javascript array.includes(something); ``` ```javascript import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; _includesInstanceProperty(array).call(array, something); ``` -------------------------------- ### DataView get / set Uint8Clamped methods Source: https://github.com/zloirock/core-js/blob/master/README.md Adds `getUint8Clamped` and `setUint8Clamped` methods to the DataView prototype for handling clamped 8-bit unsigned integers. ```APIDOC ## DataView get / set Uint8Clamped methods ### Description Adds `getUint8Clamped` and `setUint8Clamped` methods to the DataView prototype. ### Methods - `DataView.prototype.getUint8Clamped(offset: any): uint8` - `DataView.prototype.setUint8Clamped(offset: any, value: any): void` ### Examples ```js const view = new DataView(new ArrayBuffer(1)); view.setUint8Clamped(0, 100500); console.log(view.getUint8Clamped(0)); // => 255 ``` ``` -------------------------------- ### Disable Core-js Postinstall Message Source: https://github.com/zloirock/core-js/blob/master/docs/web/docs/usage.md Prevent the core-js postinstall message from appearing during npm installation by setting environment variables or using npm loglevel. ```sh ADBLOCK=true npm install ``` ```sh DISABLE_OPENCOLLECTIVE=true npm install ``` ```sh npm install --loglevel silent ``` -------------------------------- ### Run Linting Source: https://github.com/zloirock/core-js/blob/master/CONTRIBUTING.md Execute the linter to check code style compliance. ```sh npm run lint ```