Skip to main content

Built and signed on GitHub Actions

Ooneex Collection is a robust suite of utilities for handling collections in your application.

This package works with Bun, Cloudflare Workers, Node.js, Deno, Browsers
JSR Score
100%
Published
2 months ago (1.0.8)

Ooneex Collection

Ooneex Collection is a robust suite of utilities for handling collections in your application. The collection includes various types such as Array Collections, Readonly Collections and more. These utilities make it simpler and more efficient to operate on collections, reducing complexity and improving readability. Ideal for any application that has to deal with intricate data structures, Ooneex Collections simplifies data management and manipulation.

How to Use

ArrayCollection

ArrayCollection is designed to manage arrays in an efficient and user-friendly manner. It provides numerous methods for manipulating arrays including add, remove, sort, filter, etc.

import { ArrayCollection } from '@ooneex/collection';

const collection = new ArrayCollection<number>();

// Add a few elements
collection.add(1).add(2).add(3);
// At this point, the collection should contain: [1, 2, 3]

// Remove an element
collection.delete(2);
// Now, collection should be: [1, 3]

// Clear the collection
collection.clear();
// Now, the collection should be empty.

Collection

Collection is a basic type that provides an easy way to manage a group of elements. Elements can be added, removed, searched, sorted, and more.

import { Collection } from '@ooneex/collection';

const myCollection = new Collection<string, number>();

// Add key-value pairs to the collection
myCollection.add("Iron Man", 1963);
myCollection.add("Spider-Man", 1962);
myCollection.add("Captain America", 1941);

// Print the count of items in the collection
console.log(myCollection.count()); // Outputs: 3

// Remove an item from the collection using the key
myCollection.delete("Captain America");

// Print the count of items in the collection after deletion
console.log(myCollection.count()); // Outputs: 2

// Clear all items in the collection
myCollection.clear();

// Print the count of items in the collection after clearing
console.log(myCollection.count()); // Outputs: 0

ReadonlyArrayCollection

ReadonlyArrayCollection is similar to ArrayCollection but it is read-only. This is especially useful when you want to ensure that the data in the collection does not get modified.

import { ReadonlyArrayCollection } from '@ooneex/collection';

const myCollection = new ReadonlyArrayCollection<string>(['Item1', 'Item2', 'Item3']);

// Check if 'Item1' is in the collection
if (myCollection.has('Item1')) {
    console.log('Item1 is in the collection!');
}

// Check if the collection is empty
if (!myCollection.isEmpty()) {
    console.log('The collection is not empty');
}

// Print the collection's size
console.log(myCollection.count());

// Use the Iterator to print each value in the collection.
for (const value of myCollection.values()) {
    console.log(value);
}

// Use the Symbol.iterator property to get an iterator and print each value in the collection.
for (const value of myCollection) {
    console.log(value);
}

ReadonlyCollection

ReadonlyCollection like the Collection type, but is read-only. It can be used to ensure that the original collection data is not modified.

import { ReadonlyCollection } from '@ooneex/collection'

// Instantiate a ReadonlyCollection with string keys and number values.
let collection = new ReadonlyCollection<string, number>([
  ["one", 1],
  ["two", 2],
  ["three", 3],
]);

// Use the get method to retrieve a value for a specific key.
console.log(collection.get("two")); // Outputs: 2

// Check if a certain key exists in the collection
console.log(collection.has("three")); // Outputs: true

// Check if the collection is empty
console.log(collection.isEmpty()); // Outputs: false

// Get the total count of keys in the collection
console.log(collection.count()); // Outputs: 3

// You can also iterate over keys or values using respective methods
for (let key of collection.keys()) {
  console.log(key);
}

for (let value of collection.values()) {
  console.log(value);
}

// Or you can iterate over key-value pairs (entries)
for (let [key, value] of collection.entries()) {
  console.log(`Key: ${key}, Value: ${value}`);
}

License

MIT

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @ooneex/collection

Import symbol

import * as mod from "@ooneex/collection";

Add Package

npx jsr add @ooneex/collection

Import symbol

import * as mod from "@ooneex/collection";

Add Package

yarn dlx jsr add @ooneex/collection

Import symbol

import * as mod from "@ooneex/collection";

Add Package

pnpm dlx jsr add @ooneex/collection

Import symbol

import * as mod from "@ooneex/collection";

Add Package

bunx jsr add @ooneex/collection

Import symbol

import * as mod from "@ooneex/collection";