@solvro/utils

1.1.2 • Public • Published

Solvro utils

A collection of small utility functions that can be used both on the backend and frontend.

Contents

arrays

map

Contains the ExtendedMap class, an extension of the vanilla Map with the following extra methods:

  • getOrInsert, getOrInsertWith, getOrInsertWithAsync - getter methods that insert a default value if a key does not exist. probably inspired by rust's map Entry structs. useful for building Map-based caches.

An existing vanilla Map instance can be converted to an ExtendedMap with the extendMap() function. (beware: contains prototype shenanigans) ... or you can extend the entire vanilla Map prototype with extendGlobally()... but please don't do that in library code.

Extending the vanilla Map prototype

Use the following snippet to access ExtendedMap functions on any Map instance. Make sure to either run this at the start of your program, or before using any ExtendedMap functions on a Map object.

import { ExtendedMap, extendGlobally } from "@solvro/utils/map";

declare global {
  interface Map<K, V> {
    getOrInsert: ExtendedMap<K, V>["getOrInsert"];
    getOrInsertWith: ExtendedMap<K, V>["getOrInsertWith"];
    getOrInsertWithAsync: ExtendedMap<K, V>["getOrInsertWithAsync"];
  }
}

extendGlobally();

promises

  • wait - returns a promise that resolves after some time (the classic new promise -> set timeout that everyone had to write at least once)

semaphore

Contains a simple async Semaphore class. Useful for limiting concurrent task counts.

License

This library is licensed under MPL-2.0.

Package Sidebar

Install

npm i @solvro/utils

Weekly Downloads

145

Version

1.1.2

License

MPL-2.0

Unpacked Size

44.5 kB

Total Files

19

Last publish

Collaborators

  • knsolvro
  • mini_bomba
  • rejek