{"id":16368687,"url":"https://github.com/aigoncharov/fixed-size-list","last_synced_at":"2025-03-23T02:33:39.601Z","repository":{"id":35699102,"uuid":"143414838","full_name":"aigoncharov/fixed-size-list","owner":"aigoncharov","description":"A small library that brings a fixed-length list (aka circular buffer) with an event emitter to Typescript and Javascript","archived":false,"fork":false,"pushed_at":"2022-02-11T00:26:31.000Z","size":207,"stargazers_count":8,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T00:42:00.512Z","etag":null,"topics":["array","circular-buffer","fixed-length","list","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aigoncharov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-03T10:40:22.000Z","updated_at":"2024-05-24T17:39:04.000Z","dependencies_parsed_at":"2022-08-08T10:17:11.272Z","dependency_job_id":null,"html_url":"https://github.com/aigoncharov/fixed-size-list","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Ffixed-size-list","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Ffixed-size-list/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Ffixed-size-list/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Ffixed-size-list/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aigoncharov","download_url":"https://codeload.github.com/aigoncharov/fixed-size-list/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245047985,"owners_count":20552431,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","circular-buffer","fixed-length","list","typescript"],"created_at":"2024-10-11T02:53:31.937Z","updated_at":"2025-03-23T02:33:39.357Z","avatar_url":"https://github.com/aigoncharov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fixed-size-list [![Build Status](https://travis-ci.org/keenondrums/fixed-size-list.svg?branch=master)](https://travis-ci.org/keenondrums/fixed-size-list)\n\nImmutable fixed-length list (a.k.a circular buffer) with an event emitter for Typescript and Javascript\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Quick start](#quick-start)\n- [Initial list](#initial-list)\n- [Events](#events)\n  - [eventNewItem](#eventnewitem)\n  - [eventTruncate](#eventtruncate)\n  - [eventReset](#eventreset)\n  - [eventCreated](#eventcreated)\n- [API](#api)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Quick start\n\n```\nnpm i fixed-size-list\n```\n\n```ts\nimport { FixedSizeList } from 'fixed-size-list'\n\nconst maxSize = 3\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize)\n// Now it's empty\nfixedSizeList.add(1)\n// Not it's [ 1 ]\nfixedSizeList.add(2)\n// Now it's [ 2, 1 ]\nfixedSizeList.add(3)\n// Now it's [ 3, 2, 1 ]\nfixedSizeList.add(4)\n// Now it's [ 4, 3, 2 ]\n\nconsole.log(fixedSizeList.data)\n// logs [4,3,2]\n\nfixedSizeList.reset()\n// Now it's []\n```\n\n## Initial list\n\nYou can set initial values easily passing them to the constructor\n\n```ts\nimport { FixedSizeList } from 'fixed-size-list'\n\nconst maxSize = 3\nconst list = [1, 2, 3]\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize, list)\n// Now it's [ 1, 2, 3 ]\n```\n\nBe aware that the initial list is truncated if it's longer than maxSize\n\n```ts\nimport { FixedSizeList } from 'fixed-size-list'\n\nconst maxSize = 2\nconst list = [1, 2, 3]\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize, list)\n// Now it's [ 1, 2 ]\n// 3 was truncated\n```\n\n## Events\n\nFixedSizeList 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.\n\n### eventNewItem\n\nIt emits an added item\n\n```ts\nimport { FixedSizeList, eventNewItem } from 'fixed-size-list'\n\nconst maxSize = 2\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize)\nconst unsubscribe = fixedSizeList.on(eventNewItem, (newItem) =\u003e console.log('item added', newItem))\nfixedSizeList.add(5)\n// logs 'item added 5'\n\n// later on\nunsubscribe()\n```\n\n### eventTruncate\n\nIt emits an array of removed items\n\n```ts\nimport { FixedSizeList, eventTruncate } from 'fixed-size-list'\n\nconst maxSize = 2\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize)\nconst unsubscribe = fixedSizeList.on(eventTruncate, (removedItems) =\u003e\n  console.log('items removed', removedItems.toString()),\n)\nfixedSizeList.add(5)\nfixedSizeList.add(4)\nfixedSizeList.add(3)\n// logs 'items removed [ 5 ]'\n\n// later on\nunsubscribe()\n```\n\n### eventReset\n\n```ts\nimport { FixedSizeList, eventReset } from 'fixed-size-list'\n\nconst maxSize = 2\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize)\nconst unsubscribe = fixedSizeList.on(eventReset, () =\u003e console.log('list reset'))\nfixedSizeList.reset()\n// logs 'list reset'\n\n// later on\nunsubscribe()\n```\n\n### eventCreated\n\nWe can add the third optional parameter of FixedSizeList's constructor and pass a custom event emitter\n\n```ts\nimport { FixedSizeList, eventCreated, IFixedSizeListEvents } from 'fixed-size-list'\nimport mitt from 'mitt'\n\nconst maxSize = 2\nconst list = []\nconst emitter = mitt()\nconst unsubscribe = emitter.on(eventCreated, () =\u003e console.log('list created'))\n\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(maxSize, list, emitter)\n// logs 'list created'\n\n// later on\nunsubscribe()\n```\n\n### All\n\nWe can subscribe to all events at once\n\n```ts\nimport { FixedSizeList, eventCreated, IFixedSizeListEvents } from 'fixed-size-list'\n\nconst fixedSizeList = new FixedSizeList\u003cnumber\u003e(10)\nconst unsubscribe = emitter.on('*', () =\u003e console.log('Any event'))\n\n// later on\nunsubscribe()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faigoncharov%2Ffixed-size-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faigoncharov%2Ffixed-size-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faigoncharov%2Ffixed-size-list/lists"}