I turned strings into letter arrays and started rebuilding URLs with index ranges

I turned strings into letter arrays and started rebuilding URLs with index ranges

●1 ●19 ●60
calendar_today • schedule1 min read

I was just playing with a tiny idea:

What if every string was just an array of single characters… and I could pull pieces out by index ranges to form words, paths, or API links?

That experiment became string-range-tokens — a zero-dependency ESM package that does exactly that.

The core idea

import { toTokens, sub, build } from 'string-range-tokens';

const t = toTokens('https://api.example.com/v1/users');

sub(t, 0, 4);                 // "https"
sub(t, 23, 25, [26, 31]);     // "/v1/users"
sub(t, -5, -1);               // "users"   ← negative indexes work
build(t, [0, 4], [23, 25]);   // "https/v1"

toTokens turns any string into a proper array of characters (Unicode-safe).
sub and build let you reassemble any combination of inclusive ranges.

Why bother?

Sometimes you want to:

  • Assemble API paths or query fragments from a shared token pool
  • Avoid writing the full string literal in one place
  • Pick substrings by position without regex
  • Build tiny string “templates” from a fixed alphabet
  • Teach indexing, ranges, and a bit of recursion in a playful way

It’s not encryption (never hide real secrets with it). It’s just a clean, lightweight way to work with strings as letter arrays.

Extra powers

plan – find the ranges that rebuild a target

const pool = toTokens('the quick brown fox');
const ranges = plan(pool, 'brew');
build(pool, ...ranges); // "brew"

scatter / gather – seeded shuffle + reconstruct

const { pool, key } = scatter('/api/orders', 7);
gather(pool, key); // "/api/orders"

Useful for deterministic scrambling when you want both the shuffled tokens and a way to put them back together.

Install

npm i string-range-tokens

Or just copy the source — it’s a single file with no dependencies.

Works in Node 18+, Deno, Bun, and the browser.
Both JavaScript and TypeScript implementations are included.

CDN:

import { toTokens, sub, build, plan, scatter, gather } 
  from 'https://cdn.jsdelivr.net/npm/string-range-tokens@1.0.0/src/index.js';

If you end up using it for something fun (or weird), I’d love to hear about it.

4 Comments

2 votes
2
2 votes
2
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelski - Apr 23

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Merancang Backend Bisnis ISP: API Pelanggan, Paket Internet, Invoice, dan Tiket Support

Masbadar - Mar 13

Site Migration SEO Checklist: Redirect and Validate URLs

floxolab - Oct 1

Screaming Frog Review (2026): The Technical SEO Crawler I Audit With | FloxoLab

floxolab - Oct 21
chevron_left
1.8k Points • 80 Badges
26Posts
17Comments
13Connections
A full-stack web developer: Building smart solutions, designing meaningful experiences, and continuo... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!