* 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)
| 269 | * @template T |
| 270 | */ |
| 271 | function 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 |
no test coverage detected