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

@rcompat/string

v0.13.0

Published

Standard library strings

Readme

@rcompat/string

String utilities for JavaScript runtimes.

What is @rcompat/string?

A cross-runtime module providing common string operations: Base64 encoding, case conversion, dedentation, glob pattern matching, and UTF-8 byte length calculation. Works consistently across Node, Deno, and Bun.

Installation

npm install @rcompat/string
pnpm add @rcompat/string
yarn add @rcompat/string
bun add @rcompat/string

Usage

Base64 encoding/decoding

import Base64 from "@rcompat/string/Base64";

// encode to Base64
const encoded = Base64.encode("Hello, World!");
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="

// decode from Base64
const decoded = Base64.decode(encoded);
console.log(decoded); // "Hello, World!"

Convert to camelcase

import string from "@rcompat/string";

// from kebab-case
string.camelcased("hello-world"); // "helloWorld"
string.camelcased("my-component"); // "myComponent"

// from snake_case
string.camelcased("hello_world"); // "helloWorld"
string.camelcased("user_profile"); // "userProfile"

// mixed
string.camelcased("my-user_name"); // "myUserName"

Dedent multi-line strings

import string from "@rcompat/string";

// as a template tag
const sql = string.dedent`
  SELECT *
  FROM users
  WHERE active = true
`;
// "SELECT *\nFROM users\nWHERE active = true"

// with interpolation
const table = "users";
const query = string.dedent`
  SELECT *
  FROM ${table}
  WHERE id = 1
`;

Convert glob to RegExp

import string from "@rcompat/string";

// simple wildcards
const pattern = string.globify("*.js");
pattern.test("app.js"); // true
pattern.test("app.ts"); // false
pattern.test("dir/app.js"); // false

// double wildcard (recursive)
const recursive = string.globify("**/*.js");
recursive.test("app.js"); // true
recursive.test("src/app.js"); // true
recursive.test("src/lib/app.js"); // true

// single character wildcard
const single = string.globify("file?.txt");
single.test("file1.txt"); // true
single.test("fileA.txt"); // true
single.test("file10.txt"); // false

Capitalize first letter

import string from "@rcompat/string";

string.upperfirst("hello"); // "Hello"
string.upperfirst("world"); // "World"
string.upperfirst("javaScript"); // "JavaScript"

Get UTF-8 byte length

import utf8 from "@rcompat/string/utf8";

// ASCII (1 byte per character)
utf8.bytelength("hello"); // 5

// unicode (variable bytes)
utf8.bytelength("héllo"); // 6 (é = 2 bytes)
utf8.bytelength("日本語"); // 9 (3 bytes each)
utf8.bytelength("🎉"); // 4 (emoji = 4 bytes)

Get UTF-8 size

import utf8 from "@rcompat/string/utf8";

utf8.size("hello"); // 5
utf8.size("héllo"); // 6
utf8.size("日本語"); // 9
utf8.size("🎉"); // 4

API Reference

Base64

import Base64 from "@rcompat/string/Base64";

Base64.encode(decoded: string): string;
Base64.decode(encoded: string): string;

Object with methods for Base64 encoding and decoding.

| Method | Description | | -------- | ------------------------- | | encode | Encode a string to Base64 | | decode | Decode a Base64 string |

camelcased

import string from "@rcompat/string";

string.camelcased(string: string): string;

Converts kebab-case or snake_case strings to camelCase.

dedent

import string from "@rcompat/string";

string.dedent(string: string): string;
string.dedent(strings: TemplateStringsArray, ...values: unknown[]): string;

Removes common leading whitespace from multi-line strings. Can be used as a template tag or called directly with a string.

globify

import string from "@rcompat/string";

string.globify(pattern: string): RegExp;

Converts a glob pattern to a RegExp.

| Pattern | Matches | | ------- | ---------------------------- | | * | Any characters except / | | ** | Any characters including / | | ? | Any single character | | . | Literal dot (auto-escaped) |

upperfirst

import string from "@rcompat/string";

string.upperfirst(string: string): string;

Capitalizes the first character of a string.

utf8.bytelength

import utf8 from "@rcompat/string/utf8";

utf8.bytelength(string: string): number;

Returns the UTF-8 byte length of a string using native Buffer.byteLength.

utf8.size

import utf8 from "@rcompat/string/utf8";

utf8.size(string: string): number;

Pure JavaScript implementation to calculate UTF-8 byte size. Useful when you need a dependency-free solution or for environments without Buffer.

Examples

Generate component names

import string from "@rcompat/string";

function toComponentName(filename) {
// "my-button.svelte" -> "MyButton"
const name = filename.replace(/\.\w+$/, "");
return string.upperfirst(string.camelcased(name));
}

toComponentName("my-button.svelte"); // "MyButton"
toComponentName("user_profile.vue"); // "UserProfile"

Build SQL queries

import string from "@rcompat/string";

function buildQuery(table, conditions) {
  return string.dedent`
    SELECT *
    FROM ${table}
    WHERE ${conditions.join(" AND ")}
    ORDER BY created_at DESC
  `;
}

const query = buildQuery("users", ["active = true", "role = 'admin'"]);

File matching

import string from "@rcompat/string";

function matchFiles(files, pattern) {
  const regex = string.globify(pattern);
  return files.filter((file) => regex.test(file));
}

const files = ["app.js", "app.ts", "src/utils.js", "src/lib/helper.js"];

matchFiles(files, "*.js"); // ["app.js"]
matchFiles(files, "**/*.js"); // ["app.js", "src/utils.js", "src/lib/helper.js"]
matchFiles(files, "src/**/*.js"); // ["src/utils.js", "src/lib/helper.js"]

Validate content length

import utf8 from "@rcompat/string/utf8";

function validateContent(content, maxBytes = 1024) {
  const bytes = utf8.bytelength(content);
  if (bytes > maxBytes) {
    throw new Error(`Content exceeds ${maxBytes} bytes (got ${bytes})`);
  }
  return content;
}

validateContent("Hello"); // OK
validateContent("日本語".repeat(500)); // Error: exceeds limit

Encode data for URL

import Base64 from "@rcompat/string/Base64";

function encodeState(state) {
  const json = JSON.stringify(state);
  return Base64.encode(json);
}

function decodeState(encoded) {
  const json = Base64.decode(encoded);
  return JSON.parse(json);
}

const state = { page: 1, filter: "active" };
const encoded = encodeState(state); // "eyJwYWdlIjoxLCJmaWx0ZXIiOiJhY3RpdmUifQ=="
const decoded = decodeState(encoded); // { page: 1, filter: "active" }

Cross-Runtime Compatibility

| Runtime | Supported | | ------- | --------- | | Node.js | ✓ | | Deno | ✓ | | Bun | ✓ |

No configuration required — just import and use.

License

MIT

Contributing

See CONTRIBUTING.md in the repository root.