circularr
Circular fixed size array.
Install
$ yarn add circularr
Usage
// create from // create new with fixed size // fill using valuearr.fill0 // [0, 0, 0] // shift in some valuesarr.shift16 // [0, 0, 16]arr.shift32 // [0, 16, 32] console.log...arr // undefined, 16, 32
Circularr
implements iterable
protocol, so it can be used with any standard iterable syntax
// array destructuring // destructuring copy // for..offor of array
API
fill
Fills the array using value, effectively resetting it.
fillvalue: T: this
// mutate fillarray.fill0 // [0, 0, 0]
shift
Pushes value to the end of the array, wherein the first value gets popped out and returned.
shiftvalue: T: T
array.shift8 // [0, 0, 8] => 0array.shift16 // [0, 8, 16] => 0array.shift32 // [8, 16, 32] => 0array.shift64 // [16, 32, 64] => 8array.length // 3
unshift
Pushes the value to the front of the array, popping the last value out.
unshiftvalue: T: T
array.unshift8 // [8, 0, 0] => 0array.unshift16 // [16, 8, 0] => 0array.unshift32 // [32, 16, 8] => 0array.unshift64 // [64, 32, 16] => 8array.length // 3
slice
Does work the same way as Array.slice
.
slicestartIndex?: number, endIndex?: number: Circularr<T>
trim
Removes undefined
values from both ends.
trim: Circularr<T>
array.shift1array.shift2
at
Returns element at the index or undefined
for negative or overflow indices.
atindex: number: T | undefined
array.shift1array.shift2
wrapAt
Returns element at the index. For negative and overflow indices - the index will be wrapped around, and correct value will be returned.
wrapAtindex: number: T | undefined
array.shift1array.shift2