// Runtime checks and cleanup for value types in the spec that aren't easily expressed through our type system. // These will help us to prevent silent failures and corrupted documents. // // Most of the rest of the types not defined here are either aliases of existing types or enumerations. // Enumerations should probably just be implemented as enums, with instructions to end-users, without a runtime check. // // // export const decimalNumber = (val: number): number => { if (isNaN(val)) { throw new Error(`Invalid value '${val}' specified. Must be an integer.`); } return Math.floor(val); }; // // // export const unsignedDecimalNumber = (val: number): number => { const value = decimalNumber(val); if (value < 0) { throw new Error(`Invalid value '${val}' specified. Must be a positive integer.`); } return value; }; // The xsd:hexBinary type represents binary data as a sequence of binary octets. // It uses hexadecimal encoding, where each binary octet is a two-character hexadecimal number. // Lowercase and uppercase letters A through F are permitted. For example, 0FB8 and 0fb8 are two // equal xsd:hexBinary representations consisting of two octets. // http://www.datypic.com/sc/xsd/t-xsd_hexBinary.html const hexBinary = (val: string, length: number): string => { const expectedLength = length * 2; if (val.length !== expectedLength || isNaN(Number(`0x${val}`))) { throw new Error(`Invalid hex value '${val}'. Expected ${expectedLength} digit hex value`); } return val; }; // // // // // export const longHexNumber = (val: string): string => hexBinary(val, 4); // // // // // export const shortHexNumber = (val: string): string => hexBinary(val, 2); // // // // // export const uCharHexNumber = (val: string): string => hexBinary(val, 1); // // // // // // // // // // export const universalMeasureValue = (val: string): string => { const unit = val.slice(-2); if (!universalMeasureUnits.includes(unit)) { throw new Error(`Invalid unit '${unit}' specified. Valid units are ${universalMeasureUnits.join(", ")}`); } const amount = val.substring(0, val.length - 2); if (isNaN(Number(amount))) { throw new Error(`Invalid value '${amount}' specified. Expected a valid number.`); } return `${Number(amount)}${unit}`; }; const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"]; // // // // // export const positiveUniversalMeasureValue = (val: string): string => { const value = universalMeasureValue(val); if (parseFloat(value) < 0) { throw new Error(`Invalid value '${value}' specified. Expected a positive number.`); } return value; }; // // // // // // // // // // // // // export const hexColorValue = (val: string): string => { if (val === "auto") { return val; } // It's super common to see colors prefixed with a pound, but technically invalid here. // Most clients work with it, but strip it off anyway for strict compliance. const color = val.charAt(0) === "#" ? val.substring(1) : val; return hexBinary(color, 3); }; // // // export const signedTwipsMeasureValue = (val: string | number): string | number => typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val); // // // export const hpsMeasureValue = (val: string | number): string | number => typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val); // // // export const signedHpsMeasureValue = (val: string | number): string | number => typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val); // // // export const twipsMeasureValue = (val: string | number): string | number => typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val); // // // // // export const percentageValue = (val: string): string => { if (val.slice(-1) !== "%") { throw new Error(`Invalid value '${val}'. Expected percentage value (eg '55%')`); } const percent = val.substring(0, val.length - 1); if (isNaN(Number(percent))) { throw new Error(`Invalid value '${percent}' specified. Expected a valid number.`); } return `${Number(percent)}%`; }; // // // // // // // // // export const measurementOrPercentValue = (val: number | string): number | string => { if (typeof val === "number") { return decimalNumber(val); } if (val.slice(-1) === "%") { return percentageValue(val); } return universalMeasureValue(val); }; // // // export const eighthPointMeasureValue = unsignedDecimalNumber; // // // export const pointMeasureValue = unsignedDecimalNumber; // // // // // http://www.datypic.com/sc/xsd/t-xsd_dateTime.html // The type xsd:dateTime represents a specific date and time in the format /* cspell:disable-next-line */ // CCYY-MM-DDThh:mm:ss.sss, which is a concatenation of the date and time forms, // separated by a literal letter "T". All of the same rules that apply to the date // and time types are applicable to xsd:dateTime as well. // // An optional time zone expression may be added at the end of the value. // The letter Z is used to indicate Coordinated Universal Time (UTC). All other time // zones are represented by their difference from Coordinated Universal Time in the // format +hh:mm, or -hh:mm. These values may range from -14:00 to 14:00. For example, // US Eastern Standard Time, which is five hours behind UTC, is represented as -05:00. // If no time zone value is present, it is considered unknown; it is not assumed to be UTC. // // Luckily, js has this format built in already. See: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString export const dateTimeValue = (val: Date): string => val.toISOString();