npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapify-ts

v0.0.2

Published

Mapify is a class that provides methods to serialize and deserialize objects supporting Map.

Downloads

3,290

Readme

Mapify-ts

Mapify-ts is a class that provides methods to serialize and deserialize objects supporting Map.


Summary


Introduction

Native JSON package does not support stringify Map out of the box.

Example 1:

const myMap = new Map();
myMap.set('foo', 'bar');
myMap.set('hello', 'world');
console.log(JSON.stringify(myMap)); // {}

Example 2:

type MyType = {
    foo: string;
    hello: string;
};
const myMap = new Map<MyType, string>();
myMap.set({ foo: 'bar', hello: 'world' }, 'foobar');
console.log(JSON.stringify(myMap)); // {}

What Mapify-ts does?

Mapify-ts will serialize the map to a native javascript object. Also enables to deserialize the object back to a Map.

This native javascript object can be serialized to JSON using stringify. And can be deserialized back.

Examples:

You can store the serialized string in a database and deserialize it later.

You can send the serialized string over the network and deserialize it on the other side.

Hope you enjoy it!


MapifyTs.serialize

Serialize a Map object to a native javascript object.

Example:

type MyType = {
    foo: string;
    hello: string;
};

const myMap = new Map<MyType, string>();

myMap.set({ foo: 'bar', hello: 'world' }, 'foobar');

console.log(myMap); // #1 console output
console.log(JSON.stringify(myMap)); // #2 console output
console.log(MapifyTs.serialize(myMap)); // #3 console output
console.log(JSON.stringify(MapifyTs.serialize(myMap))); // #4 console output

Following the example above, the console output will be:

#1 console output

This is the raw output from terminal.

> console.log(myMap);

Map(1) { { foo: 'bar', hello: 'world' } => 'foobar' }

Although it is not possible to stringify the map, it is possible to print it to the console.

#2 console output

This is the output from terminal after stringify the map.

> console.log(JSON.stringify(myMap));

{}

The stringify method does not support Map out of the box.

#3 console output

This is the output from terminal after MapifyTs.serialize the map.

> console.log(MapifyTs.serialize(myMap));

{
    __map__: { '__map:0__': { foo: 'bar', hello: 'world' } },
    '__map:0__': 'foobar'
}

The MapifyTs.serialize method will serialize the map to a native javascript object mapping the keys to a __map__ parent property.

#4 console output

This is the output from terminal after stringify the output from MapifyTs.serialize.

> console.log(JSON.stringify(MapifyTs.serialize(myMap)));

{"__map__":{"__map:0__":{"foo":"bar","hello":"world"}},"__map:0__":"foobar"}

The stringify method supports the output from MapifyTs.serialize.


MapifyTs.deserialize

Deserialize a native javascript object to a Map object.

Example:

type MyType = {
    foo: string;
    hello: string;
};

const myMap = new Map<MyType, string>();
myMap.set({ foo: 'bar', hello: 'world' }, 'foobar');

const serialized = MapifyTs.serialize(myMap);
const strSerialized = JSON.stringify(serialized);

// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

// you can store the serialized string in a database
// and deserialize it later

// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

// you can send the serialized string over the network
// and deserialize it on the other side

// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

const jsonDeserialized = JSON.parse(strSerialized);
const deserialized = MapifyTs.deserialize(jsonDeserialized);

console.log(deserialized); // #1 console output

Following the example above, the console output will be:

#1 console output

This is the output from terminal after MapifyTs.deserialize the object.

> console.log(deserialized);

Map(1) { { foo: 'bar', hello: 'world' } => 'foobar' }

The MapifyTs.deserialize method will deserialize the native javascript object to a Map object.

Pre-requisites

  • Node 16.20+
  • NPM 8.19.4+

Installation

npm i mapify-ts

Import

import MapifyTs from 'mapify-ts';

References

  • https://pauloe-me.medium.com/typescript-npm-package-publishing-a-beginners-guide-40b95908e69c
  • https://howtodoinjava.com/typescript/maps/

Change log

0.0.2

  • Force new version

0.0.1

  • Initial release
  • Serialize and deserialize Map objects
  • Serialize and deserialize Map objects with custom keys
  • Serialize and deserialize Map objects with custom values
  • Serialize and deserialize Map objects with custom keys and values
  • Serialize and deserialize Map objects with custom keys and values with nested Maps
  • Serialize and deserialize Map objects with custom keys and values with nested Maps with custom keys and values

Thanks for the help

@gustavoyasunaka @RodrigoWebDev


Contribute

Feel free to contribute to this project. I will be happy to review your PRs.

Support me

You can support me listening to my music on all streaming platforms.

Check my Linktree https://linktr.ee/idemax for all links.