Current section
Files
Jump to
Current section
Files
src/gbr/js/dom/util_ffi.mjs
/**
* Módulo c/ funções utilitárias.
*/
import {
BitArray as BitArrayInner,
Ok as GleamOk,
Error as GleamError
} from "../../../gleam.mjs";
import {
toBitArray as toBitArrayInner
} from "../../../../gleam_stdlib/gleam.mjs";
import {
unwrap as opt_unwrap
} from "../../../../gleam_stdlib/gleam/option.mjs";
export const unwrap = opt_unwrap
export const BitArray = BitArrayInner
export const toBitArray = (array) => toBitArrayInner(array);
export const newOk = (result) => new GleamOk(result);
export const newError = (msg) => new GleamError(msg);
export const getError = (error) => error && error.message
? error.message
: `${JSON.stringify(error)}`;
export function checkNull(value, errMsg) {
if (value !== null && value !== undefined) {
return newOk(value);
} else {
return newError(errMsg + " : " + "Value is null");
}
}
export function maybe(cb, errMsg) {
try {
return newOk(cb());
} catch (error) {
return newError(errMsg + " : " + getError(error));
}
}
export async function maybeAsync(cb, errMsg) {
try {
return newOk(await cb());
} catch (error) {
return newError(errMsg, ":", getError(error));
}
}
export function maybeInstanceOf(obj, whatsInstanceOf, errMsg) {
try {
if (!whatsInstanceOf) {
return newError(errMsg, "not found:", whatsInstanceOf);
}
if (!(obj instanceof whatsInstanceOf)) {
return newError(errMsg, typeof obj, "not is of", whatsInstanceOf)
}
return newOk(obj);
} catch (error) {
return newError(errMsg, "instance of:" + getError(error));
}
}
export function unexpectError(msg) {
throw new Error("Unexpected error: " + msg);
}