Skip to content

Latest commit

 

History

History
144 lines (95 loc) · 5.33 KB

File metadata and controls

144 lines (95 loc) · 5.33 KB

@happyvertical/utils

Foundation utilities for the HAVE SDK: ID generation, date parsing, URL handling, string conversion, error classes, logging, code extraction/validation/sandboxing, CLI argument parsing, and environment config loading.

Installation

pnpm add @happyvertical/utils
# Published to GitHub Packages — requires .npmrc with @happyvertical registry

A browser-safe entry point is available at @happyvertical/utils/browser (excludes Node.js-specific code like node:vm sandbox and node:crypto hashing).

Usage

ID Generation

import { makeId, createId, isCuid } from '@happyvertical/utils';

const id = makeId();         // CUID2 (default)
const uuid = makeId('uuid'); // UUID via crypto.randomUUID()
const cuid = createId();     // CUID2 directly
isCuid(id);                  // true

String & URL Utilities

import { camelCase, snakeCase, keysToCamel, keysToSnake, makeSlug, urlFilename, isUrl } from '@happyvertical/utils';

camelCase('hello-world');      // "helloWorld"
snakeCase('helloWorld');       // "hello_world"
keysToCamel({ user_name: 'j' }); // { userName: 'j' } (recursive)
makeSlug('My Title & Co.');   // "my-title-38-co"
urlFilename('https://example.com/path/file.pdf'); // "file.pdf"
isUrl('https://example.com'); // true

Date Utilities

import { dateInString, formatDate, parseDate, parseAmazonDateString, addInterval } from '@happyvertical/utils';

dateInString('Report_January_15_2023.pdf'); // Date(2023, 0, 15)
formatDate(new Date(), 'yyyy-MM-dd');       // "2023-01-15"
parseDate('2023-01-15');                    // Date object (ISO)
parseDate('01/15/2023', 'MM/dd/yyyy');      // Date object (custom format)
parseAmazonDateString('20220223T215409Z');   // Date object
addInterval(new Date(), { days: 7 });       // Date 7 days from now

Error Classes

All extend BaseError with code, context, and timestamp fields, plus toJSON():

import { ValidationError, ApiError, NetworkError, TimeoutError, ParsingError, FileError, DatabaseError } from '@happyvertical/utils';

throw new ValidationError('Invalid email', { field: 'email', value: input });

Code Extraction & Sandbox

Extract code blocks from markdown/AI responses, validate, and execute in isolated node:vm contexts:

import { extractCodeBlock, extractJSON, validateCode, executeInSandbox } from '@happyvertical/utils';

const code = extractCodeBlock(markdownText, 'javascript');
const data = extractJSON<{ name: string }>(aiResponse);

const result = validateCode(code, { maxLength: 10000 });
if (result.valid) {
  const output = executeInSandbox(code, { globals: { data }, timeout: 5000 });
}

CLI Argument Parsing

import { parseCliArgs } from '@happyvertical/utils';

const parsed = parseCliArgs(process.argv, [
  { name: 'build', description: 'Build project', options: { output: { type: 'string', description: 'Output dir' } } }
]);
// { command: 'build', args: [], options: { output: './dist' } }

Environment Config

import { loadEnvConfig } from '@happyvertical/utils';

// Loads HAVE_AI_* env vars, merged with user options (user options take precedence)
const config = loadEnvConfig({ provider: 'openai' }, {
  packageName: 'ai',
  schema: { provider: 'string', model: 'string', timeout: 'number' }
});

Logging

import { getLogger, setLogger, disableLogging } from '@happyvertical/utils';

const logger = getLogger();
logger.info('Started', { userId: 123 });
disableLogging(); // Switch to no-op logger

API Reference

IDsmakeId(type?), createId(), isCuid(id)

StringscamelCase(str), snakeCase(str), keysToCamel(obj), keysToSnake(obj), domainToCamel(str), makeSlug(str)

URLsurlFilename(url), urlPath(url), isUrl(str), normalizeUrl(url), generateScopeFromUrl(url), hashPageContent(html)

DatesdateInString(str), prettyDate(str), parseAmazonDateString(str), formatDate(date, format?), parseDate(str, format?), isValidDate(date), addInterval(date, duration)

PluralizationpluralizeWord(word), singularize(word), isPlural(word), isSingular(word)

Type GuardsisArray(obj), isPlainObject(obj)

AsyncwaitFor(fn, options?), sleep(ms)

CodeextractCodeBlock(text, lang?), extractAllCodeBlocks(text, lang?), extractJSON<T>(text), extractFunctionDefinition(code, name), validateCode(code, options?), isSafeCode(code), createSandbox(options?), executeCode(code, sandbox, options?), executeCodeAsync(code, sandbox, options?), executeInSandbox(code, options?), executeInSandboxAsync(code, options?)

CLIparseCliArgs(argv, commands, builtInCommands?)

ConfigloadEnvConfig<T>(userOptions?, options?), toCamelCase(str), toScreamingSnakeCase(str), convertType(value, type)

LogginggetLogger(), setLogger(logger), disableLogging(), enableLogging()

MisclogTicker(tick, options?), getTempDirectory(subfolder?)

Error ClassesBaseError, ValidationError, ApiError, FileError, NetworkError, DatabaseError, ParsingError, TimeoutError

TypesLogger, ErrorCode, Command, OptionConfig, ParsedArgs, ConfigOptions<T>, ValidationOptions, ValidationResult, SandboxOptions, ExecuteOptions

License

MIT