Skip to content

Instantly share code, notes, and snippets.

@nthung2112
Created March 30, 2022 17:31
Show Gist options
  • Save nthung2112/8317047dece1619b687eac90033f469f to your computer and use it in GitHub Desktop.
Save nthung2112/8317047dece1619b687eac90033f469f to your computer and use it in GitHub Desktop.
Invariant ES6
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