@mattiasahlsen/ts-utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

@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

Dependents (0)

Package Sidebar

Install

npm i @mattiasahlsen/ts-utils

Weekly Downloads

0

Version

0.0.4

License

none

Unpacked Size

27.1 kB

Total Files

38

Last publish

Collaborators

  • mattiasahlsen