1.2.0 • Published 4 years ago

es6-mapify v1.2.0

Weekly downloads
90,022
License
Apache-2.0
Repository
github
Last release
4 years ago

ES6-Mapify

Convert JS Objects to ES6 Maps and vice versa.

ES6 Map objects are really nice for iteration, but they're not so nice for directly referencing properties, the way JS Objects are. This is a nice way to convert back and forth. First, simply use npm to include es6-mapify in your project's dependencies:

npm install -S es6-mapify

Now you can import it and use it like so:

import { mapify } from 'es6-mapify';

// converts basic objects
let myObj = {foo: 'bar'};
let myMap = mapify(myObj);
myMap.get('foo'); // 'bar'

// doesn't do anything to non-objects
mapify('foo'); // 'foo';
mapify(null);  // null

// is smart about objects nested inside arrays and other objects
let arrMap = mapify([1, {foo: 'bar'}, 3]);
arrMap[2];            // 3
arrMap[1].get('foo'); // 'bar'

let myMap = mapify({foo: {bar: 'baz'}});
myMap.get('foo').get('bar'); // 'baz';

Of course, you might want to go the other direction too! If you have a Map and want the corresponding basic JS object, just use demapify:

import { demapify } from 'mapify';

// converts basic maps
let myMap = new Map();
myMap.set('foo', 'bar');
demapify(myMap); // {foo: 'bar'}

// doesn't do anything to non-objects
demapify(2); // 2

// is smart about nested Maps (and Maps in arrays)
let myMap = new Map()
  , myMap2 = new Map();
myMap.set('foo', 'bar');
myMap2.set('baz', 'quux');
myMap.set('inception', myMap2);
demapify(myMap); // {foo: 'bar', inception: {baz: 'quux'}}