MCPcopy Create free account
hub / github.com/ampproject/amphtml / assert

Function assert

third_party/subscriptions-project/swg-gaa.js:271–297  ·  view source on GitHub ↗

* Throws an error if the first argument isn't trueish. * * Supports argument substitution into the message via %s placeholders. * * Throws an error object that has two extra properties: * - associatedElement: This is the first element provided in the var args. * It can be used for improved d

(shouldBeTrueish, message, var_args)

Source from the content-addressed store, hash-verified

269 * @template T
270 */
271function assert(shouldBeTrueish, message, var_args) {
272 let firstElement;
273 if (!shouldBeTrueish) {
274 message = message || 'Assertion failed';
275 const splitMessage = message.split('%s');
276 const first = splitMessage.shift();
277 let formatted = first;
278 const messageArray = [];
279 pushIfNonEmpty(messageArray, first);
280 for (let i = 2; i < arguments.length; i++) {
281 const val = arguments[i];
282 if (val && val.tagName) {
283 firstElement = val;
284 }
285 const nextConstant = splitMessage.shift();
286 messageArray.push(val);
287 pushIfNonEmpty(messageArray, nextConstant.trim());
288 formatted += toString(val) + nextConstant;
289 }
290 const e = new Error(formatted);
291 e.fromAssert = true;
292 e.associatedElement = firstElement;
293 e.messageArray = messageArray;
294 throw e;
295 }
296 return shouldBeTrueish;
297}
298
299/**
300 * @param {!Array} array

Callers 2

stringToBytesFunction · 0.70
createElementFunction · 0.70

Calls 3

pushIfNonEmptyFunction · 0.70
toStringFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected