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

@mattiasahlsen/ts-utils

v0.0.4

Published

This is a library written complete in Typescript the right way, with proper type support and inferred types.

Downloads

5

Readme

@mattiasahlsen/ts-utils

This is a library written complete in Typescript the right way, with proper type support and inferred types.

Npm link: https://www.npmjs.com/package/@mattiasahlsen/ts-utils

Install

npm install @mattiasahlsen/ts-utils --save

Importing

Recommended way to import, only import the used function

This will reduce the imported bundle size.

import { notUndefined } from "@mattiasahlsen/ts-utils/not-undefined";
import { duplicateFilter } from "@mattiasahlsen/ts-utils/duplicate-filter";

Import many (or all) functions in the same import

This will include all the functions in the package in the imported bundle, even if you only use a few.

import { notUndefined, duplicateFilter } from "@mattiasahlsen/ts-utils";

Functions

notUndefined()

function notUndefined<T>(x: T | undefined): x is T;

Usage

import { notUndefined } from "@mattiasahlsen/ts-utils/not-undefined";

[1, undefined, 2].filter(notUndefined) // [1, 2]

duplicateFilter()

function duplicateFilter<T>(
  element: T,
  index: number,
  elements: T[]
): boolean;

Usage

import { notUndefined } from "@mattiasahlsen/ts-utils/duplicate-filter";

[1, 1, 2].filter(duplicateFilter) // [1, 2]

range()

function range(toOrFrom: number, toParameter?: number): number[];

Usage

import { range } from "@mattiasahlsen/ts-utils/range";

range(5) // [0, 1, 2, 3, 4]
range(2, 5) // [2, 3, 4]

copySubsetOfObject()

function copySubsetOfObject<T extends object, Key extends keyof T>(
  originalObject: T,
  keys: readonly Key[]
): Pick<T, Key>;

Usage

import { copySubsetOfObject } from "@mattiasahlsen/ts-utils/copy-subset-of-object"

copySubsetOfObject({ key1: "val1", key2: "val2" }, ["key1"]) // { key1: "val1" }

getPromiseWithResolve()

function getPromiseWithResolve(options?: {
  timeout?: number // defaults to no timeout
  timeoutErrorMessage?: string
})

Usage

import { getPromiseWithResolve } from "@mattiasahlsen/ts-utils/get-promise-with-resolve"

// The resolve and reject are bound to the promise
// The promise will automatically reject with
// a timeout error in 10 seconds
const { promise, resolve, reject } = getPromiseWithResolve({ timeout: 10000 })

// Any function with access to the shared cache will
// be able to wait for the promise that will resolve
// to the service
someSharedCache.someServiceNotYetCreated = promise

// This function has access to resolve the promise,
// and can also pass it to other functions
someServiceManagerThatWillEventuallyFetchTheService.queueFetchingService(
  "someServiceNotYetCreated",
  resolve
)

Classes

RandomNumberGenerator

A seedable random number generator. This generator is not cryptographically secure. Should rather be used for testing or non-security-related functionality.

interface IRandomNumberGenerator {
  randomNumber(max?: number): number
  randomInt(max: number): number
  resetSeed(seed: number): void
}

class RandomNumberGenerator
  implements IRandomNumberGenerator;
import { RandomNumberGenerator } from "@mattiasahlsen/ts-utils/random-numbers";

const seed = 1
const randomNumberGenerator = new RandomNumberGenerator(seed)

const maxNumber = 5
randomNumberGenerator.randomNumber(maxNumber) // random number between 0 and 5 (not including 0 or 5)

randomNumberGenerator.resetSeed(seed)
randomNumberGenerator.randomNumber(maxNumber) // the same random number

randomNumberGenerator.randomInt(5) // a random integer between (and including) 0 and 4

DependencyManager

A class to asynchronously manage dependencies between services.

export interface IDependencyManager {
  registerService(serviceKey: string, service: unknown): void
  getDependency(
    serviceKey: string,
    options: { from: string }
  ): Promise<unknown>
  findCircularDependencies(serviceKey: string): void
}

class DependencyManager implements IDependencyManager

Usage

// In the aggregator service file

import { DependencyManager } from "@mattiasahlsen/ts-utils/dependency-manager";

// Wait for a dependency that is loaded later
const dependencyManager = new DependencyManager()
const testServicePromise = dependencyManager.getDependency('testService', {
  from: 'aggregatorService', // passed to be able to track circular dependencies
})
// promise not yet resolved
testServicePromise.then(() => {
  console.log("testService resolved)
})

// no console.log yet

const mockTestService = mock()
dependencyManager.registerService('$testService', mockTestService)

// now the service is registered, the promise will be resolved and
// we we would see "testservice resolved" in the console