map-with-default
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

map-with-default

What is this package?

npm package containing a class similar to Map, but have a default value for a missing key.

Where to use it?

You might be wondering what is the use of having a default value for a map? Even I was surprised by seeing it fit at so many places in my regular projects to ease the life.

Here are some sample usages:

1). A good replacement for switch-case?

function fileIconToUse(format: string): string {
  switch(format) {
    case 'pdf':
      return 'file-pdf'
    case 'xls':
      return 'file-excel'
    case 'zip':
      return 'file-archive'
    default:
      return 'file'
  }
}

const fileIconMap = new MapWithDefault<string, string>([
  ['pdf', 'file-pdf'],
  ['xls', 'file-excel'],
  ['zip', 'file-archive']
], 'file');

2). Could replace your select list array?

const isSelected = [false, false, false];
<Checkbox checked={isSelected[index]} />

const isSelectedMap = new MapWithDefault<number, boolean>([], false);
<Checkbox checked={isSelectedMap.get(index)} />

// With efficient reset/clear?

Package Sidebar

Install

npm i map-with-default

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

4.39 kB

Total Files

4

Last publish

Collaborators

  • hchoudhary