core/converters/surface.converter.ts

core/converters/surface.converter.ts is a file in Bane's Lab Site. 28 lines of code and 8 definitions.

import type { LineEdit, SurfaceLine } from "#types/surface.types";
import { LINE_BREAK } from "#configuration/constants/code.constants";

export const lineEdit = function lineEdit(before: readonly string[], after: readonly SurfaceLine[]): LineEdit {
    const shorter = Math.min(before.length, after.length);
    let prefix = 0;
    while (prefix < shorter && before[prefix] === after[prefix]?.text) {
        prefix += 1;
    }
    let suffix = 0;
    while (suffix < shorter - prefix && before[before.length - 1 - suffix] === after[after.length - 1 - suffix]?.text) {
        suffix += 1;
    }
    return {
        at: prefix,
        inserted: after.slice(prefix, after.length - suffix),
        removed: before.length - suffix - prefix,
    };
};

export const indentOf = function indentOf(text: string): number {
    return text.length - text.trimStart().length;
};

export const outputRest = function outputRest(shown: string | undefined, output: string): string | null {
    return shown !== undefined && shown !== "" && output.startsWith(shown) ? output.slice(shown.length) : null;
};

export const outputLines = function outputLines(output: string): string[] {
    const lines = output.split(LINE_BREAK);
    return lines.at(-1) === "" ? lines.slice(0, -1) : lines;
};