Skip to content

Methods

This section provides a comprehensive guide on how to use the various methods available in the ListManager class provided by Listner. Each method is designed to help you manage your lists efficiently, whether you're adding, updating, searching, or deleting items.

Creating a List

To start using Listner, you first need to create an instance of the ListManager class. You can specify the type of items your list will hold for better type safety.

ts
import { ListManager } from 'listner';

// Create a new ListManager instance
const fooList = new ListManager<YourType>();

Adding Items

insert() Adds a single item to the list.

ts
fooList.insert(item);

insertMultiple() Adds multiple items to the list at once.

ts
fooList.insertMultiple([item1, item2, item3]);

Retrieving Items

getAll() Return all items in the list.

ts
const items = fooList.getAll();

getLength() Returns the number of items in the list.

ts
const count = fooList.getLength();

getRandom() Returns a random item from the list.

ts
const randomItem = fooList.getRandom();

Searching and Updating

search() Searches for an item in the list that matches the query.

ts
const foundItem = fooList.search({ key: "value" });

searchAndUpdate() Searches for an item and updates it with the provided data.

ts
const updatedItem = fooList.searchAndUpdate({ key: "value" }, { key: "newValue" })

Deleting Items

delete() Deletes an item from the list that matches the query.

ts
const isDeleted = fooList.delete({ key: "value" });

deleteMultiple() Deletes multiple items that match the query and returns the count of deleted items.

ts
const deletedCount = fooList.deleteMultiple({ key: "value" });

deleteAll() Deletes all items from the list.

ts
fooList.deleteAll();

Advanced Operations

popRandom() Removes and returns a random item from list.

ts
const poppedItem = fooList.popRandom();

update() Updates and item that matches the query with the provided data.

ts
const isUpdated = fooList.update({ key: "value" }, { key: "newValue" });

updateMultiple() Updates multiple items that match the query with the provided data and returns the count of updated items.

ts
const updatedCount = fooList.updateMultiple({ key: "value" }, { key: "newValue" });