9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
/**
|
|
* Shared utilities for CLI adapters.
|
|
*/
|
|
import { ArgumentError } from '@jackwener/opencli/errors';
|
|
/**
|
|
* Clamp a numeric value to [min, max].
|
|
* Matches the signature of lodash.clamp and Rust's clamp.
|
|
*/
|
|
export function clamp(value, min, max) {
|
|
return Math.max(min, Math.min(value, max));
|
|
}
|
|
export function clampInt(raw, fallback, min, max) {
|
|
const parsed = Number(raw);
|
|
if (!Number.isFinite(parsed)) {
|
|
return fallback;
|
|
}
|
|
return clamp(Math.floor(parsed), min, max);
|
|
}
|
|
export function normalizeNumericId(value, label, example) {
|
|
const normalized = String(value ?? '').trim();
|
|
if (!/^\d+$/.test(normalized)) {
|
|
throw new ArgumentError(`${label} must be a numeric ID`, `Pass a numeric ${label}, for example: ${example}`);
|
|
}
|
|
return normalized;
|
|
}
|
|
export function requireNonEmptyQuery(value, label = 'query') {
|
|
const normalized = String(value ?? '').trim();
|
|
if (!normalized) {
|
|
throw new ArgumentError(`${label} cannot be empty`);
|
|
}
|
|
return normalized;
|
|
}
|