core/predicates/surface.predicate.ts

core/predicates/surface.predicate.ts is a file in Bane's Lab Site. 27 lines of code and 9 definitions.

import type { SurfaceFrame, SurfaceLine, SurfaceRecording } from "#types/surface.types";

const isRecord = function isRecord(value: unknown): value is Record<string, unknown> {
    return typeof value === "object" && value !== null;
};

const hasText = function hasText(value: Record<string, unknown>, keys: readonly string[]): boolean {
    return keys.every((key) => typeof value[key] === "string");
};

const listOf = function listOf(value: Record<string, unknown>, key: string): readonly unknown[] {
    const held = value[key];
    return Array.isArray(held) ? held : [];
};

const isLine = function isLine(value: unknown): value is SurfaceLine {
    return isRecord(value) && hasText(value, ["text"]) && typeof value["changed"] === "boolean";
};

const isFrame = function isFrame(value: unknown): value is SurfaceFrame {
    return isRecord(value) && hasText(value, ["command", "output", "surface"]) && listOf(value, "lines").every(isLine);
};

const isTextList = function isTextList(value: Record<string, unknown>, key: string): boolean {
    return Array.isArray(value[key]) && listOf(value, key).every((entry) => typeof entry === "string");
};

export const isRecording = function isRecording(value: unknown): value is SurfaceRecording {
    if (!isRecord(value) || !hasText(value, ["figure"]) || !isTextList(value, "opening")) {
        return false;
    }
    const frames = listOf(value, "frames");
    return frames.length > 0 && frames.every(isFrame);
};