A wrapper for a JS native Map object to make traversing them a bit easier.
npm install axial-mapA wrapper for a JS native Map object to make traversing them a bit easier.
This is an effort to combine the features of a native Javascript Map object with a double-linked list. Data within the structure can be accessed using a key like any normal JS object but can also maintain an order and be traversed using next() and previous() methods. A cursor can be set to prevent the need to loop through an entire object.
In addition to the traversability of the object a user can directly set the cursor to a specific key. Other convenience methods like slice(), splice(), and each() have been added.
The Axial Map can also be instantiated with a maxSize property. When the map is "full" and a new element is added to the map, the map will use an internal fifo() method (First In First Out) to remove the oldest element in the map. This is useful for keeping an ordered rotating map of data.
To see an example and to try it out go to the codesandbox.io page.
- [constructor][1]
- [add][3]
- [fifo][5]
- [remove][28]
- [firstKey][6]
- [first][7]
- [nthKey][8]
- [nth][10]
- [lastKey][12]
- [last][13]
- [get][15]
- [setCursor][17]
- [current][19]
- [nextOrLast][20]
- [next][21]
- [previousOrFirst][22]
- [previous][23]
- [slice][24]
- [splice][26]
- [each][30]
- [reset][32]
- opts [Object][33] (optional, default {})
Returns void
- key ([string][34] | integer)
- object any
Returns void
First in first out. Remove the oldest item from the map and the keys array
Returns void
Removes and returns an element from the map given its key.
- key ([string][34] | integer)
Returns ([Array][35] | null)
Returns the first key of the keys array or null.
Returns ([string][34] | null)
Returns the first item in the map.
Returns (any | null)
Return the (n)th key of the keys array.
- n integer
Returns ([string][34] | null)
Returns the (n)th item of the map or null.
- n any
Returns (any | null)
Returns the last key of the keys array
Returns ([string][34] | null)
Returns the last item of the map or null.
- n any
Returns (any | null)
Get an item from the map given its key.
- key [string][34]
Returns (any | null)
Set the cursor to the position of key in the keys array.
- key [string][34]
Returns void
Returns the item of the map that corresponds to the current cursor location.
Returns (any | null)
Advances the cursor from its current position and returns the item of the map
that corresponds with the new cursor position. If the cursor would advance out
of bounds, then the current position (the last position) is maintained and
that item is returned.
null is returned if the map is empty.
Returns (any | null)
Advances the cursor and returns the next item. If the "next" item is out of
bounds null is returned.
Returns (any | null)
Rolls back the cursor 1 position from its current position and returns the
item of the map that corresponds with the new cursor position. If the cursor
would roll back out of bounds, then the current position (the first position)
is maintained and that item is returned.
null is returned if the map is empty.
Returns (any | null)
Rolls back the cursor 1 position and returns the next item. If the "previous"
item is out of bounds null is returned.
Returns (any | null)
Copies a selection of the map and returns a multidimensional array containing
keys and items from start to end exclusive.
- start integer
- end integer (optional, default Infinity)
Returns [Array][35]
Removes a group of items from start to end exclusive. The removed items are returned as a multidimensional array containing keys and items.
- start integer
- deleteCount integer (optional, default Infinity)
Will iterate over each item in the map invoking the provided callback. The
callback is provided three arguments. The element itself, the key, and the
iteration number (index).
- callback [function][36]
Returns void
Reset the AxialMap to it's initial state. The maxSize property is maintained.
[1]: #constructor
[2]: #parameters
[3]: #add
[4]: #parameters-1
[5]: #fifo
[6]: #firstkey
[7]: #first
[8]: #nthkey
[9]: #parameters-2
[10]: #nth
[11]: #parameters-3
[12]: #lastkey
[13]: #last
[14]: #parameters-4
[15]: #get
[16]: #parameters-5
[17]: #setcursor
[18]: #parameters-6
[19]: #current
[20]: #nextorlast
[21]: #next
[22]: #previousorfirst
[23]: #previous
[24]: #slice
[25]: #parameters-7
[26]: #splice
[27]: #parameters-8
[28]: #remove
[29]: #parameters-9
[30]: #each
[31]: #parameters-10
[32]: #reset
[33]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[35]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[36]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function