Skip to main content
Home

Built and signed on GitHub Actions

A module containing general purpose utility functions

This package works with Node.js, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers, Deno
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
It is unknown whether this package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (0.5.0)

A module containing general purpose utility functions and types

Examples

Abortable Promise

import { AbortablePromise } from "@sillvva/utils";

const promise = new AbortablePromise((resolve, reject) => {
	setTimeout(() => {
		resolve("Hello World");
	}, 1000);
}, { signal: AbortSignal.timeout(500) });

promise.then((result) => {
	console.log(result); // Hello World
}).catch((error) => {
	console.log(error); // Error: The operation was aborted.
});

Debounce

import { debounce } from "@sillvva/utils";

const debouncedSearch = debounce((query: string) => {
  console.log("Searching for:", query);
  return `Results for ${query}`;
}, 300);

// Only the last call executes after 300ms
debouncedSearch.call("hello").then(result => {
  console.log(result); // "Results for hello"
});

isDefined

import { isDefined } from "@sillvva/utils";

const values = [1, null, 2, undefined, 3]; // (number | null | undefined)[]
const defined = values.filter(isDefined); // number[]
console.log(defined); // [1, 2, 3]

Slugify

import { slugify } from "@sillvva/utils";

const slug = slugify("Hello World");
console.log(slug); // hello-world

Sorter

import { sorter } from "@sillvva/utils";

const fruits = ["apple", "Banana", "Orange", "banana"];
const sorted = fruits.toSorted(sorter);
console.log(sorted); // ["apple", "banana", "Banana", "Orange"]

const arrObjs = [{ id: 3 }, { id: 1 }, { id: 2 }];
const sortedObjs = arrObjs.toSorted((a, b) => sorter(a.id, b.id));
console.log(sortedObjs); // [{ id: 1 }, { id: 2 }, { id: 3 }]

Substring Count

import { substrCount } from "@sillvva/utils";

const text = "hello world hello";
const count = substrCount(text, "hello");
console.log(count); // 2

// With overlapping matches
const overlapping = substrCount("aaaa", "aa", true);
console.log(overlapping); // 3

Wait

import { wait } from "@sillvva/utils";

async function example() {
  console.log("Starting...");
  await wait(1000);
  console.log("1 second later!");
}

Utility Types

import type { MapKeys, Prettify, Falsy, DictOrArray } from "@sillvva/utils";

// MapKeys - Extract key type from Map
type MyMap = Map<string, number>;
type Keys = MapKeys<MyMap>; // string

// Prettify - Flatten intersection types
type Ugly = { a: string } & { b: number };
type Pretty = Prettify<Ugly>; // { a: string; b: number }

// Falsy - Type guard for falsy values
function isFalsy(value: unknown): value is Falsy {
  return !value;
}

// DictOrArray - Handle objects or arrays
function processData(data: DictOrArray) {
  if (Array.isArray(data)) {
    console.log("Processing array with", data.length, "items");
  } else {
    console.log("Processing object with keys:", Object.keys(data));
  }
}
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";
or

Import directly with a jsr specifier

import * as utils from "jsr:@sillvva/utils";

Add Package

pnpm i jsr:@sillvva/utils
or (using pnpm 10.8 or older)
pnpm dlx jsr add @sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";

Add Package

yarn add jsr:@sillvva/utils
or (using Yarn 4.8 or older)
yarn dlx jsr add @sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";

Add Package

vlt install jsr:@sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";

Add Package

npx jsr add @sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";

Add Package

bunx jsr add @sillvva/utils

Import symbol

import * as utils from "@sillvva/utils";