npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fixed-size-list

v0.3.0

Published

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

Downloads

1,654

Readme

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()