@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
76 lines (75 loc) • 3.4 kB
JavaScript
// deno-lint-ignore-file valibot-project/require-name-suffix
/**
* Common valibot schemas for primitive types used across the API.
* @module
*/ import * as v from "valibot";
// ============================================================
// Number
// ============================================================
/** Unsigned decimal number as a string (e.g., "123.45"). */ export const UnsignedDecimal = /* @__PURE__ */ (()=>{
return v.pipe(v.union([
v.string(),
v.number()
]), v.toString(), v.string(), v.transform((value)=>normalizeDecimalString(value)), v.regex(/^[0-9]+(\.[0-9]+)?$/));
})();
/** Decimal number as a string, can be negative (e.g., "-123.45"). */ export const Decimal = /* @__PURE__ */ (()=>{
return v.pipe(v.union([
v.string(),
v.number()
]), v.toString(), v.string(), v.transform((value)=>normalizeDecimalString(value)), v.regex(/^-?[0-9]+(\.[0-9]+)?$/));
})();
/** Safe integer number (>= Number.MIN_SAFE_INTEGER & <= Number.MAX_SAFE_INTEGER). */ export const Integer = /* @__PURE__ */ (()=>{
return v.pipe(v.union([
v.string(),
v.number()
]), v.toNumber(), v.number(), v.safeInteger());
})();
/** Unsigned safe integer number (>= 0 & <= Number.MAX_SAFE_INTEGER). */ export const UnsignedInteger = /* @__PURE__ */ (()=>{
return v.pipe(v.union([
v.string(),
v.number()
]), v.toNumber(), v.number(), v.safeInteger(), v.minValue(0));
})();
/**
* Normalize a decimal string: drop redundant leading and trailing zeros and collapse negative zero.
* A string that is not a well-formed decimal is returned unchanged.
*
* @example
* ```ts ignore
* normalizeDecimalString("00123"); // => "123"
* normalizeDecimalString("1.2000"); // => "1.2"
* normalizeDecimalString(".5"); // => "0.5"
* normalizeDecimalString("-0.0"); // => "0"
* normalizeDecimalString("1.0.0"); // => "1.0.0" (not a decimal — unchanged)
* ```
*/ function normalizeDecimalString(value) {
const match = value.match(/^(-?)([0-9]*)(?:\.([0-9]*))?$/);
if (!match) return value;
const [, sign, intRaw, fracRaw = ""] = match;
if (intRaw === "" && fracRaw === "") return value;
const int = intRaw.replace(/^0+/, "") || "0";
let fracEnd = fracRaw.length;
while(fracEnd > 0 && fracRaw[fracEnd - 1] === "0")fracEnd--;
const frac = fracRaw.slice(0, fracEnd);
const body = frac === "" ? int : `${int}.${frac}`;
return sign === "-" && body !== "0" ? `-${body}` : body;
}
// ============================================================
// Hex
// ============================================================
/** Hexadecimal string starting with "0x". */ export const Hex = /* @__PURE__ */ (()=>{
return v.pipe(v.string(), v.regex(/^0[xX][0-9a-fA-F]+$/), v.transform((value)=>value.toLowerCase()));
})();
/** Ethereum address (42 characters hex string). */ export const Address = /* @__PURE__ */ (()=>{
return v.pipe(Hex, v.length(42));
})();
/** Client order ID (34 characters hex string). */ export const Cloid = /* @__PURE__ */ (()=>{
return v.pipe(Hex, v.length(34));
})();
// ============================================================
// Other
// ============================================================
/** Percentage string (e.g., "50%"). */ export const Percent = /* @__PURE__ */ (()=>{
return v.pipe(v.string(), v.regex(/^[0-9]+(\.[0-9]+)?%$/), v.transform((value)=>value));
})();
//# sourceMappingURL=_schemas.js.map