2.0.6 • Published 4 years ago

factory-map v2.0.6

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Factory Map

Extension of ECMAScript 6 Map to provide default values where no key exists.

Replace code like:

    let map = Map();
    // ...
    let foo = map.get('foo');
    if (foo === undefined) {
      map.set('foo', 1);
    }

with

    let map = FactoryMap();
    // ...
    let foo = map.get('foo', () => 1);

Usage

Use just like a normal ECMAScript 6 Map with the following two additions:

  1. Provide an optional factory function to get(). If the key does not exist in the map, then the factory function is called to create a value. This value is inserted into the map at the given key and returned.

        let map = new FactoryMap();               // create empty map
        let value = map.get('foo', key => 'bar')  // query for a key that does not exist
        console.log(value);                       // print 'bar'
  2. Provide an optional factory function to as the last parameter to FactoryMap constructor. This factory function applies to all calls to get(). A factory function can still be passed to get() to override.

        let map1 = new FactoryMap(key => 'bar');
        let value1 = map1.get('foo')
        console.log(value1);                       // 'bar'
    
        let map2 = new FactoryMap([['baz', 'x']], key => 'bar');
        let value2 = map2.get('foo')
        console.log(value2);                       // 'bar'
    
        let map3 = new FactoryMap(key => 'bar');
        let value3 = map3.get('foo', key => 'bat')
        console.log(value3);                       // 'bat'

License

Distributed under MIT license

Contributing

Feedback and contributions are welcome. All code changes should be accompanied by suitable tests to be run with:

npm test

2.0.5

4 years ago

2.0.4

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

8 years ago