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

@beppobert/ts-utils

v0.0.2

Published

A collection of TypeScript utilities. I will never want to re-write these again.

Downloads

3

Readme

ts-utils

A collection of TypeScript utilities. I will never want to re-write these again.

Installation

npm install @beppobert/ts-utils

Typeguards

isDefined

import { isDefined } from "@beppobert/ts-utils";

const value: string | undefined = "Hello, World!";
if (isDefined(value)) {
  // value is string here
  console.log(value);
} else {
  // value is undefined here
  console.log("value is undefined");
}

[1, 2, null, undefined].filter(isNil); // number[]

isNull

import { isNull } from "@beppobert/ts-utils";

const value: string | null = "Hello, World!";

if (isNull(value)) {
  // value is null here
  console.log("value is null");
} else {
  // value is string here
  console.log(value);
}

isUndefined

import { isUndefined } from "@beppobert/ts-utils";

declare const value: string | undefined;

if (isUndefined(value)) {
  // value is undefined here
  console.log("value is undefined");
} else {
  // value is string here
  console.log(value);
}

isNil

import { isNil } from "@beppobert/ts-utils";

declare const value: string | null | undefined;
if (isNil(value)) {
  // value is null or undefined here
  console.log("value is null or undefined");
} else {
  // value is string here
  console.log(value);
}

[1, 2, null, undefined].filter(isNil); // (null| undefined)[]

isTruthy

import { isTruthy } from "@beppobert/ts-utils";

declare const value: string | null | undefined;

if (isTruthy(value)) {
  // value is string here
  console.log(value);
} else {
  // value is null or undefined here
  console.log("value is null or undefined");
}

isFalsy

import { isFalsy } from "@beppobert/ts-utils";

declare const value: string | null | undefined;

if (isFalsy(value)) {
  // value is null or undefined or "" here
  console.log("value is null or undefined");
} else {
  // value is string here
  console.log(value);
}

isKeyOf

import { isKeyOf } from "@beppobert/ts-utils";

type MyType = {
  a: string;
  b: number;
  c: boolean;
};

declare const obj: MyType;

if (isKeyOf(obj, "a")) {
  // obj.a is string here
  console.log(obj.a);
} else if (isKeyOf(obj, "d")) {
  const v: unknown = obj.d; // no error; obj.d is unknown
}

isTypeOf

import { isTypeOf } from "@beppobert/ts-utils";

declare const value: unknown;

if (isTypeOf(value, "string")) {
  // value is string here
  console.log(value);
} else if (isTypeOf(value, "number")) {
  // value is number here
  console.log(value);
} else if (isTypeOf(value, "boolean")) {
  // value is boolean here
  console.log(value);
} else if (isTypeOf(value, "object", "undefined")) {
  // value is object/unknown here
  console.log(value);
}

hasPath

import { hasPath } from "@beppobert/ts-utils";

declare const obj: { a: { b: { c: string } } };

if (hasPath(obj, "a", "b", "c")) {
  // obj.a.b.c is string here
  console.log(obj.a.b.c);
}
if (hasPath(obj, "a.b.d")) {
  // obj.a.b.d is unknown here
  console.log(obj.a.b.d);
}

Flow

assertNever

import { assertNever } from "@beppobert/ts-utils";

type MyType = "a" | "b" | "c";

declare const value: MyType;

switch (value) {
  case "a":
    console.log("a");
    break;
  case "b":
    console.log("b");
    break;
  case "c":
    console.log("c");
    break;
  default:
    assertNever(value);
}

switch (value) {
  case "a":
    console.log("a");
    break;
  case "b":
    console.log("b");
    break;
  default:
    assertNever(value); // error; value is "c"
}

Object-Helper

entries

import { entries } from "@beppobert/ts-utils";

const obj = { a: 1, b: 2, c: 3 };
const objectEntries = entries(obj); // (["a",1]|["b",2]|["c",3])[]
for (const [key, value] of entries(obj)) {
  console.log(key, value);
}

fromEntries

import { fromEntries } from "@beppobert/ts-utils";

const objectEntries = [
  ["a", 1],
  ["b", 2],
  ["c", 3],
] as const;

const obj = fromEntries(objectEntries); // { a: 1, b: 2, c: 3 }

keys

import { keys } from "@beppobert/ts-utils";

const obj = { a: 1, b: 2, c: 3 };
const objectKeys = keys(obj); // ("a"|"b"|"c")[]
for (const key of keys(obj)) {
  console.log(key);
}

omit

import { omit } from "@beppobert/ts-utils";

const obj = { a: 1, b: 2, c: 3 };
const newObj = omit(obj, "a", "b"); // { c: 3 }

pick

import { pick } from "@beppobert/ts-utils";

const obj = { a: 1, b: 2, c: 3 };
const newObj = pick(obj, "a", "b"); // { a: 1, b: 2 }

Common Types

Maybe

import { Maybe } from "@beppobert/ts-utils";

const value: Maybe<string> = "Hello, World!";
value = undefined;
value = null;

Unknown

import { Unknown } from "@beppobert/ts-utils";

const value: Unknown = "Hello, World!";

value = 42;
value = true;
value = {};
value = [];
value = undefined;
//.../

fn

import { Fn } from "@beppobert/ts-utils";

const fn: Fn = (...args) => 0;
const fn2: Fn<[string], null> = (arg) => null;
const fn3: Fn<[string, number], boolean> = (arg1, arg2) => true;