Created
March 30, 2022 17:31
-
-
Save nthung2112/8317047dece1619b687eac90033f469f to your computer and use it in GitHub Desktop.
Invariant ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const invariant = (condition, format, ...args) => { | |
if (!condition) { | |
if (!format) { | |
throw new Error('General error occured.'); | |
} | |
let i = 0; | |
throw new Error(format.replace(/%s/g, () => args[i++])); | |
} | |
}; | |
invariant( | |
1 > 2, | |
'Hello from error. %s %s %s %s %s %s', | |
1, 2, 3, 4, 5, 6 | |
); | |
// Results to | |
// > Uncaught Error: Hello from error. 1 2 3 4 5 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment