( sentinel, shouldBeTruthy, opt_message = 'Assertion failed', var_args )
| 33 | * @throws {Error} when shouldBeTruthy is not truthy. |
| 34 | */ |
| 35 | export function assert( |
| 36 | sentinel, |
| 37 | shouldBeTruthy, |
| 38 | opt_message = 'Assertion failed', |
| 39 | var_args |
| 40 | ) { |
| 41 | if (shouldBeTruthy) { |
| 42 | return /** @type {void} */ (shouldBeTruthy); |
| 43 | } |
| 44 | |
| 45 | // Include the sentinel string if provided and not already present |
| 46 | if (sentinel && opt_message.indexOf(sentinel) == -1) { |
| 47 | opt_message += sentinel; |
| 48 | } |
| 49 | |
| 50 | // Skip the first 3 arguments to isolate format params |
| 51 | // const messageArgs = Array.prototype.slice.call(arguments, 3); |
| 52 | // Index at which message args start |
| 53 | let i = 3; |
| 54 | |
| 55 | // Substitute provided values into format string in message |
| 56 | const splitMessage = opt_message.split('%s'); |
| 57 | let message = splitMessage.shift(); |
| 58 | const messageArray = [message]; |
| 59 | |
| 60 | while (splitMessage.length) { |
| 61 | const subValue = arguments[i++]; |
| 62 | const nextConstant = /** @type {NonNullable<*>} */ (splitMessage.shift()); |
| 63 | |
| 64 | message += elementStringOrPassThru(subValue) + nextConstant; |
| 65 | messageArray.push(subValue, nextConstant.trim()); |
| 66 | } |
| 67 | |
| 68 | const error = new Error(message); |
| 69 | error.messageArray = remove(messageArray, (x) => x !== ''); |
| 70 | // __AMP_REPORT_ERROR is installed globally per window in the entry point in |
| 71 | // AMP documents. It may not be present for Bento/Preact elements on non-AMP |
| 72 | // pages. |
| 73 | self.__AMP_REPORT_ERROR?.(error); |
| 74 | throw error; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Asserts types, backbone of `assertNumber`, `assertString`, etc. |
no test coverage detected