@benderthecrime/collections

Helpful collections shamelessly copied from Python and ported to JS: orhttps://docs.python.org/2/library/collections.html#module-collections

Usage no npm install needed!

<script type="module">
  import benderthecrimeCollections from 'https://cdn.skypack.dev/@benderthecrime/collections';
</script>

README

Collections

Helpful collections shamelessly copied from Python and ported to JS.

Installation

npm i @benderthecrime/collections

Usage

DefaultObject

const collections = require('@benderthecrime/collections')
const {DefaultObject} = collections

const collector = DefaultObject(Number)
const values = ['a', 'b', 'c', 'd', 'a']

for (const value in values) {
    // No need to check to see if the value already exists in the collection.
    collector[value] += 1
}

// Works with scalar values too!
const defaulter = DefaultObject(null)

// And custom classes!
const defaulter = DefaultObject(Test, 'some argument', 'another argument')

// You can even make a DefaultObject inside a DefaultObject!
const collectoe = DefaultObject(DefaultObject, String)