What is fromentries?
The 'fromentries' npm package provides a polyfill for the Object.fromEntries method, which transforms a list of key-value pairs into an object. This is particularly useful for converting Map objects or arrays of pairs into plain JavaScript objects.
What are fromentries's main functionalities?
Convert Array of Pairs to Object
This feature allows you to convert an array of key-value pairs into a plain JavaScript object. This is useful for scenarios where data is received in a pair format and needs to be transformed into an object.
const fromEntries = require('fromentries');
const entries = [['name', 'Alice'], ['age', 30]];
const obj = fromEntries(entries);
console.log(obj); // { name: 'Alice', age: 30 }
Convert Map to Object
This feature allows you to convert a Map object into a plain JavaScript object. This is useful for scenarios where data is stored in a Map and needs to be transformed into an object for easier manipulation.
const fromEntries = require('fromentries');
const map = new Map([['name', 'Bob'], ['age', 25]]);
const obj = fromEntries(map);
console.log(obj); // { name: 'Bob', age: 25 }
Other packages similar to fromentries
object.fromentries
The 'object.fromentries' package is another polyfill for the Object.fromEntries method. It provides similar functionality to 'fromentries' by converting arrays of key-value pairs or Map objects into plain JavaScript objects. Both packages serve the same purpose, but 'object.fromentries' might have different implementation details or additional features.
lodash
Lodash is a utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. While it is not a direct polyfill for Object.fromEntries, Lodash offers similar functionality through its '_.fromPairs' method, which converts arrays of key-value pairs into objects. Lodash is more comprehensive and includes many other utility functions beyond just converting pairs to objects.
fromentries
Object.fromEntries() ponyfill (in 6 lines)
Install
npm install fromentries
Why this package?
Existing polyfill packages (like
object.fromentries
)
pull in a bunch of dependencies and adds over 8
KB to the browser bundle size. This allows them to work in ES3 environments
like IE6, but it's also overkill; almost no one supports IE6 anymore.
I'd rather not ship tons of extra code to website visitors. A polyfill for this
feature can be implemented in a few short lines of code using modern language
features. That's what fromentries
(this package) does.
This means that fromentries
only works in evergreen browsers like:
- Chrome
- Firefox
- Edge
- Safari
- Opera
It does not work in browsers like IE11 and older (unless you transpile it first).
Usage
const fromEntries = require('fromentries')
const map = new Map([ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ])
const obj = fromEntries(map)
constole.log(obj)
const searchParams = new URLSearchParams('foo=bar&baz=qux')
const obj2 = fromEntries(searchParams)
console.log(obj2)
What is a ponyfill?
A ponyfill is almost the same as a polyfill, but not quite. Instead of
patching functionality for older browsers, a ponyfill provides that
functionality as a standalone module you can use.
Read more at PonyFoo.
See also
License
MIT. Copyright (c) Feross Aboukhadijeh.