A 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
- add
- fifo
- remove
- firstKey
- first
- nthKey
- nth
- lastKey
- last
- get
- setCursor
- current
- nextOrLast
- next
- previousOrFirst
- previous
- slice
- splice
- each
- reset
opts
Object (optional, default{}
)
Returns void
key
(string | 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 | integer)
Returns (Array | null)
Returns the first key of the keys array or null.
Returns (string | null)
Returns the first item in the map.
Returns (any | null)
Return the (n)th key of the keys array.
n
integer
Returns (string | 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 | 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
Returns (any | null)
Set the cursor to the position of key
in the keys array.
key
string
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
integerend
integer (optional, defaultInfinity
)
Returns Array
Removes a group of items from start
to end
exclusive. The removed items are returned as a multidimensional array containing keys and items.
start
integerdeleteCount
integer (optional, defaultInfinity
)
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
Returns void
Reset the AxialMap to it's initial state. The maxSize
property is maintained.