* 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)
| 2974 | * @template T |
| 2975 | */ |
| 2976 | function assert(shouldBeTrueish, message, var_args) { |
| 2977 | let firstElement; |
| 2978 | if (!shouldBeTrueish) { |
| 2979 | message = message || 'Assertion failed'; |
| 2980 | const splitMessage = message.split('%s'); |
| 2981 | const first = splitMessage.shift(); |
| 2982 | let formatted = first; |
| 2983 | const messageArray = []; |
| 2984 | pushIfNonEmpty(messageArray, first); |
| 2985 | for (let i = 2; i < arguments.length; i++) { |
| 2986 | const val = arguments[i]; |
| 2987 | if (val && val.tagName) { |
| 2988 | firstElement = val; |
| 2989 | } |
| 2990 | const nextConstant = splitMessage.shift(); |
| 2991 | messageArray.push(val); |
| 2992 | pushIfNonEmpty(messageArray, nextConstant.trim()); |
| 2993 | formatted += toString(val) + nextConstant; |
| 2994 | } |
| 2995 | const e = new Error(formatted); |
| 2996 | e.fromAssert = true; |
| 2997 | e.associatedElement = firstElement; |
| 2998 | e.messageArray = messageArray; |
| 2999 | throw e; |
| 3000 | } |
| 3001 | return shouldBeTrueish; |
| 3002 | } |
| 3003 | |
| 3004 | /** |
| 3005 | * @param {!Array} array |
no test coverage detected