JSPM

pojo-maps

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 201
  • Score
    100M100P100Q77781F
  • License MIT

Plain old Javascript Map implementation

Package Exports

  • pojo-maps

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pojo-maps) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

POJO Maps

In using React and Redux, you may find that you have to use POJO objects instead of ES6+ native objects like Set and Map. This project aims to provide a well typed, immutable POJO Map implementation that can simplify tasks you often do with POJO Maps.

See also pojo-sets

CICD Badge

Quick start

Install the package

yarn add pojo-maps

Import and start using it!

import { PojoMap } from 'pojo-maps';

const myMap = PojoMap.fromEntries(['a', 1]. ['b', 2]);

Usage

PojoMap are meant to be a drop-in replacement everywhere you use immutable Partial<Record<T, U>> structures. Their main benefit is to handle the ambiguity in Typescript around missing & "undefined" keys.

// Traditional Record types:
declare const items: Partial<Record<string, string>>;

// Lame
Object.values(items); // type: Array<string | undefined>
// Extra lame!!
items['myvalue'] = undefined;

// PojoMap:
declare const map: PojoMap<string, string>;

// Cool!
PojoMap.values(map); // type: Array<string>
// Wow! Error!!!
PojoMap.set(map, 'myvalue', undefined); // Argument of type 'undefined' is not assignable to parameter

The traditional record types require a manual type assertion. By using immutable helper methods, PojoMap can do all of the normal record operations in a typesafe manner.

Advanced Usage

PojoMap contains helper methods to do most common Object or Record operations.

const alphaNum = PojoMap.fromEntries([['a', 1], ['b', 2], ['c', 3]] as const);

const abcd = PojoMap.set(alphaNum, 'd', 4);
const abd = PojoMap.remove(abcd, 'c');

PojoMap.keys(abd); // ['a', 'b', 'd']
PojoMap.values(abcd); // [1, 2, 3, 4]
PojoMap.entries(alphaNum); // [['a', 1], ['b', 2], ['c', 3]]

// Add additional types to your map?
const empty = PojoMap.empty<string, string>();
const withNums = PojoMap.set('a', 10);


// Convert a PojoMap into a PojoSet
const set = PojoSet.from(PojoMap.keys(alphaNum));