Skip to main content

src/cadre_ffi.mjs

import { List } from "./gleam.mjs";
import { Vec2 } from "../vec/vec/vec2.mjs";
import { Vec3 } from "../vec/vec/vec3.mjs";
import { Vec4 } from "../vec/vec/vec4.mjs";

export function property_to_string(property) {
  if (property === true) return "true";
  if (property === false) return "false";

  const type = typeof property;
  if (type === "string") return property;
  if (type === "number" || type === "bigint" || Number.isInteger(property)) {
    return property.toString();
  }

  if (Array.isArray(property)) {
    return property.map((property) => property_to_string(property)).join(",");
  }

  if (property instanceof List) {
    return property
      .toArray()
      .map((property) => property_to_string(property))
      .join(",");
  }
  if (property instanceof Vec2) {
    return `${property.x} ${property.y}`;
  }
  if (property instanceof Vec3) {
    return `${property.x} ${property.y} ${property.z}`;
  }
  if (property instanceof Vec4) {
    return `${property.x} ${property.y} ${property.z} ${property.w}`;
  }

  return "";
}