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

json-set-map

v1.1.2

Published

Set and map extension that support JSON serialization and de-serialization

Downloads

18,221

Readme

NPM version Build Status Coverage Status

json-set-map

Set and Map extension that support JSON serialization and de-serialization

The original Set and Map object are not modified, so can still be used.

This library does not provides polyfills for Set and Map.

Install

yarn add json-set-map

The current version supports node v10.x+ and modern browsers.

Usage

  • Typescript and es6
import { Set, Map } from 'json-set-map'
  • Nodejs
var Set = require('json-set-map').Set;
var Map = require('json-set-map').Map;

The unmodified Set and Map class can still be used:

import { Set as jSet, Map as jMap } from 'json-set-map'
let originalMap = new Map()
let originalSet = new Set()
let serializableMap = new jMap()
let serializableSet = new jSet()

API

Method toJSON

Called automaticly by JSON.stringify

// Set
> originalSet.add(1).add(2)
< Set { 1, 2 }
> JSON.stringify(originalSet)
< '{}'
> serializableSet.add(1).add(2)
< SerializableSet { 1, 2 }
> JSON.stringify(serializableSet)
< '[1,2]'

// Map
> originalMap.set(1,2).set(3,4)
< Map { 1 => 2, 3 => 4 }
> JSON.stringify(originalMap)
< '{}'
> serializableMap.add(1).add(2)
< SerializableMap { 1 => 2, 3 => 4 }
> JSON.stringify(serializableMap)
< '[[1,2],[3,4]]'

Method fromJSON

Static method useful to load a Set or Map from the object returned by JSON.parse. A parser function can be used to load the objects of the array as shown in the example below.

> import { Set, Map } from 'json-set-map'

// Used without a parser it's the same as calling new Set
> Set.fromJSON(["2016-05-16T22:26:47.534Z","2016-05-16T22:26:47.533Z"])
< SerializableSet { '2016-05-16T22:26:47.534Z', '2016-05-16T22:26:47.533Z' } // Items are strings

// A parser function can be used to load the object.
> let parser=(d) => new Date(d)
> Set.fromJSON(["2016-05-16T22:26:47.534Z","2016-05-16T22:26:47.533Z"], { parser })
< SerializableSet { 2016-05-16T22:26:47.534Z, 2016-05-16T22:26:47.533Z } // Items are Dates

// Used without a parser it's the same as calling new Map 
> Map.fromJSON([['KeY','ValuE']])
< SerializableMap { 'KeY' => 'ValuE' }

// Separate parser function for the key and the value can be used.
> let keyParser = (item) => item.toUpperCase()
> let valueParser = (item) => item.toLowerCase()
> Map.fromJSON([['KeY','ValuE']], {keyParser, valueParser})
< SerializableMap { 'KEY' => 'value' }

Refer to the JSDoc documentation on the files for mode details.

Node

JSON does not support undefined and is replaced with null. This prevents the correct de-serialization of a Set with both undefined and null as element or of a Map with both element as key.

const str = JSON.stringify(new jSet().add(null).add(undefined)) // => '[null,null]'
jSet.fromJSON(JSON.parse(str))                                  // => SerializableSet { null }