1.1.0 • Published 2 years ago

multi-index-map v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

MultiIndexMap

npm version install size bundle size npm downloads

Browser Support

ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔11 ✔

Installing

Using npm:

$ npm install multi-index-map

Using yarn:

$ yarn add multi-index-map

Usage :

 
   const contacts = [
     { id: 'ddd-33sdfia', email: 'unique1@email.com', number: '8787898989', state: 'India', ... },
     { id: 'eee-swwwadd', email: 'unique2@email.com', number: '676776676', state: 'US', ... },
     { id: '222-33sdssf', email: 'unique3@email.com', number: '898976789', state: 'India', ...}
     ...
   ];
   // id, email, number are unique properties in the among the above contacts array

   // we can form a map based on these unique properties
   const contactsMap = new MultiIndexMap(['id', 'email', 'number'], contacts);

   // now you can use one of the above three properties to get access to the objects
   console.log(contactsMap.get('id', 'eee-swwwadd'));
   // { id: 'ddd-33sdfia', email: 'unique1@email.com', number: '8787898989' ... }
   console.log(contactsMap.get('email', 'unique3email.com'))
   // { id: '222-33sdssf', email: 'unique3@email.com', number: '898976789', state: 'India', ...}
   console.log(contactsMap.get('number', '676776676'))
   // { id: 'eee-swwwadd', email: 'unique2@email.com', number: '676776676'..},

   // provides methods which Map has like has, set, delete methods
   console.log(contactsMap.has('email', 'unique3email.com')) // true
   console.log(contactsMap.delete('id', '222-33sdssf')) // removes item with id : 222-33sdssf
   
   const newContact = { id: '4443-2sss8',email: 'unique88@email.com', number: '999090090', state: 'India', ...};
   contactsMap.set(newContact); // will add the item to the map

Time and Space Complexities

where m: no of indexes, n: no of items 1. only properties are indexed and items are not duplicated: m-1 indextables n items each = O((m - 1) * n) 1 main table with n items = O(n) 2. operations

operationcomplexity
new MultiIndex(..)O(m*n)
getO(1)
setO(m)
hasO(1)
deleteO(m)

Resources

License

MIT