Axial Map
A wrapper for a JS native Map object to make traversing them a bit easier.
Why
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.
Rotating object store
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.
CodeSandbox Demo
To see an example and to try it out go to the codesandbox.io page.
Documentation
Table of Contents
- constructor
- add
- fifo
- remove
- firstKey
- first
- nthKey
- nth
- lastKey
- last
- get
- setCursor
- current
- nextOrLast
- next
- previousOrFirst
- previous
- slice
- splice
- each
- reset
constructor
Parameters
opts
Object (optional, default{}
)
Returns void
add
Parameters
key
(string | integer)object
any
Returns void
fifo
First in first out. Remove the oldest item from the map and the keys array
Returns void
remove
Removes and returns an element from the map given its key.
Parameters
key
(string | integer)
Returns (Array | null)
firstKey
Returns the first key of the keys array or null.
Returns (string | null)
first
Returns the first item in the map.
Returns (any | null)
nthKey
Return the (n)th key of the keys array.
Parameters
n
integer
Returns (string | null)
nth
Returns the (n)th item of the map or null.
Parameters
n
any
Returns (any | null)
lastKey
Returns the last key of the keys array
Returns (string | null)
last
Returns the last item of the map or null.
Parameters
n
any
Returns (any | null)
get
Get an item from the map given its key.
Parameters
key
string
Returns (any | null)
setCursor
Set the cursor to the position of key
in the keys array.
Parameters
key
string
Returns void
current
Returns the item of the map that corresponds to the current cursor location.
Returns (any | null)
nextOrLast
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)
next
Advances the cursor and returns the next item. If the "next" item is out of
bounds null
is returned.
Returns (any | null)
previousOrFirst
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)
previous
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)
slice
Copies a selection of the map and returns a multidimensional array containing
keys and items from start
to end
exclusive.
Parameters
start
integerend
integer (optional, defaultInfinity
)
Returns Array
splice
Removes a group of items from start
to end
exclusive. The removed items are returned as a multidimensional array containing keys and items.
Parameters
start
integerdeleteCount
integer (optional, defaultInfinity
)
each
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).
Parameters
callback
function
Returns void
reset
Reset the AxialMap to it's initial state. The maxSize
property is maintained.