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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@carlwr/typescript-extra

v0.5.0

Published

Personal helpers and convenience functions for TypeScript

Downloads

18

Readme

typescript-extra

Personal helpers and convenience functions for TypeScript

Links:

Installation

npm install @carlwr/typescript-extra

# run checks and tests:
npm qa

API

allUnique

function allUnique<T>(xs: T[]): boolean

whether the elements of xs are unique, in the === sense

O(n) (if the hash tables behave, and they should for primitives) (constants likely not too great)

assertNever

function assertNever(_: never): never

drain

function drain<T>(xs: [T, ...T[]]): () => T

Stateful iterator yielding the elements of xs.

When the last element is reached, calls will continue to return that element indefinitely.

example:

const next = drain([1, 2, 3])
next()  // => 1
next()  // => 2
next()  // => 3
next()  // => 3
next()  // => 3 (returns 3 forever)

extract

function extract<T>(root: unknown, pred: (k: string, v: unknown) => undefined | T): T[]

walk root recursively while collecting all Ts for which pred returnes a defined result

example:

const root = {
  A: 1,
  b: 0,
  c: {
    99: [{A:2}, {e:"zero"}, null],
    A: 3
  }
}
function pred(k: string, v: any): number|undefined {
  if (k === 'A' && typeof v === 'number') { return v }
  return undefined
}
const result = extract<number>(root, pred)
// result === [1, 2, 3]

flatmapNonEmpty

function flatmapNonEmpty<T, U>(xs: readonly [T, T], fn: (x: T) => [U, ...U[]]): [U, ...U[]]

Flatmap over a non-empty array with a function returning a non-empty arrays. Return the flattened result as a non-empty array.

getMatch

function getMatch(re: RegExp, str: string): string

return the match of the regex, or throw if no match

hasAtleastTwo

function hasAtleastTwo<T>(xs: readonly T[]): xs is [T, T, ...T[]]

hasKey

function hasKey<T, K>(value: T, key: K): value is T & { [P in PropertyKey]: unknown }

whether the value is an object that has the key

in the true branch, the type of the passed argument is narrowed to include the knowledge that the key is present, without destroying any other knowledge about the type prior to the call

isDefined

function isDefined<T>(x: undefined | null | T): x is NonNullable<T>

isEmpty

function isEmpty<T>(xs: readonly T[]): xs is []

isNonEmpty

function isNonEmpty<T>(xs: readonly T[]): xs is [T, ...T[]]

isSingle

function isSingle<T>(xs: readonly T[]): xs is [T]

mapAsync

function mapAsync<T, U>(xs: readonly T[], f: (x: T) => Promise<U>): Promise<U[]>

mapFilterAsync

function mapFilterAsync<T, U>(xs: readonly T[], f: (x: T) => Promise<undefined | null | U>): Promise<U[]>

mapNonEmpty

function mapNonEmpty<T, U>(xs: readonly [T, T], fn: (x: T) => U): [U, ...U[]]

Map over a non-empty array while preserving the type as non-empty

memoized

function memoized<T>(f: () => Promise<T>): () => Promise<T>

Cached, lazy, single-flight evaluation of a promise.

A rejected promise is cached as well, i.e. no re-attempts (failures are assumed to be permanent).

partitionAsync

function partitionAsync<A, B>(items: readonly A[], pred: (a: A) => Promise<B>): Promise<readonly [A[], A[]]>

rm_rf

function rm_rf(path: string): Promise<void>

remove a file or directory recursively; ignore errors

safeIndex

function safeIndex<T>(xs: readonly [T, T], i: number): T

return the ith element of xs, or throw if i is out of bounds

trim

function trim(str: string): string

the .trim() method as a function

trim(str) <=> str.trim()

withoutFirstSubstring

function withoutFirstSubstring(first: string, str: string): string

remove a substring from the beginning of a string; throw if str does not start with first

example:

withoutFirstSubstring('e', 'ego') // => 'go'