import { isBigIntStringValid, isEmailValid, isIntegerValid, isStringValid } from 'jg-utilities';isStringValid(name, 1, 100); // a non-blank string of 1 to 100 charactersisIntegerValid(page, 1); // an integer of at least 1isEmailValid(email); // a valid address without a forbidden extensionisBigIntStringValid(id, 1n); // a positive integer string that fits PostgreSQL's BIGINT
Format values for people:
ts
import { escapeHtml, formatRelativeTime, pluralize, prettifyDate, prettifyFileSize, prettifyNumber, toSlug,} from 'jg-utilities';prettifyNumber(1234567.891); // '1,234,567.89'prettifyNumber(-5, { prefix: '$' }); // '-$5'prettifyFileSize(1536); // '1.50 kB'prettifyDate(createdAt, 'date-medium'); // 'October 17, 2026'formatRelativeTime(observedAt); // '5 minutes ago', 'in 2 hours', 'yesterday'toSlug('Héllo, World! 2026'); // 'hello-world-2026'`${count} ${pluralize(count, 'file')}`; // '1 file', '3 files'escapeHtml(name); // safe to put into HTML, such as an email's body
Send someone back where they were after signing in, only within the site and never back to the sign-in page:
Keep secrets out of logs, and read the YouTube links people paste:
ts
import { parseYouTubeVideoId, redactSecrets, redactUrlQueryParams, toYouTubeEmbedUrl,} from 'jg-utilities';redactUrlQueryParams(request.url, ['token']); // the path alone when the query holds a tokenredactSecrets(logLine).text; // GitHub tokens, sk- keys, and private keys become [redacted]const videoId = parseYouTubeVideoId(pastedUrl); // the id, or null for anything but one videoconst embedUrl = videoId === null ? undefined : toYouTubeEmbedUrl(videoId);
Generate secure random values, IDs, and timestamps:
ts
import { createTimestamp, generateNameBasedUuid, generateRandomString, generateUuid,} from 'jg-utilities';generateRandomString(16); // drawn with crypto.getRandomValues, fit for secretsgenerateUuid(7); // a time-ordered UUIDgenerateNameBasedUuid(namespace, `${operationId}:step-2`); // the same version 5 UUID every timecreateTimestamp(); // '2026-10-17T08:30:00.000Z'
Name what an upload really is from its first bytes, never from its name or the type the client sent:
ts
import { detectMediaType } from 'jg-utilities';const type = detectMediaType(bytes, ['image/png', 'image/jpeg', 'application/pdf']);// 'image/png', 'image/jpeg', 'application/pdf', or null for anything else
Work with UTC calendar dates, such as the days a report covers, and fit text into a byte budget, such as a push notification’s payload limit:
ts
import { generateUtcDateSequence, getTrailingUtcDateRange, toUtcDate, truncateToUtf8Bytes,} from 'jg-utilities';toUtcDate(createdAt); // '2026-10-17', the UTC day of an instantconst range = getTrailingUtcDateRange(30); // the last 30 UTC days, today includedgenerateUtcDateSequence(range); // one 'YYYY-MM-DD' per day, oldest firsttruncateToUtf8Bytes(body, 2048); // whole code points only, at most 2048 bytes in UTF-8
Run side effects away from the code that triggers them with an in-process event bus:
ts
import { createEventBus } from 'jg-utilities';type IAppEvents = { userSignedUp: { userId: string } };const events = createEventBus<IAppEvents>({ onError: (error) => logger.error(error) });events.subscribe('userSignedUp', ({ userId }) => sendWelcomeEmail(userId));events.emit('userSignedUp', { userId }); // returns at once; each handler runs later, on its own
Serialize, measure, and compare text and JSON exactly, and check paths before writing them:
ts
import { compareCodePoints, countUtf8Bytes, deepFreeze, findCaseFoldCollision, isPortablePathValid, measureJsonBytes, stringifyJsonDeterministically,} from 'jg-utilities';stringifyJsonDeterministically(document, { strict: true }); // RFC 8785 key order, plain JSON onlymeasureJsonBytes(message, 1_048_576) > 1_048_576; // too big, found without serializing itcountUtf8Bytes(title, 255); // stops counting once it passes the limitnames.sort(compareCodePoints); // UTF-8 byte order, where sort() compares UTF-16 code unitsisPortablePathValid(path, { maxComponentBytes: 64, maxPathBytes: 160 }); // safe on every OSfindCaseFoldCollision(paths); // such as ['README.md', 'readme.md'], or nullconst config = deepFreeze(loadConfig()); // nothing in it can change, at any depth
Keep a request’s result from reaching another account, run work in parallel but record it in order, and keep bytes in a bounded cache:
ts
import { createByteCache, runInOrder, runWhileCurrent, upsertMarkedRegion } from 'jg-utilities';const requestedFor = session.getState().accountId;const profile = await runWhileCurrent((signal) => fetchProfile(signal), { isCurrent: () => session.getState().accountId === requestedFor, subscriptions: [session.subscribe], // a Zustand store's subscribe, say createStaleError: (cause) => new Error('The account changed.', { cause }),});const results = await runInOrder(cases, { concurrency: 4, execute: (testCase) => evaluate(testCase), commit: (result) => appendToCheckpoint(result), // one at a time, in the cases' order});const pages = createByteCache({ maxBytes: 8 * 1024 * 1024 }); // least recently used out firstconst readme = upsertMarkedRegion(text, block, { start: '<!-- tool:start -->', end: '<!-- tool:end -->',});
Paginate with opaque cursors, and wait or retry with timing in seconds:
Give each list a cursor codec that validates its cursors and accepts them only for the filters they were made for, and keep a rate-limited API’s requests within its limits:
ts
import { createCursorCodec, createRequestCoordinator } from 'jg-utilities';const paymentCursors = createCursorCodec({ schema: PaymentCursorSchema }); // a Zod schema, sayconst after = paymentCursors.decode(request.query.cursor, { status }); // INVALID_CURSOR otherwiseconst github = createRequestCoordinator({ maxConcurrent: 4, maxQueued: 64 });const issues = await github.run(() => fetchIssues(), { caller: installationId, maxQueuedForCaller: 8,});github.deferUntil(retryAt); // queued requests wait out a Retry-After, without holding a slot
Run a service’s background passes through a task runner, so a shutdown waits for them, and bound a job with a deadline:
ts
import { createDeadline, createTaskRunner } from 'jg-utilities';const runner = createTaskRunner({ onError: (error, { key }) => logger.error(key, error) });const interval = setInterval(() => { void runner.trigger((signal) => syncInvoices(signal), { key: 'invoices' }); // skipped while one runs}, 60_000);const report = await runner.run(() => buildReport(), { key: 'report', ifBusy: 'join' }); // one at a time// at shutdown: no new tasks, the running ones aborted, and resolved once they all settleclearInterval(interval);await runner.close({ abort: true });const deadline = createDeadline({ timeoutSeconds: 55, signal: request.signal });try { for (const file of files) { deadline.throwIfStopped(); // the request's reason, or DEADLINE_EXCEEDED once 55 seconds passed await uploadFile(file, deadline.signal); // aborts with a TimeoutError at the deadline }} finally { deadline.dispose();}
run claims its key before the task starts, and another run with the key, even one from inside the task, rejects with TASK_RUNNER_BUSY, or, with ifBusy: 'join', resolves to the running task’s result. A task must not join its own key or await its own runner’s close(), because it would wait for itself. close makes later runs reject with TASK_RUNNER_CLOSED and later triggers resolve to false, and never rejects. A deadline’s child({ timeoutSeconds }) bounds one step within it, and stopReason() says whether the caller’s signal, 'cancelled', or the deadline, 'deadline', ended the work.
Save a file from a browser, keeping the object URL long enough for the download to start:
ts
import { downloadBlob, downloadJson } from 'jg-utilities';downloadBlob(new Blob([csv], { type: 'text/csv' }), 'payments.csv'); // false without a documentdownloadJson(settings, 'settings.json'); // pretty JSON ending in a newline
Both click a hidden link that’s added to the page and removed, and revoke the file’s object URL 40 seconds later, revokeDelaySeconds for downloadBlob, because revoking it at once can cancel the download. downloadJson serializes first, so a value JSON can’t hold, such as a BigInt or a cycle, throws UNABLE_TO_SERIALIZE_JSON wherever it runs.
Generated from TSDoc
API reference
Every export of [email protected], 169 in all, with its signature, its errors, and an example.
Settles with a promise unless the signal aborts first, such as to stop waiting for a step that can't be cancelled when the user leaves the page. It rejects with the signal's reason, at once when the signal was already aborted, and removes its listener either way. The promise itself keeps running, because a promise can't be cancelled.
Applies substitutions to a string based on a provided object. The string can contain placeholders in the format of {{key}}, which will be replaced by the corresponding value from the substitutions object. If a placeholder does not have a corresponding key in the substitutions object, it will remain unchanged in the output string.
The input string containing placeholders in the format of {{key}}.
substitutions?Record<string, unknown>
No description.
Defaults to {}.
options?ISubstitutionOptions
No description.
Defaults to ....
Returns
string
A string with the placeholders replaced by their corresponding values from the substitutions object.
Example
import { applySubstitutions } from 'jg-utilities';applySubstitutions('Hi {{name}}, you have {{count}} messages.', { name: 'Ada', count: 3 }); // => 'Hi Ada, you have 3 messages.'
Compares two strings by Unicode code point, the order of UTF-8 bytes and of most other languages' strings, where < and the default sort compare UTF-16 code units, which put characters above U+FFFF, such as emoji, before U+E000 to U+FFFF. It allocates nothing, so it suits sorting large lists. A lone surrogate compares as its own code point.
Signature
compareCodePoints(left: string, right: string): number
Parameters
leftstring
The first string.
rightstring
The second string.
Returns
number
A negative number when left comes first, a positive one when right does, and 0 when they're equal.
Counts the bytes a string takes in UTF-8, as TextEncoder writes it, without encoding it, and stops once the count passes maxBytes, so measuring a long string against a small limit costs little. A lone surrogate counts as 3 bytes, the size of the U+FFFD that replaces it.
Signature
countUtf8Bytes(text: string, maxBytes?: number): number
Parameters
textstring
The string.
maxBytes?number
No description.
Returns
number
The exact count when it's at most maxBytes, otherwise a number above it.
Throws
INVALID_BYTE_LIMITThe byte limit must be a non-negative safe integer.
Creates a least-recently-used cache of bytes, such as file pages or API responses, that never holds more than maxBytes and, when given, maxEntries. It copies bytes into new memory on the way in and out, a Buffer included, so no caller can change a cached value. retain pins an entry, such as while a reader streams it, so it's never evicted or replaced until released, and pinned bytes count toward the limit.
Creates a codec for a list's pagination cursors: encode validates a continuation, such as the last row's sort keys and the list's filters, with the schema and encodes it as an opaque base64url cursor; decode reverses that for a cursor a client sent, and accepts it only for the list it came from. Without matchesScope, every key of the scope must equal the payload's.
decode returns undefined for a missing cursor, meaning the first page, and throws INVALID_CURSOR for any cursor it refuses, with data.reason saying whether its encoding, its shape, or its scope was wrong, and the schema's own error, if any, as cause: for a client, a cursor that fails its schema is the same mistake as a malformed one.
Creates a deadline for a bounded job, such as a request handler's or a cron pass's, from a timeout, a time, or both, the earlier winning, and a caller's signal. Its signal aborts with a TimeoutError DOMException when the deadline passes, which jg-fetch reads as a timeout, or with the caller's reason, and its timer waits in steps of at most 2^31 - 1 milliseconds, so a deadline days away works. throwIfStopped reads the clock, so it notices a deadline that passed while the process was suspended, before the timer fires. Call dispose once the work ends early, to stop the timer.
The timeout in seconds, the time, and the caller's signal.
Returns
IDeadline
The deadline.
Throws
INVALID_DEADLINEThe deadline needs timeoutSeconds, finite seconds of at least 0, or deadlineAt, a valid Date, epoch milliseconds, or Infinity, and a signal that's an AbortSignal when it's given.
Example
import { createDeadline } from 'jg-utilities';const files = ['a.txt', 'b.txt'];const uploaded: string[] = [];const deadline = createDeadline({ timeoutSeconds: 55 });try { for (const file of files) { deadline.throwIfStopped(); // throws DEADLINE_EXCEEDED once 55 seconds passed uploaded.push(file); // such as an upload given deadline.signal }} finally { deadline.dispose();}uploaded; // => ['a.txt', 'b.txt']deadline.stopReason(); // => null
Creates an in-process event bus that decouples the code that emits an event from the handlers that react to it, such as sending a welcome email after a sign-up. emit returns at once, and each event is delivered later, in the order it was emitted. Each handler runs on its own, so one that throws or rejects doesn't stop the others: its error goes to onError. A handler unsubscribed before it's called isn't called, and subscribing the same handler twice to one event registers it once. Declare the event map with type, because an interface has no index signature.
Creates a coordinator that runs tasks, such as requests to a rate-limited API, at most maxConcurrent at a time, and queues the rest, first in, first out, up to maxQueued. A caller can be capped too, with caller and maxQueuedForCaller, so one caller can't fill the queue.
deferUntil starts a cooldown, such as from a Retry-After header: queued tasks wait until it ends, without holding a slot, and tasks run with rejectDuringCooldown reject at once instead. A task whose signal aborts while it waits leaves the queue. A task's own result or error passes through unchanged, and its slot is released either way. No timer is left once nothing waits.
run rejects with REQUEST_QUEUE_FULL when the queue, or the caller's share of it, is full, with REQUEST_COOLDOWN_ACTIVE when the task may not wait out a cooldown, with REQUEST_COORDINATOR_CLOSED after close, and with INVALID_QUEUE_LIMIT for a maxQueuedForCaller that isn't an integer of at least 0. deferUntil throws INVALID_DATE for a deadline that isn't a valid date.
Creates a runner for background tasks, such as a service's timer ticks or queue passes, that a shutdown can drain: close stops new tasks at once, optionally aborts the running ones through the signal each task receives, and resolves once every task has settled.
run starts a task now and passes its result or error through. It claims its key before the task starts, so only one task with that key runs at a time: another run with the key, even from inside the task, rejects with TASK_RUNNER_BUSY, or, with ifBusy: 'join', resolves to the running task's result. A task must not join its own key or await its own runner's close, because it would wait for itself. trigger is for timers: it skips a tick while the key's task runs or the runner is closing, resolving to false, and sends a task's error to onError instead of rejecting.
Creates an ISO 8601 timestamp, in UTC, from a date, a millisecond epoch, or a date string, or from the current time when no value is given.
Signature
createTimestamp(value?: string | number | Date): string
Parameters
value?string | number | Date
The moment to format. Defaults to now.
Returns
string
The timestamp, such as 2026-10-17T08:30:00.000Z.
Throws
ErrorInvalid time value
Example
import { createTimestamp } from 'jg-utilities';createTimestamp(); // the current time, such as '2026-10-17T08:30:00.000Z'createTimestamp(0); // => '1970-01-01T00:00:00.000Z'
Decodes a base64url value, without padding, into the UTF-8 string it encodes. Only the canonical encoding of a string is accepted, so every string has exactly one valid encoding.
Signature
decodeBase64Url(value: string): string
Parameters
valuestring
The base64url value to decode.
Returns
string
The decoded string.
Throws
INVALID_BASE64URLif the value isn't a string of base64url characters with a valid length.
INVALID_BASE64URLif the decoded bytes aren't valid UTF-8.
INVALID_BASE64URLif the value isn't the canonical encoding of its result.
Example
import { decodeBase64Url } from 'jg-utilities';decodeBase64Url('eyJwYWdlIjoyfQ'); // => '{"page":2}'
Decodes a cursor made by encodeCursor. The cursor comes from a client, so it's validated before decoding: a non-empty base64url string no longer than maxLength. The caller still validates the decoded value's shape, such as with a schema.
Freezes a value and every object it holds, at any depth, so nothing in it can change, such as a configuration shared across a program. Unlike a recursive Object.freeze, it walks every own property, symbol keys included, freezes the children of an object that was already frozen, and visits each object once, so a cycle is safe. It reads only data properties, so no getter runs, and leaves typed arrays and DataViews, which can't be frozen, as they are.
Names the media type of content from its leading bytes, never from a name or a type someone claims, among the types a caller accepts: PNG, JPEG, GIF, WebP and ICO images, PDF, and WebM, Ogg, MP4, MP3 and WAV audio. A WebM or MP4 file holding video reads as its audio type, since their first bytes name only the container. The first accepted type whose signature matches wins.
Saves a Blob as a file in a browser, through a temporary hidden link that's added to the page, clicked, and removed. The object URL is revoked only after revokeDelaySeconds, 40 by default, because revoking it right after the click can cancel the download in some browsers. Without a document, such as in Node.js, it does nothing.
Saves a value as a pretty-printed JSON file in a browser, ending in a newline, with downloadBlob. The value is serialized first, so one that can't be throws wherever it runs.
Escapes text for HTML, so it shows as written instead of becoming markup, such as a name in an email template or a Telegram message. It's safe in element content and quoted attribute values, not in a URL, a script, or a style.
Signature
escapeHtml(text: string): string
Parameters
textstring
The text.
Returns
string
The escaped text.
Example
import { escapeHtml } from 'jg-utilities';escapeHtml('<b>"Tom" & Jerry\'s</b>'); // => '<b>"Tom" & Jerry's</b>'
Escapes text for one line of Markdown, such as a heading or a table cell, so the text shows as written: it puts a backslash before each backslash, backtick, *, _, {, }, [, ], <, >, and |, which stops code, emphasis, links, inline HTML, and table pipes, and turns each line break and control character into a space, so the text stays on its line.
Finds two paths that name the same file on a filesystem that ignores letter case and Unicode normalization, as Windows and macOS do by default, such as README.md and readme.md. Each path is folded with NFC, then uppercase, then lowercase, which also folds letters that only uppercasing maps, such as the long ſ, which folds to s. An exact duplicate isn't a collision.
Finds the region between two marker lines in a document, such as the part of a README a tool keeps up to date between <!-- tool:start --> and <!-- tool:end -->. Each marker must fill its whole line, apart from a byte order mark before the first line, and the document must hold exactly one of each, the start first. Lines may end with LF or CRLF.
The region, from its start marker through the end marker's line break, with the start marker line's line ending, or null when the document has neither marker.
Throws
INVALID_MARKED_REGIONif a marker shares its line with other text, appears twice or without
Says how long before or after another moment a date is, in the largest whole unit of days, hours and minutes, rounded, such as for "Observed 5 minutes ago". Less than a minute either way reads as now.
Generates the version 5 UUID of a name within a namespace, as RFC 9562 defines it: the same namespace and name always give the same UUID, and any other name gives another, such as for a stable ID derived from an operation's own ID.
The namespace, a UUID, such as one an app picks for its own IDs.
namestring
The name, as text whose UTF-8 bytes are hashed.
Returns
string
The UUID, version 5.
Throws
INVALID_UUID_NAMESPACEThe namespace is not a valid UUID.
Example
import { generateNameBasedUuid } from 'jg-utilities';// the DNS namespace from RFC 9562generateNameBasedUuid('6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'www.example.com'); // => '2ed6657d-e927-568b-95e1-2665a8aea6a2'
Generates a string of characters picked at random from a set. Each character is drawn with crypto.getRandomValues and rejection sampling, so every character in the set is equally likely and the result is fit for secrets such as passwords.
The characters to pick from. Defaults to ASCII letters and digits.
Defaults to 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.
Returns
string
The random string, or an empty string when the length or the set is empty.
Example
import { generateRandomString } from 'jg-utilities';generateRandomString(12); // such as 'aZ3kPq9TbW0x'generateRandomString(6, '0123456789'); // such as '402913'
Lists every UTC calendar date in an inclusive range, in ascending order, such as the days of a chart's axis. It gives one entry per day, so bound the range before passing one from a client.
import { generateUuid } from 'jg-utilities';generateUuid(4); // such as '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'generateUuid(7); // time-ordered, so later IDs sort after earlier ones
Gets the value of a property from the last item when the array length reaches the initial page size.
Signature
getNextPageParam<T extends object, K extends string | number | symbol>( propName: K, items: T[], initialPageSize: number,): T[K] | undefined
Parameters
propNameK
The property name to read from the last item.
itemsT[]
The array of items from which to read the last property value.
initialPageSizenumber
The expected page size required before reading the last property value.
Returns
T[K] | undefined
The last item's property value, or undefined when the array length is below the initial page size.
Example
import { getNextPageParam } from 'jg-utilities';// with a page size of 2, a full page means there may be moregetNextPageParam('id', [{ id: 1 }, { id: 2 }], 2); // => 2getNextPageParam('id', [{ id: 3 }], 2); // => undefined
Checks whether a global navigator object exists. It reports presence only and doesn't identify a browser: Node.js 21 and later define a global navigator too.
Signature
hasNavigator(): boolean
Returns
boolean
True when the navigator global is defined.
Example
import { hasNavigator } from 'jg-utilities';hasNavigator(); // true in browsers and in Node.js 21 or later
Verifies if a value has a valid Authorization Header format based on the RFC6750. Example: Authorization: Bearer eyJhbGciOiJIUzI1NiIXVCJ9TJV...r7E20RMHrHDcEfxjoYZgeFONFh7HgQ More info: - https://stackoverflow.com/questions/33265812/best-http-authorization-header-type-for-jwt - https://www.rfc-editor.org/rfc/rfc6750
Signature
isAuthorizationHeaderValid(value: unknown): value is string
Parameters
valueunknown
The value to be validated.
Returns
value is string
A boolean indicating whether the value is a valid Authorization header.
Verifies that a value is an integer written in plain decimal digits, as PostgreSQL returns a BIGINT, and that it fits a range: a signed 64-bit integer by default. Leading zeros, a plus sign, -0, decimals, exponents, and padding are rejected, so one number has one spelling.
Signature
isBigIntStringValid(value: unknown, min?: bigint, max?: bigint): value is string
Parameters
valueunknown
The value to be validated.
min?bigint
No description.
Defaults to INT64_MIN.
max?bigint
No description.
Defaults to INT64_MAX.
Returns
value is string
A boolean indicating whether the value is such a string, within the range.
Verifies if a value is a valid email address with a maximum length of 320 characters. Important: when providing forbiddenExtensions, ensure to include the dot (.) at the beginning. For example, to forbid .con, use ['.con'].
Signature
isEmailValid(value: unknown, forbiddenExtensions?: string[]): value is string
Parameters
valueunknown
The value to be validated.
forbiddenExtensions?string[]
No description.
Defaults to ....
Returns
value is string
A boolean indicating whether the value is a valid email address and does not have a forbidden extension.
Compares two values. Primitives, null, and undefined are compared with Object.is, so NaN equals NaN and 0 doesn't equal -0. Two objects or arrays are compared deeply, ignoring key order, through their deterministic JSON. An object never equals a primitive.
Verifies if a value is a file name that is valid on Windows, macOS and Linux: one path component whose UTF-8 size is between 1 and maxBytes. It rejects ".", "..", control characters, the characters < > : " / \ | ? *, a trailing space or period, and Windows device names such as "con" or "lpt1.txt", in any letter case.
Signature
isFileNameValid(value: unknown, maxBytes?: number): value is string
Parameters
valueunknown
The value to be validated.
maxBytes?number
No description.
Defaults to MAX_FILE_NAME_BYTES.
Returns
value is string
A boolean indicating whether the value is a portable file name.
Verifies if a value is a valid integer and is within a range (optional). If a range is not provided, it will use the properties Number.MIN_SAFE_INTEGER & Number.MAX_SAFE_INTEGER.
Signature
isIntegerValid(value: unknown, min?: number, max?: number): value is number
Parameters
valueunknown
The value to be validated.
min?number
No description.
max?number
No description.
Returns
value is number
A boolean indicating whether the value is a valid integer and within the specified range.
Verifies if a value is a valid number and is within a range (optional). The minimum value defaults to Number.MIN_SAFE_INTEGER (-9007199254740991) while the maximum value defaults to Number.MAX_SAFE_INTEGER (9007199254740991).
Signature
isNumberValid(value: unknown, min?: number, max?: number): value is number
Parameters
valueunknown
The value to be validated.
min?number
No description.
Defaults to Number.MIN_SAFE_INTEGER.
max?number
No description.
Defaults to Number.MAX_SAFE_INTEGER.
Returns
value is number
A boolean indicating whether the value is a valid number and within the specified range.
Verifies if a password meets the following requirements: - Meets a length range (Defaults to 8 - 2048) - At least one uppercase letter - At least one lowercase letter - At least one number - At least one special character
Signature
isPasswordValid( value: unknown, minLength?: number, maxLength?: number,): value is string
Parameters
valueunknown
The value to be validated.
minLength?number
No description.
Defaults to 8.
maxLength?number
No description.
Defaults to 2048.
Returns
value is string
A boolean indicating whether the value is a valid password.
Verifies if a value is a relative path, with / between its components, that works on Windows, macOS and Linux: no empty, . or .. component, and every component a valid file name, as isFileNameValid checks it, within maxComponentBytes and matching componentPattern when one is given. With maxPathBytes, the whole path must fit it too. Sizes are UTF-8 bytes.
Signature
isPortablePathValid( value: unknown, options??: IPortablePathOptions,): value is string
Parameters
valueunknown
The value to be validated.
options??IPortablePathOptions
The size limits and the pattern each component must match.
Defaults to {}.
Returns
value is string
A boolean indicating whether the value is a portable relative path.
Tells whether a click is a plain primary click, the main button without Alt, Ctrl, Meta, or Shift, such as before a link handler takes over navigation, so a click that opens a new tab or window stays the browser's. It reads only the event's fields, so it takes DOM and React events.
Verifies if a value is a valid unix timestamp in milliseconds. The smallest value is set for the beginning of the Unix epoch (January 1st, 1970 - 14400000) while the largest value is based on the numeric limit established by JavaScript (9007199254740991).
Signature
isTimestampValid(value: unknown): value is number
Parameters
valueunknown
The value to be validated.
Returns
value is number
A boolean indicating whether the value is a valid unix timestamp in milliseconds.
Measures how many UTF-8 bytes JSON.stringify(value) would write, without writing it, such as to refuse a message over a size limit before serializing it. It stops once the count passes maxBytes, so an oversized value costs little. It takes primitives as well as objects and arrays, calls toJSON as JSON.stringify does, and descends at most 64 levels.
Signature
measureJsonBytes(value: unknown, maxBytes: number): number
Parameters
valueunknown
The value.
maxBytesnumber
The limit, a non-negative safe integer.
Returns
number
The exact size when it's at most maxBytes, otherwise a number above it.
Throws
INVALID_BYTE_LIMITThe byte limit must be a non-negative safe integer.
UNABLE_TO_SERIALIZE_JSONif JSON.stringify would write nothing, or throw, such as for a cycle
Erroror a BigInt, or the value is nested more than 64 levels deep
Normalizes a query string by removing control characters, zero-width characters, and extra spaces, then converts it to lowercase. Optionally, it can truncate the string to a specified maximum length.
Reads the id of the single YouTube video a URL points at, such as one a user pasted. It accepts https URLs without credentials or a port, on these hosts and paths, and ignores the query, apart from v, and the hash: - youtu.be/<id> - youtube.com/watch?v=<id>, /shorts/<id>, /embed/<id>, and /live/<id> - m.youtube.com/watch?v=<id> - youtube-nocookie.com/embed/<id>
The www. form of each host works too. Anything else, such as a playlist, a channel, or a value that isn't a URL, returns null rather than throwing.
Checks whether the user asked the system to reduce motion. The view defaults to the global window when one exists; pass another one, such as an element's ownerDocument.defaultView, to check a different window.
Formats the number that will be inserted in a badge so it doesn't take too much space. If the current count is 0, it returns undefined as the badge shouldn't be displayed.
Formats a date instance based on a template. The output is always en-US, whatever the runtime's locale, and hours have no leading zero. - date-short: 12/05/2024 (Default) - date-medium: December 5, 2024 - date-long: Thursday, December 5, 2024 - time-short: 9:05 PM - time-medium: 9:05:20 PM - datetime-short: 12/5/2024, 9:05 PM - datetime-medium: December 5, 2024 at 9:05 PM - datetime-long: Thursday, December 5, 2024 at 9:05:20 PM
Without a time zone, the runtime's local zone is used, so a server and a browser can format the same instant differently. Pass 'UTC' for calendar dates and for data defined in UTC periods, and an explicit time zone for server-rendered output. Templates with a time end with the zone's short name when a time zone is given, for example "December 5, 2024 at 11:30 PM UTC". Prefer date-medium, datetime-medium and time-medium in UI text over the numeric templates.
Formats a bytes value into a human readable format. Bigints, such as BIGINT database values, are accepted too; values beyond Number.MAX_SAFE_INTEGER are formatted without losing precision.
Signature
prettifyFileSize(value: number | bigint, decimalPlaces?: number): string
Parameters
valuenumber | bigint
The bytes value to be formatted, as a number or a bigint.
decimalPlaces?number
The number of decimal places to include. Defaults to 2.
Replaces the secrets in free text, such as a log line or a file headed for an AI model: complete PEM private key blocks first, then every match of the token patterns, which default to SECRET_TOKEN_PATTERNS. It runs in linear time over the key blocks, matches each pattern globally whatever its flags, and never throws for any text, so a logger can call it safely.
With preserveLineBreaks, a private key becomes the replacement followed by its line breaks, or preceded by them when it ends the text, so line numbers in the text stay the same.
Removes a URL's query and fragment when the query holds any of the named parameters, such as a token, so the URL can be logged. A name matches exactly, after decoding, or as the start of an array parameter, such as token[]. A name that can't be decoded could hide one of them, so the whole URL becomes [redacted], as does a URL that is only a query.
Checks an untrusted path, such as a returnTo parameter after sign-in, and returns it only when it stays on the same site and outside the excluded paths, so a link can't send someone to another site or back into a sign-in loop. It must start with one /; a protocol-relative URL, an absolute URL, a backslash, or a path that can't be decoded is rejected. An excluded path matches itself and everything under it, by whole segments, case-insensitively, and after decoding, as a router matches routes.
Executes an asynchronous function persistently, retrying on error with incremental delays defined in retryScheduleDuration (seconds). When the signal aborts, the returned promise rejects immediately, even while an attempt is pending. The attempt's own work isn't cancelled; pass the signal into it as well when it supports one.
Runs items at most concurrency at a time and commits their results one at a time, in the items' order, such as evaluations that run in parallel but are written to a checkpoint in a fixed order. A worker starts its next item only after its previous one is committed, so at most concurrency results wait uncommitted. The first failure, of an item or of a commit, stops new items: the ones already running finish without being committed, and then the promise rejects with that failure.
Runs work that belongs to a state that can change while it runs, such as a request made for the signed-in account, and keeps its outcome from reaching a newer state, such as another account that signed in meanwhile. It subscribes to the stores whose changes matter, and once a change leaves the work stale, it aborts the work's signal and stays stale, even if the state changes back. Then whatever the work does, resolving or rejecting, ends in createStaleError. The caller's signal aborts the work too, with its own reason.
Creates a shallow copy of the input array and shuffles it with the Fisher-Yates algorithm, drawing each position with crypto.getRandomValues, so every order is equally likely.
Signature
shuffleArray<T>(input: T[]): T[]
Parameters
inputT[]
The array to be shuffled.
Returns
T[]
A new array with the values of the input array in a random order.
Throws
INVALID_OR_EMPTY_ARRAYif the input is not array or it is empty
Example
import { shuffleArray } from 'jg-utilities';shuffleArray([1, 2, 3, 4]); // a new array, such as [3, 1, 4, 2]
Stringifies an object or an array with every object's keys in the order RFC 8785 sets, by UTF-16 code unit, so equal data always gives the same text, such as for a cache key, a digest, or a signature. The text is built directly, so integer-like keys sort with the others instead of first. Default mode writes what JSON.stringify writes: it calls toJSON, leaves out properties that are undefined, functions, or symbols, and writes null for them in arrays, for holes, and for NaN and ±Infinity. Strict mode accepts only plain JSON data, and throws for undefined anywhere, NaN and ±Infinity, BigInts, symbols, functions, objects that aren't plain, symbol keys, and holes, naming the value's path; -0 is written as 0.
Takes the longest run of items, from the first, whose page fits a byte budget, such as an API response or a CLI's output page that must stay under a size limit. It measures each item's JSON once, adds it to the size of the items as a JSON array, and asks measurePage for the size of the page around them, such as with an envelope and a cursor, stopping at the first item that doesn't fit. It reads the items lazily, so a generator that builds each item stops being read there. measurePage receives the same array each time, so copy it to keep it.
The value to create a Date instance from. It can be a Date object, a timestamp, or a date string.
Returns
Date
A Date instance.
Example
import { toDate } from 'jg-utilities';toDate('2026-10-17T08:30:00.000Z'); // a Date for that instanttoDate(1_792_217_505_000); // a Date from a millisecond epoch
Converts a time string into milliseconds. The time string should be in the format of "{value} {unit}", where the value is a number and the unit can be milliseconds, seconds, minutes, hours, days, weeks, months, or years. For example: "2 days", "5 minutes", "2 hours".
Converts a string value into a slug, meeting the following requirements: - lowercase - removes accents/diacritics - replaces spaces and separators with "-" - removes invalid URL characters - collapses repeated "-" - trims leading/trailing "-"
Signature
toSlug(value: string): string
Parameters
valuestring
The string value to convert.
Returns
string
A string converted to a slug.
Throws
UNABLE_TO_SLUGIFY_STRINGif the resulting slug is empty after processing the input string
Gives the UTC calendar date of an instant, as YYYY-MM-DD, such as the day a UTC report or analytics period counts it in. The instant must fall in the years 0000 to 9999.
Signature
toUtcDate(value?: IDateValue): string
Parameters
value?IDateValue
No description.
Returns
string
The UTC date, such as 2026-10-17.
Throws
INVALID_DATEThe value is not a valid date in the years 0000 to 9999.
Example
import { toUtcDate } from 'jg-utilities';toUtcDate('2026-10-17T23:30:00.000-05:00'); // => '2026-10-18'toUtcDate(0); // => '1970-01-01'toUtcDate(); // today's UTC date
Shortens a string to the longest prefix of whole code points that fits a number of UTF-8 bytes, such as a push notification's body under the provider's payload limit. A character made of several code points, such as an emoji with a skin tone, can lose its last ones, but a code point is never split.
Puts a block into a document between its markers: it replaces the region findMarkedRegion finds, using the start marker line's line ending, or appends the block when the document has no markers, after a blank line, using CRLF only when every line break of the document is CRLF. Everything outside the region, a byte order mark included, stays as it was.
Probes a condition until it returns a value other than null, undefined or false, and resolves with that value. It probes immediately, then after every interval, and one final time at the deadline. The timeout bounds the whole call, so it rejects even while a probe is still pending. A running probe isn't cancelled when the call stops waiting; pass your own signal into it when it supports one.
The single-line secrets redactSecrets finds by default: GitHub's personal, OAuth, user, server, and refresh tokens, its fine-grained personal access tokens, and sk- API keys, such as OpenAI's, each with at least 20 characters after its prefix.
a copy of the bytes under the key, which becomes the most recently used, or undefined
setproperty
(key: string, bytes: Uint8Array) => boolean
stores a copy of the bytes, evicting the least recently used entries that aren't retained, and returns whether it did; it doesn't when the bytes exceed maxBytes, the key's entry is retained, or retained entries leave no room
retainproperty
(key: string) => IByteCacheRetention | undefined
pins the key's entry without changing its order, or returns undefined when there's none
validates the payload when it's encoded and when a cursor is decoded
maxLengthproperty
number
the longest cursor, in characters, that's encoded or decoded. Defaults to 2048
matchesScopeproperty
(payload: TPayload, scope: TScope) => boolean
Whether a decoded payload belongs to the list being read. Without it, every key of the scope must equal the payload's, with null and undefined counting as the same value.
Date Template A date can be prettified by choosing a template that meets the user's requirements. - date-short: 12/05/2024 - date-medium: December 5, 2024 - date-long: Thursday, December 5, 2024 - time-short: 9:05 PM - time-medium: 9:05:20 PM - datetime-short: 12/5/2024, 9:05 PM - datetime-medium: December 5, 2024 at 9:05 PM - datetime-long: Thursday, December 5, 2024 at 9:05:20 PM
the options accepted by stringifyJsonDeterministically
Declaration
type IDeterministicJsonOptions = { strict?: boolean | undefined;};
Members
strictproperty
boolean
accepts only plain JSON data, such as a document whose bytes a digest or signature covers, and throws for anything JSON.stringify would drop, convert, or call toJSON on
A value that survives a trip through JSON. An object's values may also be undefined, because JSON.stringify drops them, so an object with optional fields fits. Interfaces and classes don't fit, because they have no index signature: pass an interface-typed value as { ...value }, or declare its type with type.
Declaration
type IJsonValue = string | number | boolean | null | IJsonValue[] | { [key: string]: IJsonValue | undefined;};
where a marked region sits in a document: from its start marker through its end marker's line break, and the line break its start marker's line ends with
starts a task now and resolves or rejects as it does, or rejects with TASK_RUNNER_CLOSED once the runner is closing, or with TASK_RUNNER_BUSY while a task with the same key runs
starts a task, such as a timer's tick, unless the runner is closing or a task with the same key runs, resolving to false at once then, and to true once the task settles. Its error goes to onError
signalproperty
AbortSignal
the runner's signal, aborted by close({ abort: true })
isClosingproperty
() => boolean
whether close was called
isIdleproperty
(key?: string) => boolean
whether no task runs, or no task with the key when one is given