basant307/AI_Governance_Project
048
1// src/format.ts2var POSITIONALS_EXP = /(%?)(%([sdijo]))/g;3function serializePositional(positional, flag) {4 switch (flag) {5 case "s":6 return positional;7 case "d":8 case "i":9 return Number(positional);10 case "j":11 return JSON.stringify(positional);12 case "o": {13 if (typeof positional === "string") {14 return positional;15 }16 const json = JSON.stringify(positional);17 if (json === "{}" || json === "[]" || /^\[object .+?\]$/.test(json)) {18 return positional;19 }20 return json;21 }22 }23}24function format(message, ...positionals) {25 if (positionals.length === 0) {26 return message;27 }28 let positionalIndex = 0;29 let formattedMessage = message.replace(30 POSITIONALS_EXP,31 (match, isEscaped, _, flag) => {32 const positional = positionals[positionalIndex];33 const value = serializePositional(positional, flag);34 if (!isEscaped) {35 positionalIndex++;36 return value;37 }38 return match;39 }40 );41 if (positionalIndex < positionals.length) {42 formattedMessage += ` ${positionals.slice(positionalIndex).join(" ")}`;43 }44 formattedMessage = formattedMessage.replace(/%{2,2}/g, "%");45 return formattedMessage;46}47 48// src/invariant.ts49var STACK_FRAMES_TO_IGNORE = 2;50function cleanErrorStack(error) {51 if (!error.stack) {52 return;53 }54 const nextStack = error.stack.split("\n");55 nextStack.splice(1, STACK_FRAMES_TO_IGNORE);56 error.stack = nextStack.join("\n");57}58var InvariantError = class extends Error {59 constructor(message, ...positionals) {60 super(message);61 this.message = message;62 this.name = "Invariant Violation";63 this.message = format(message, ...positionals);64 cleanErrorStack(this);65 }66};67var invariant = (predicate, message, ...positionals) => {68 if (!predicate) {69 throw new InvariantError(message, ...positionals);70 }71};72invariant.as = (ErrorConstructor, predicate, message, ...positionals) => {73 if (!predicate) {74 const formatMessage = positionals.length === 0 ? message : format(message, ...positionals);75 let error;76 try {77 error = Reflect.construct(ErrorConstructor, [78 formatMessage79 ]);80 } catch (err) {81 error = ErrorConstructor(formatMessage);82 }83 throw error;84 }85};86export {87 InvariantError,88 format,89 invariant90};91//# sourceMappingURL=index.mjs.map