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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sovereignbase/utils

v2.3.0

Published

Shared TypeScript utilities for removing repeated code across Sovereignbase codebases.

Readme

npm version JSR CI codecov license

utils

Shared TypeScript utilities for removing repeated code across Sovereignbase codebases.

Compatibility

  • Runtimes: modern JavaScript runtimes; the repository includes runtime compatibility tests for Node, Bun, Deno, Cloudflare Workers, Edge Runtime, and browsers.
  • Module format: ESM and CommonJS.
  • Required globals / APIs: Web Crypto is required by deriveBytes(), EventTarget and CustomEvent are required by LanguageBroker, and structuredClone is required for successful safeStructuredClone() results.
  • Browser capability checks: browserHasSovereignbaseDependencies() resolves to false outside secure browser contexts and when required browser APIs are missing.
  • TypeScript: bundled types.

Goals

  • Remove repeated utility code from Sovereignbase repositories.
  • Keep shared helpers small, explicit, and side-effect free.
  • Validate behavior across the runtimes used in Sovereignbase projects.

Installation

npm install @sovereignbase/utils
# or
pnpm add @sovereignbase/utils
# or
yarn add @sovereignbase/utils
# or
bun add @sovereignbase/utils
# or
deno add jsr:@sovereignbase/utils
# or
vlt install jsr:@sovereignbase/utils

Usage

deriveBytes()

import { deriveBytes } from '@sovereignbase/utils'

const encoder = new TextEncoder()
const bytes = await deriveBytes(
  encoder.encode('account-1'),
  encoder.encode('profile-encryption'),
  32
)

Derives a deterministic number of domain-separated bytes with HKDF-SHA-256. Identical base, domain, and length inputs produce identical output. Use a distinct domain for each purpose.

prototype()

import { prototype } from '@sovereignbase/utils'

prototype(null) // 'null'
prototype({ ok: true }) // 'record'
prototype(new URL('https://sovereignbase.dev')) // 'url'

Returns a normalized lowercase runtime tag for common primitives, serializable built-ins, and selected platform objects.

isRecord()

import { isRecord } from '@sovereignbase/utils'

const value: unknown = { ok: true }

if (isRecord(value)) {
  value.ok // unknown
}

Checks that a value is a plain object record: non-null, not an array, and backed by an Object constructor prototype.

safeStructuredClone()

import { safeStructuredClone } from '@sovereignbase/utils'

const result = safeStructuredClone({ ok: true, nested: { count: 1 } })

if (result[0]) {
  const clone = result[1]
  clone // deep cloned value
}

Attempts a structured clone and returns a tuple instead of throwing on unsupported values.

getISO31661Alpha2CountryCodeSet()

import {
  type ISO31661Alpha2,
  getISO31661Alpha2CountryCodeSet,
} from '@sovereignbase/utils'

function epicFunction1(countryCode: ISO31661Alpha2) {
  const countryCodes = getISO31661Alpha2CountryCodeSet()
  const isCountryCode = countryCodes.has(countryCode)
  if (!isCountryCode) throw new Error('THAT IS NOT A COUNTRY CODE DUDE!')
  console.log('EPIC COUNTRY CODE MOMENT:', countryCode)
}

Returns a fresh Set containing all supported ISO 3166-1 alpha-2 country codes.

BCP47LanguageTag

import { type BCP47LanguageTag } from '@sovereignbase/utils'

function setDocumentLanguage(languageTag: BCP47LanguageTag) {
  document.documentElement.lang = languageTag
}

setDocumentLanguage('en-US')

Provides IANA-registered standalone language tags and Unicode CLDR locale tags, including region, script, and variant forms, without adding runtime code to the package.

LanguageBroker

import { LanguageBroker } from '@sovereignbase/utils'

const languages = new LanguageBroker(
  document.documentElement.lang,
  ['en-US', 'fi-FI'],
  (language) => {
    document.documentElement.lang = language
  }
)

languages.addEventListener('change', (event) => {
  console.log(`Language changed to ${event.detail}`)
})

languages.set('fi-FI')
languages.get() // 'fi-FI'
languages.has('fi-FI') // true
languages.has('sv-SE') // false
[...languages.list()] // ['en-US', 'fi-FI']

Keeps the current BCP 47 language tag within an inferred, non-empty set of supported languages and reports updates through an optional callback and typed change events. An unsupported initial string falls back to the first supported language. has() narrows strings to the supported-language union, which get(), set(), list(), callbacks, and event details also preserve.

UnicodeLocaleIdentifier

import { type UnicodeLocaleIdentifier } from '@sovereignbase/utils'

function formatDate(locale: UnicodeLocaleIdentifier, date: Date) {
  return new Intl.DateTimeFormat(locale).format(date)
}

formatDate('fi-FI', new Date())

Provides the locale identifiers backed by Unicode CLDR locale data, including default-content identifiers such as en-US.

OpenGraphLocale

import { type OpenGraphLocale } from '@sovereignbase/utils'

function localeMeta(locale: OpenGraphLocale) {
  return `<meta property="og:locale" content="${locale}">`
}

localeMeta('fi_FI')

Provides CLDR-backed Open Graph locales in the protocol's language_TERRITORY format.

browserHasSovereignbaseDependencies()

import { browserHasSovereignbaseDependencies } from '@sovereignbase/utils'

if (await browserHasSovereignbaseDependencies()) {
  console.log('browser runtime supports Sovereignbase dependencies')
}

Checks whether the current browser environment exposes the secure-context, storage, worker, notification, Web Crypto, and WebAuthn APIs required by Sovereignbase browser features.

afterIdleFor()

import { afterIdleFor } from '@sovereignbase/utils'

const saveAfterTypingStops = afterIdleFor(500, () => {
  console.log('save draft')
})

document.addEventListener('input', saveAfterTypingStops)

Creates a function that resets a timer on every call and runs the callback after the requested idle timeout.

daysAsMilliseconds()

import { daysAsMilliseconds } from '@sovereignbase/utils'

const retentionPeriod = daysAsMilliseconds(30)

Converts a number of 24-hour days to milliseconds.

waitFor()

import { waitFor } from '@sovereignbase/utils'

await waitFor(1_000)

Returns a promise that resolves after the requested number of milliseconds.

Tests

  • Unit and integration tests in Vitest with 100% statement, branch, function, and line coverage.
  • Public TypeScript API typechecks under the package's strict configuration.
  • Browser E2E tests in Chromium, Firefox, WebKit, Mobile Chromium, Mobile Firefox, and Mobile WebKit.
  • Runtime E2E tests in Node.js ESM and CommonJS, Bun ESM and CommonJS, Deno, Vercel Edge Runtime, and Cloudflare Workers (workerd).

API documentation

TypeDoc documentation is generated into docs/ by npm run build:docs and as part of the normal build.

Benchmarks

Run npm run bench to benchmark the synchronous utility paths on the current machine.

License

Apache-2.0