axial-map

1.0.7 • Public • Published

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

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

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

Returns (any | null)

setCursor

Set the cursor to the position of key in the keys array.

Parameters

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 integer
  • end integer (optional, default Infinity)

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 integer
  • deleteCount integer (optional, default Infinity)

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

Returns void

reset

Reset the AxialMap to it's initial state. The maxSize property is maintained.

Package Sidebar

Install

npm i axial-map

Weekly Downloads

5

Version

1.0.7

License

ISC

Unpacked Size

36.6 kB

Total Files

7

Last publish

Collaborators

  • sarcastron