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

@discordoo/collection

v1.1.3

Published

An utility data structure used within the Discordoo.

Downloads

55

Readme

Discordoo Collection

A utility data structure used within the Discordoo, Discord API library.

About

Collection was created for use inside Discordoo, for storing the cache in it and for convenient interaction with local lists.

We decided to put the Collection in a separate repository, so that third-party developers can use it without installing the entire Discordoo.

Quick Reference

Examples

How to use collection filter:

import { Collection } from '@discordoo/collection'

const collection = new Collection()

collection.set(1, 2).set(2, 3).set(3, 4).set(4, 5).set(5, 6)

// filter out elements that does not match filter fn
const filtered = collection.filter((value, key, collection) => key % 2 !== 0) // returns array

// do something with filtered elements
console.log(filtered.length) // 3
filtered.forEach(v => console.log(v[0])) // log all keys of filtered elements
filtered.forEach(v => console.log(v[1])) // log all values of filtered elements

/**
 * Filtered elements array looks like this:
 * [ [ FilteredKey, FilteredValue ], [ FilteredKey, FilteredValue ], [ FilteredKey, FilteredValue ] ]
 * which means that this
 * Collection [Map] {
 *   1 => 2,
 *   2 => 3,
 *   3 => 4,
 *   4 => 5,
 *   5 => 6
 * }
 * after this
 * collection.filter((value, key, collection) => key % 2 !== 0)
 * turns into this
 * [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
 * */

const fn = (value, key, collection) => key % 2 !== 0

// also you can choose in wich format return filtered values
collection.filter(fn, { returnType: 'map' }) // return data stored in javascript Map
collection.filter(fn, { returnType: 'collection' }) // return data stored in collection
collection.filter(fn, { returnType: 'array' }) // return data stored in array (default)

How to use filter option 'return' for typescript users:

import { Collection } from '@discordoo/collection'

const collection = new Collection(), fn = () => {}

console.log(collection.filter<Collection>   (fn, { returnType: 'collection' }).size)
console.log(collection.filter<Map<any, any>>(fn, { returnType: 'map' })       .size)
console.log(collection.filter<Array<any>>   (fn, { returnType: 'array' })     .length)
console.log(collection.filter(fn).length) // default to Array<[ K, V ]>

How to use collection random:

import { Collection } from '@discordoo/collection'

const collection = new Collection()

collection.random() // get 1 random value from collection
const values = collection.random(32) // get 32 random values from collection
console.log(values) // [ value, value, value ]

// get random keys from collection
collection.random(11, { returnType: 'keys' })
// get random key from collection
collection.random(undefined /* or 1 */, { returnType: 'keys' })

How to use collection random returnType option for typescript users:

import { Collection } from '@discordoo/collection'

type K = number
type V = string

const collection = new Collection<K, V>() // number - key, string - value

collection.random<K[]>(5, { returnType: 'keys' }) // tell typescript that we are expect array of collection keys
collection.random<[ K, V ]>(undefined, { returnType: 'blocks' }) // tell typescript that we are expect 1 block
collection.random<Array<[ K, V ]>>(5, { returnType: 'blocks' }) // tell typescript that we are expect array of blocks

Discord.js collection VS ddoo collection speed tests

Discord.js is the most popular library for developing bots, so we will compare with it. (if you're interested, ddoo is roughly comparable to eris in collection speed)

Higher is better.

Test: get random elements from collection

  1. 100 elements in collection, 5 random elements | result
  2. 800 elements in collection, 150 random elements | result
  3. 10000 elements in collection, 1000 random elements | result
  4. 10000 elements in collection, 8000 random elements | result

Test: filter 50% of elements from collection

  1. 100 elements in collection | result
  2. 800 elements in collection | result
  3. 10000 elements in collection | result

Test: map collection elements

  1. 100 elements in collection | result
  2. 800 elements in collection | result
  3. 10000 elements in collection | result