UNPKG

@wandelbots/nova-js

Version:

Official JS client for the Wandelbots API

87 lines (86 loc) 2.6 kB
//#region src/lib/converters.ts /** * Parse a string as a URL, with options to enforce or default the scheme. */ function parseUrl(url, options = {}) { const { scheme, defaultScheme } = options; const schemeRegex = /^[a-zA-Z]+:\/\//; if (scheme) { url = url.replace(schemeRegex, ""); url = `${scheme}://${url}`; } else if (defaultScheme && !schemeRegex.test(url)) url = `${defaultScheme}://${url}`; return new URL(url); } /** * Attempt to parse a string as a URL; return undefined if we can't */ function tryParseUrl(url, options = {}) { try { return parseUrl(url, options); } catch { return; } } /** * Permissively parse a NOVA instance URL from a config variable. * If scheme is not specified, defaults to https for *.wandelbots.io hosts, * and http otherwise. * Throws an error if a valid URL could not be determined. */ function parseNovaInstanceUrl(url) { if (tryParseUrl(url, { defaultScheme: "http" })?.host.endsWith(".wandelbots.io")) return parseUrl(url, { defaultScheme: "https" }); else return parseUrl(url, { defaultScheme: "http" }); } /** Try to parse something as JSON; return undefined if we can't */ function tryParseJson(json) { try { return JSON.parse(json); } catch { return; } } /** Try to turn something into JSON; return undefined if we can't */ function tryStringifyJson(json) { try { return JSON.stringify(json); } catch { return; } } /** * Converts object parameters to query string. * e.g. { a: "1", b: "2" } => "?a=1&b=2" * {} => "" */ function makeUrlQueryString(obj) { const str = new URLSearchParams(obj).toString(); return str ? `?${str}` : ""; } /** Convert radians to degrees */ function radiansToDegrees(radians) { return radians * (180 / Math.PI); } /** Convert degrees to radians */ function degreesToRadians(degrees) { return degrees * (Math.PI / 180); } /** * Check for coordinate system id equivalence, accounting for the "world" default * on empty/undefined values. */ function isSameCoordinateSystem(firstCoordSystem, secondCoordSystem) { if (!firstCoordSystem) firstCoordSystem = "world"; if (!secondCoordSystem) secondCoordSystem = "world"; return firstCoordSystem === secondCoordSystem; } /** * Helpful const for converting {x, y, z} to [x, y, z] and vice versa */ const XYZ_TO_VECTOR = { x: 0, y: 1, z: 2 }; //#endregion export { parseNovaInstanceUrl as a, tryParseJson as c, makeUrlQueryString as i, tryParseUrl as l, degreesToRadians as n, parseUrl as o, isSameCoordinateSystem as r, radiansToDegrees as s, XYZ_TO_VECTOR as t, tryStringifyJson as u }; //# sourceMappingURL=converters-DnG1fX23.mjs.map