Skip to content

A map that sets a default value when an unset key is requested.

License

Notifications You must be signed in to change notification settings

nlcgits/defaultmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

defaultmap

A map that sets a default value when an unset key is requested.

Just like a regular map, except you pass a default value to the constructor and that value is set whenever you try to get an unset key.

Constructor

const { DefaultMap } = require('@nextlevelcoder/DefaultMap');

let staticMap = new DefaultMap(7); // 7
console.log(staticMap.get(1)); // 7
console.log(staticMap.get({})); // 7
console.log(staticMap.get([])); // 7

let classMap = new DefaultMap(() => new SimpleState());
console.log(classMap.get('accountState')) // a new SimpleState instance
console.log(classMap.get('networkState')) // a new SimpleState instance

let dynamicMap = new DefaultMap((key) => key);
console.log(dynamicMap.get(1)); // 1
console.log(dynamicMap.get({})); // {}
console.log(dynamicMap.get([])); // []

About

A map that sets a default value when an unset key is requested.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages