CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
parseUtil.js110 linesDownload Raw Back to helpers
1import { getErrorMap } from "../errors.js";2import defaultErrorMap from "../locales/en.js";3export const makeIssue = (params) => {4    const { data, path, errorMaps, issueData } = params;5    const fullPath = [...path, ...(issueData.path || [])];6    const fullIssue = {7        ...issueData,8        path: fullPath,9    };10    if (issueData.message !== undefined) {11        return {12            ...issueData,13            path: fullPath,14            message: issueData.message,15        };16    }17    let errorMessage = "";18    const maps = errorMaps19        .filter((m) => !!m)20        .slice()21        .reverse();22    for (const map of maps) {23        errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;24    }25    return {26        ...issueData,27        path: fullPath,28        message: errorMessage,29    };30};31export const EMPTY_PATH = [];32export function addIssueToContext(ctx, issueData) {33    const overrideMap = getErrorMap();34    const issue = makeIssue({35        issueData: issueData,36        data: ctx.data,37        path: ctx.path,38        errorMaps: [39            ctx.common.contextualErrorMap, // contextual error map is first priority40            ctx.schemaErrorMap, // then schema-bound map if available41            overrideMap, // then global override map42            overrideMap === defaultErrorMap ? undefined : defaultErrorMap, // then global default map43        ].filter((x) => !!x),44    });45    ctx.common.issues.push(issue);46}47export class ParseStatus {48    constructor() {49        this.value = "valid";50    }51    dirty() {52        if (this.value === "valid")53            this.value = "dirty";54    }55    abort() {56        if (this.value !== "aborted")57            this.value = "aborted";58    }59    static mergeArray(status, results) {60        const arrayValue = [];61        for (const s of results) {62            if (s.status === "aborted")63                return INVALID;64            if (s.status === "dirty")65                status.dirty();66            arrayValue.push(s.value);67        }68        return { status: status.value, value: arrayValue };69    }70    static async mergeObjectAsync(status, pairs) {71        const syncPairs = [];72        for (const pair of pairs) {73            const key = await pair.key;74            const value = await pair.value;75            syncPairs.push({76                key,77                value,78            });79        }80        return ParseStatus.mergeObjectSync(status, syncPairs);81    }82    static mergeObjectSync(status, pairs) {83        const finalObject = {};84        for (const pair of pairs) {85            const { key, value } = pair;86            if (key.status === "aborted")87                return INVALID;88            if (value.status === "aborted")89                return INVALID;90            if (key.status === "dirty")91                status.dirty();92            if (value.status === "dirty")93                status.dirty();94            if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {95                finalObject[key.value] = value.value;96            }97        }98        return { status: status.value, value: finalObject };99    }100}101export const INVALID = Object.freeze({102    status: "aborted",103});104export const DIRTY = (value) => ({ status: "dirty", value });105export const OK = (value) => ({ status: "valid", value });106export const isAborted = (x) => x.status === "aborted";107export const isDirty = (x) => x.status === "dirty";108export const isValid = (x) => x.status === "valid";109export const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;110