Have your Maps and eat it, too.
Extends functionality of the map to be more like an array.
- Install by running
npm install --save-dev @cspruit/array-like-map
in the same directory as the package.json file lives.
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);
- Takes a map and returns a sub-map of all results that are true.
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}]
- Converts a map's values into an array
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}]
- Converts a map's keys into an array
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']
- Removes and returns a value from a map
- Works similar to Map.delete, but returns the value removed instead of a boolean
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'
- Searches and returns the first value that is true for the condition
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}
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.
This project uses GPL 3.0.