fixed-length-array
v0.1.0
Published
Array with fixed size
Downloads
3
Readme
FixedArray
npm install fixed-length-array --save
Usage
FixedArray
inherits native Array
class and all its methods.
import FixedArray from 'fixed-length-array'
new FixedArray(2) // [ , ]
new FixedArray(1, 2) // [ 1, 2 ] — yep, as native Array
FixedArray.from([ 1, 2 ]) // [ 1, 2 ]
function gen() {
yield 1
yield 2
}
FixedArray.from(gen()) // [ 1 , 2 ]
Methods push()
and unshift()
works different. If you create an array with fixed length it keeps his length, so methods push()
and unshift()
will remove elements from other end of array (shift()
and pop()
respectively). Methods pop()
and shift()
is not accesible manually.
const array = FixedArray.from([ 1, 2 ])
console.log(array.length) // 2
array.push(3)
console.log(array.length) // 2 — length not changed
console.log(array) // [ 2, 3 ]
array.unshift(1)
console.log(array.length) // still 2
console.log(array) // [ 1, 2 ]
As you can see FixedArray
is not resizable. Only way to resize is create new FixedArray
.