From 2e1c254855daee5fa8e4ffb2b7b0c8994253b399 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 25 Apr 2022 09:58:45 +0800 Subject: [PATCH] Add Object#fromEntries polyfill (with 6 lines of code) --- packages/next-polyfill-module/src/index.js | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/next-polyfill-module/src/index.js b/packages/next-polyfill-module/src/index.js index 73b6c052f089..d1639a86c4ce 100644 --- a/packages/next-polyfill-module/src/index.js +++ b/packages/next-polyfill-module/src/index.js @@ -94,3 +94,27 @@ if (!Promise.prototype.finally) { ) } } + +/** + * Available in: + * Edge: never + * Firefox: 63 + * Chrome: 73 + * Safari: 12.1 + * + * https://caniuse.com/mdn-javascript_builtins_object_fromentries + */ +// Modified from https://github.com/tc39/proposal-object-from-entries/blob/main/polyfill.js +// Modified from https://github.com/feross/fromentries/blob/29b52a850bb3a47c390937631c2638edf3443942/index.js +// License MIT +if (!Object.fromEntries) { + Object.fromEntries = function (iterable) { + // Assume the input is either an iterable object or an array-like object + return Array.from(iterable).reduce(function (obj, entry) { + // https://github.com/tc39/proposal-object-from-entries/blob/e4837799c1586a07c101570b27997497e5290c22/polyfill.js#L9-L10 + // contract is that entry has "0" and "1" keys, not that it is an array or iterable. + obj[entry['0']] = entry['1'] + return obj + }, {}) + } +}