Package Exports
- @kiwicom/js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kiwicom/js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
This package contains some useful utilities to help you write JavaScript better. This package is a great place where to add these small helpers to share them everywhere.
yarn add @kiwicom/jsinvariant, warning
Use these functions instead of traditional error throwing or console.warn. Compare these two examples:
import { invariant } from '@kiwicom/js';
invariant(isWorkspaceDirectory === true, 'This is not a workspace directory.');
// vs:
if (isWorkspaceDirectory !== true) {
throw new Error('This is not a workspace directory.');
}It is a common idiom to use invariant() or invariant(false, ...) to throw in code that should be unreachable. The rules apply to warning (except it doesn't throw but log to stdout instead):
import { warning } from '@kiwicom/js';
warning(isWorkspaceDirectory === true, 'This is not a workspace directory.');
// vs:
if (isWorkspaceDirectory !== true) {
console.warn('This is not a workspace directory.');
}First it's more readable this way - you don't have to use conditions at all. But more importantly we can now work with these errors/warnings a bit better. The idea is to transpile these functions so they do not contain sensitive error messages in production. These functions are therefore perfect fit for production applications. Not so great for NPM packages though because you may want to preserve the error messages.
Please note: this transpilation is currently not implemented yet.
These both functions use sprintf behind the scenes (only %s is supported):
import { sprintf } from '@kiwicom/js';
sprintf('Oh, %s', 'yeah!');