-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
assert.js
37 lines (33 loc) · 1.02 KB
/
assert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
/**
* Verify that a certain condition is met, or throw an error if otherwise.
*
* This is useful for communicating expectations in the code to other human
* readers as well as catching bugs that accidentally violate these expectations.
*
* ```js
* const { assert } = require('ember-cli/lib/debug');
*
* // Test for truthiness:
* assert('Must pass a string.', typeof str === 'string');
*
* // Fail unconditionally:
* assert('This code path should never run.');
* ```
*
* @method assert
* @param {String} description Describes the condition.
* This will become the message of the error thrown if the assertion fails.
* @param {Any} condition Must be truthy for the assertion to pass.
* If falsy, an error will be thrown.
*/
function assert(description, condition) {
if (!description) {
throw new Error('When calling `assert`, you must provide a description as the first argument.');
}
if (condition) {
return;
}
throw new Error(`ASSERTION FAILED: ${description}`);
}
module.exports = assert;