@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
88 lines • 3.47 kB
JavaScript
/**
* Field-level readers for an authored spec: each reads one value, records why it is unusable,
* and returns `undefined` then. Ported from oh-my-pi 9df0457b31 (MIT, Copyright Can Boluk et al.).
*/
import { truncateText } from "./format.js";
import { FIELD_NAME, JOB_WIDGET_AGGS } from "./spec-types.js";
import { JOB_WIDGET_TONES } from "./view-types.js";
export function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function readList(value, where, errors, min, max) {
if (!Array.isArray(value)) {
errors.push(`${where}: expected an array`);
return undefined;
}
if (value.length < min || value.length > max) {
errors.push(`${where}: expected ${min}–${max} entries, got ${value.length}`);
return undefined;
}
return value;
}
export function readString(value, where, errors, options = {}) {
if (value === undefined || value === null) {
if (options.required)
errors.push(`${where}: required`);
return undefined;
}
if (typeof value !== "string") {
errors.push(`${where}: expected a string`);
return undefined;
}
const trimmed = value.trim();
if (trimmed.length === 0) {
if (options.required)
errors.push(`${where}: must not be empty`);
return undefined;
}
if (options.max !== undefined && trimmed.length > options.max) {
errors.push(`${where}: at most ${options.max} characters`);
return undefined;
}
return options.cut === undefined ? trimmed : truncateText(trimmed, options.cut);
}
export function readNumber(value, where, errors) {
if (value === undefined || value === null)
return undefined;
if (typeof value !== "number" || !Number.isFinite(value)) {
errors.push(`${where}: expected a finite number`);
return undefined;
}
return value;
}
export function readFieldName(value, where, errors) {
const name = readString(value, where, errors, { required: true });
if (name !== undefined && !FIELD_NAME.test(name)) {
errors.push(`${where}: "${name}" must match ${FIELD_NAME.source}`);
return undefined;
}
return name;
}
/** An absent `agg` is `fallback` when one is given (bars and meters); an unknown one is an error. */
export function readAgg(value, where, errors, fallback) {
if ((value === undefined || value === null) && fallback !== undefined)
return fallback;
const agg = JOB_WIDGET_AGGS.find((candidate) => candidate === value);
if (agg === undefined)
errors.push(`${where}: expected one of ${JOB_WIDGET_AGGS.join(", ")}`);
return agg;
}
export function readBetter(value, where, errors) {
if (value === undefined || value === null)
return undefined;
if (value === "higher" || value === "lower")
return value;
errors.push(`${where}: expected "higher" or "lower"`);
return undefined;
}
export function readTone(value, where, errors) {
const tone = JOB_WIDGET_TONES.find((candidate) => candidate === value);
if (tone === undefined)
errors.push(`${where}: expected one of ${JOB_WIDGET_TONES.join(", ")}`);
return tone;
}
/** `{ [name]: value }`, or `{}` when `value` is absent, for spreading optional properties. */
export function optional(value, name) {
return value === undefined ? {} : { [name]: value };
}
//# sourceMappingURL=spec-parse-primitives.js.map