What is map-or-similar?
The map-or-similar npm package provides a simple and flexible way to create and manage map-like collections. It allows for the easy manipulation of key-value pairs with a focus on providing a consistent interface that works similarly to the native JavaScript Map object, but with additional utilities and fallbacks for environments where Map is not available or desired.
What are map-or-similar's main functionalities?
Creating a new map
This feature allows users to create a new map-like collection. The collection can store key-value pairs, similar to the native JavaScript Map.
const MapOrSimilar = require('map-or-similar');
const myMap = new MapOrSimilar();
Setting and getting values
Users can set key-value pairs in the collection and retrieve values by their keys. This functionality mimics that of the JavaScript Map, providing a familiar interface for managing collections.
myMap.set('key', 'value');
const value = myMap.get('key');
Checking for key existence
This feature allows users to check whether a specific key exists in the collection. It's useful for conditionally adding or modifying data based on the presence of keys.
const hasKey = myMap.has('key');
Other packages similar to map-or-similar
lodash
Lodash is a comprehensive utility library that offers a wide range of functions for manipulating objects, arrays, and more. While it provides some similar functionalities to map-or-similar, such as managing collections and key-value pairs, lodash is much broader in scope and includes a vast array of utilities beyond just map-like collections.
immutable
Immutable.js offers persistent data structures including Map, List, Set, and more. Unlike map-or-similar, which provides a simple map-like interface, Immutable.js focuses on immutable collections. This ensures that the original collection cannot be modified, offering a different approach to collection management that can be beneficial for certain applications.
Map Or Similar
Returns a JavaScript Map() or a similar object with the same interface, if Map is not available.
Focuses on performance.
No dependencies.
Made for the browser and nodejs.
Install
npm install map-or-similar --save
Use
var MapOrSimilar = require('map-or-similar');
var myMap = new MapOrSimilar();
myMap.set('key1', 'value1');
myMap.set({ val: 'complex object as key' }, 'value2');
The following methods and properties are supported identically to Map():
set(key, val) : Sets a value to a key. Key can be a complex object, array, etc.
get(key) : Returns the value of a key.
has(key) : Returns true if the key exists, otherwise false.
delete(key) : Deletes a key and its value.
forEach(callback) : Invokes callback(val, key, object) once for each key-value pair in insertion order.
size : Returns the number of keys-value pairs.
Does not support other Map methods or properties.
Test
npm run test
License
Released under an MIT license.
Other Libs
- Memoizerific: Fastest, smallest, most-efficient JavaScript memoization lib to memoize JS functions.
- Multi Key Cache: A JavaScript (JS) cache that can have multiple complex values as keys.