All files / src/tools/manifest dev-fields.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 5/5
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                    15x                     11x                           189x   118x 1x                     43x   43x                   18x    
/**
 * The small readers every block of a unit's paf.dev.config.json shares, so the seeds, the walks, and
 * the switches word a refusal the same way and read a clock the same way.
 */
import { ToolError } from '../shared/tool-error.ts';
 
/** Where a message points, such as `my-family/paf.dev.config.json` and `face-a's `, empty for the top of the file. */
export type Where = { file: string; owner: string };
 
/** A 24-hour `HH:MM`, with the hour and the minute caught. */
const CLOCK_TEXT = /^([01]\d|2[0-3]):([0-5]\d)$/;
 
/**
 * The start of a refusal for one field.
 *
 * @param where Where the field sits.
 * @param field The field, such as `stores.weather.temp`.
 * @param value What the file holds there.
 * @return The opening, such as `my-family/paf.dev.config.json sets face-a's stores.weather.temp to 900`.
 */
export function sets(where: Where, field: string, value: unknown): string {
  return `${where.file} sets ${where.owner}${field} to ${JSON.stringify(value)}`;
}
 
/**
 * Refuses any field a block holds that it does not take, naming the ones it does, since a misspelt
 * field would otherwise be left out with nothing to say.
 *
 * @param block The block.
 * @param known The fields it takes.
 * @param where Where the block sits.
 * @param field The block's own name, such as `stores.phone`.
 * @param what What takes the block, as the refusal names it, such as `the phone seed`.
 */
export function onlyKnown(block: Record<string, unknown>, known: readonly string[], where: Where, field: string, what: string): void {
  const unknown = Object.keys(block).filter((key) => !known.includes(key));
 
  if (unknown.length > 0) {
    throw new ToolError(`${where.file} sets ${where.owner}${field}.${unknown.join(', ')}, which ${what} does not take. It takes ${known.join(', ')}`);
  }
}
 
/**
 * A 24-hour `HH:MM` as minutes past midnight.
 *
 * @param value The time as the file holds it.
 * @return Minutes past midnight, 0 to 1439, or null when the value is not a 24-hour `HH:MM`.
 */
export function clockMinutes(value: unknown): number | null {
  const time = typeof value === 'string' ? CLOCK_TEXT.exec(value) : null;
 
  return time ? Number(time[1]) * 60 + Number(time[2]) : null;
}
 
/**
 * Minutes past midnight as a 24-hour `HH:MM`, the way the file writes a clock.
 *
 * @param minute Minutes past midnight, 0 to 1439.
 * @return The clock, such as `08:05`.
 */
export function clockText(minute: number): string {
  return `${String(Math.floor(minute / 60)).padStart(2, '0')}:${String(minute % 60).padStart(2, '0')}`;
}