mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-29 04:53:10 +00:00
51 lines
1,009 B
TypeScript
51 lines
1,009 B
TypeScript
/**
|
|
* Assert that the statement is true, otherwise invoke an error with the given message.
|
|
*/
|
|
export function assert(statement: boolean, error: string): void {
|
|
if (!statement) {
|
|
throw new Error(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Make sure that the given value is within the range
|
|
*/
|
|
export function assertRange(value: number, gte: number, lte: number = Infinity): void {
|
|
if (!(gte <= value && value <= lte)) {
|
|
throw new RangeError(`Value must be within [${gte}, ${lte}], got: ${value}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A basic logging interface
|
|
*/
|
|
interface Logger {
|
|
log: (args?: any[]) => void;
|
|
warn: (args?: any[]) => void;
|
|
}
|
|
|
|
/**
|
|
* The default logger is the console
|
|
*/
|
|
let defaultLogger: Logger = console;
|
|
|
|
/**
|
|
* Set the logging interface
|
|
*/
|
|
export function setLogger(logger: Logger): void {
|
|
defaultLogger = logger;
|
|
}
|
|
|
|
/**
|
|
* Log anything
|
|
*/
|
|
export function log(...args: any[]): void {
|
|
defaultLogger.log(...args);
|
|
}
|
|
|
|
/**
|
|
* Warn anything
|
|
*/
|
|
export function warn(...args: any[]): void {
|
|
defaultLogger.warn(...args);
|
|
}
|