New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fixed-size-list

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fixed-size-list

Immutable fixed-length list (a.k.a circular buffer) with an event emitter for Typescript and Javascript

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
237
decreased by-35.07%
Maintainers
1
Weekly downloads
 
Created
Source

fixed-size-list Build Status

Immutable fixed-length list (a.k.a circular buffer) with an event emitter for Typescript and Javascript

Quick start

npm i fixed-size-list
import { FixedSizeList } from 'fixed-size-list'

const maxSize = 3
const fixedSizeList = new FixedSizeList<number>(maxSize)
// Now it's empty
fixedSizeList.add(1)
// Not it's [ 1 ]
fixedSizeList.add(2)
// Now it's [ 2, 1 ]
fixedSizeList.add(3)
// Now it's [ 3, 2, 1 ]
fixedSizeList.add(4)
// Now it's [ 4, 3, 2 ]

console.log(fixedSizeList.data)
// logs [4,3,2]

fixedSizeList.reset()
// Now it's []

Initial list

You can set initial values easily passing them to the constructor

import { FixedSizeList } from 'fixed-size-list'

const maxSize = 3
const list = [1, 2, 3]
const fixedSizeList = new FixedSizeList<number>(maxSize, list)
// Now it's [ 1, 2, 3 ]

Be aware that the initial list is truncated if it's longer than maxSize

import { FixedSizeList } from 'fixed-size-list'

const maxSize = 2
const list = [1, 2, 3]
const fixedSizeList = new FixedSizeList<number>(maxSize, list)
// Now it's [ 1, 2 ]
// 3 was truncated

Events

FixedSizeList has an event emitter. You can listen to specific events. WARNING! on returns an unsubscribe function. Do not forget to call it when you no longer need the subscription to unsubscribe.

eventNewItem

It emits an added item

import { FixedSizeList, eventNewItem } from 'fixed-size-list'

const maxSize = 2
const fixedSizeList = new FixedSizeList<number>(maxSize)
const unsubscribe = fixedSizeList.on(eventNewItem, (newItem) => console.log('item added', newItem))
fixedSizeList.add(5)
// logs 'item added 5'

// later on
unsubscribe()

eventTruncate

It emits an array of removed items

import { FixedSizeList, eventTruncate } from 'fixed-size-list'

const maxSize = 2
const fixedSizeList = new FixedSizeList<number>(maxSize)
const unsubscribe = fixedSizeList.on(eventTruncate, (removedItems) =>
  console.log('items removed', removedItems.toString()),
)
fixedSizeList.add(5)
fixedSizeList.add(4)
fixedSizeList.add(3)
// logs 'items removed [ 5 ]'

// later on
unsubscribe()

eventReset

import { FixedSizeList, eventReset } from 'fixed-size-list'

const maxSize = 2
const fixedSizeList = new FixedSizeList<number>(maxSize)
const unsubscribe = fixedSizeList.on(eventReset, () => console.log('list reset'))
fixedSizeList.reset()
// logs 'list reset'

// later on
unsubscribe()

eventCreated

We can add the third optional parameter of FixedSizeList's constructor and pass a custom event emitter

import { FixedSizeList, eventCreated, IFixedSizeListEvents } from 'fixed-size-list'
import mitt from 'mitt'

const maxSize = 2
const list = []
const emitter = mitt()
const unsubscribe = emitter.on(eventCreated, () => console.log('list created'))

const fixedSizeList = new FixedSizeList<number>(maxSize, list, emitter)
// logs 'list created'

// later on
unsubscribe()

All

We can subscribe to all events at once

import { FixedSizeList, eventCreated, IFixedSizeListEvents } from 'fixed-size-list'

const fixedSizeList = new FixedSizeList<number>(10)
const unsubscribe = emitter.on('*', () => console.log('Any event'))

// later on
unsubscribe()

Keywords

FAQs

Package last updated on 01 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc