@cspruit/array-like-map
v1.2.5
Published
Extends functionality of the map to be more like an array
Downloads
4
Readme
Array-Like-Map
Have your Maps and eat it, too.
Extends functionality of the map to be more like an array.
Installation
- Install by running
npm install --save-dev @cspruit/array-like-map
in the same directory as the package.json file lives.
Usage
import {mapFilter} from '@cspruit/array-like-map';
const animals = new Map();
animals.set('dog', { isPet: true});
animals.set('tiger', { isPet: false});
const pets = mapFilter(animals, value => value.isPet);
MapFilter
- Takes a map and returns a sub-map of all results that are true.
Example
import {mapFilter} from '@cspruit/array-like-map';
const animals = new Map();
animals.set('dog', { name: 'Spot', isPet: true});
animals.set('dragon', { name: 'Smaug', isPet: false});
const pets = mapFilter(animals, value => value.isPet);
// Should return a map of ['dog', {isPet: true}]
MapToArray
- Converts a map's values into an array
Example
import {mapToArray} from '@cspruit/array-like-map';
const animals = new Map();
animals.set('dog', { name: 'Spot', isPet: true});
animals.set('dragon', { name: 'Smaug', isPet: false});
const pets = mapToArray(animals);
// Should return [{name: 'Spot', isPet: true}, {name: 'Smaug', isPet: false}]
MapKeysToArray
- Converts a map's keys into an array
Example
import {mapToArray} from '@cspruit/array-like-map';
const animals = new Map();
animals.set('dog', { name: 'Spot', isPet: true});
animals.set('dragon', { name: 'Smaug', isPet: false});
const pets = mapToArray(animals);
// Should return ['dog', 'dragon']
MapPop
- Removes and returns a value from a map
- Works similar to Map.delete, but returns the value removed instead of a boolean
Example
import {mapPop} from '@cspruit/array-like-map';
const animals = new Map();
const dog = {name: 'Spot', isPet: true};
const dragon = {name: 'Smaug', isPet: false};
animals.set('dog', dog);
animals.set('dragon', dragon);
const removedDragon = mapPop(animals, 'dragon');
// Should return {name: 'Smaug', isPet: false} and the map now only has 'dog'
MapFind
- Searches and returns the first value that is true for the condition
Example
import {mapFind} from '@cspruit/array-like-map';
const animals = new Map();
const dog = {name: 'Spot', isPet: true};
const dragon = {name: 'Smaug', isPet: false};
animals.set('dog', dog);
animals.set('dragon', dragon);
const foundDog = mapFind(animals, (pet) => pet.name === 'Spot');
// Should return {name: 'Spot', isPet: true}
Contributing
All contributions, suggestions, and issues are welcome!
Check out the Issues page. In general anything listed is up for grabs, though bugs tend to be more detailed than enhancements and might be better to pick up if starting out.
License
This project uses GPL 3.0.