1.1.1 • Published 3 years ago

bidirectional-map v1.1.1

Weekly downloads
2,111
License
ISC
Repository
github
Last release
3 years ago

Bidirectional Map

Create "key/value" collections of one-to-one correspondence. Internally it uses two Map objects, so both keys and values can be of any type, and keys can preserve their type.

Install

npm install bidirectional-map

Usage

    import BiMap from 'bidirectional-map'
    
    // Create an empty map
    let map = new BiMap()
    map.set('bob', 'alice')
    map.get('bob')          // 'alice'
    map.getKey('alice')     // 'bob'

    // Create a map with some key/values
    let map2 = new BiMap({
        bob: 'alice',
        john: 'mary'
    })
    map2.has('bob')          // true
    map2.hasValue('mary')    // true
    map2.deleteValue('mary')

API

Constructors

ArgumentsDescription
Creates an empty structure
object: ObjectCreates a structure with the key/values of object

Properties

PropertyReturn TypeDescription
sizeanyReturns the number of key/value pairs

Methods

MethodArgumentsReturn TypeDescription
setkey: any, value: anySets a new key/value pair
getkey: anyanyReturns the value
getKeyvalue: anyanyReturns the key
clearRemoves all key/value pairs
deletekey: anyDeletes a key/value pair by key
deleteValuevalue: anyDeletes a key/value pair by value
entriesMapIteratorReturns a new Iterator object that contains an array of key, value for each element in the structure
haskey: anybooleanReturns true if it exists a pair with the provided key, false otherwise
hasValuevalue: anybooleanReturns true if it exists a pair with the provided value, false otherwise
keysMapIteratorReturns a new Iterator object that contains the keys for each element in the structure
valuesMapIteratorReturns a new Iterator object that contains the values for each element in the structure
getObjectObjectParses the internal "one direction" map to a plain {}. Throws exception if the map has non-primitive key types
getObjectReverseObjectParses the internal "one direction" reverse map to a plain {}. Throws exception if the reverse map has non-primitive key types

Changelog

  • 1.1.0
    • Add getObject() and getObjectReverse() methods
  • 1.0.0
    • Initial release :tada:

License

ISC License

1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

6 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago