Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 32 lines · 1.0 KiB · at 355e324

// The interop boundary: converts the outside world's nulls, undefineds, and// throws into Option and Result at the edge of tsus code.import { Some, None, Ok, Err } from "./index.js";export const fromNullable = (value) =>  value === null || value === undefined ? None : Some(value);export const wrapSync = (fn) => (...args) => {  try {    return Ok(fn(...args));  } catch (error) {    return Err(error);  }};export const wrapAsync = (fn) => async (...args) => {  try {    return Ok(await fn(...args));  } catch (error) {    return Err(error);  }};// Narrowing helpers for values typed unknown (parsed JSON, request bodies).// They exist so .tsus code can inspect outside data without `as` casts// (TSUS402) or null checks (TSUS002).export const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);export const asRecord = (value) => (isRecord(value) ? Some(value) : None);export const field = (value, key) =>  isRecord(value) && Object.hasOwn(value, key) ? fromNullable(value[key]) : None;