fixed-length-array

0.1.0 • Public • Published

FixedArray

Build Status Coverage Status Code Climate NPM version

Standard - JavaScript Style Guide

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.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i fixed-length-array

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • asdf404