Comparing version 1.3.0 to 1.4.0
@@ -10,3 +10,2 @@ 'use strict'; | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
@@ -19,8 +18,4 @@ // NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
debug: true | ||
}; | ||
if (!condition) { | ||
developmentDebugger(options.debug); | ||
throw new TypeError(message); | ||
@@ -33,21 +28,31 @@ } | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
debug: true | ||
}; | ||
developmentDebugger(options.debug); | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
/** | ||
* Similar to `assert` but only logs a warning if the condition is not met. Only | ||
* logs in development. | ||
*/ | ||
function developmentDebugger(enabled) { | ||
if (!enabled || process.env.NODE_ENV === 'production') { | ||
return; | ||
} // eslint-disable-next-line no-debugger | ||
function warning(condition, message) { | ||
if (!(process.env.NODE_ENV === 'production')) { | ||
if (condition) { | ||
return; | ||
} // follow message prefix convention | ||
debugger; | ||
var text = "Warning: ".concat(message); // IE9 support, console only with open devtools | ||
if (typeof console !== 'undefined') { | ||
console.warn(text); | ||
} // NOTE: throw and catch immediately to provide a stack trace: | ||
// https://developer.chrome.com/blog/automatically-pause-on-any-exception/ | ||
try { | ||
throw Error(text); // eslint-disable-next-line no-empty | ||
} catch (x) {} | ||
} | ||
} | ||
@@ -57,1 +62,2 @@ | ||
exports.assertNever = assertNever; | ||
exports.warning = warning; |
@@ -10,3 +10,2 @@ 'use strict'; | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
@@ -19,8 +18,4 @@ // NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
debug: true | ||
}; | ||
if (!condition) { | ||
developmentDebugger(options.debug); | ||
throw new TypeError(message); | ||
@@ -33,21 +28,13 @@ } | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
debug: true | ||
}; | ||
developmentDebugger(options.debug); | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
/** | ||
* Similar to `assert` but only logs a warning if the condition is not met. Only | ||
* logs in development. | ||
*/ | ||
function developmentDebugger(enabled) { | ||
if (!enabled || "production" === 'production') { | ||
return; | ||
} // eslint-disable-next-line no-debugger | ||
debugger; | ||
function warning(condition, message) { | ||
} | ||
@@ -57,1 +44,2 @@ | ||
exports.assertNever = assertNever; | ||
exports.warning = warning; |
@@ -6,3 +6,2 @@ /** | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
@@ -15,8 +14,4 @@ // NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Assert failed'; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { | ||
debug: true | ||
}; | ||
if (!condition) { | ||
developmentDebugger(options.debug); | ||
throw new TypeError(message); | ||
@@ -29,23 +24,33 @@ } | ||
* @throws always | ||
* @returns void | ||
*/ | ||
function assertNever(condition) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
debug: true | ||
}; | ||
developmentDebugger(options.debug); | ||
throw new Error("Unexpected call to assertNever: '".concat(condition, "'")); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
/** | ||
* Similar to `assert` but only logs a warning if the condition is not met. Only | ||
* logs in development. | ||
*/ | ||
function developmentDebugger(enabled) { | ||
if (!enabled || process.env.NODE_ENV === 'production') { | ||
return; | ||
} // eslint-disable-next-line no-debugger | ||
function warning(condition, message) { | ||
if (!(process.env.NODE_ENV === 'production')) { | ||
if (condition) { | ||
return; | ||
} // follow message prefix convention | ||
debugger; | ||
var text = "Warning: ".concat(message); // IE9 support, console only with open devtools | ||
if (typeof console !== 'undefined') { | ||
console.warn(text); | ||
} // NOTE: throw and catch immediately to provide a stack trace: | ||
// https://developer.chrome.com/blog/automatically-pause-on-any-exception/ | ||
try { | ||
throw Error(text); // eslint-disable-next-line no-empty | ||
} catch (x) {} | ||
} | ||
} | ||
export { assert, assertNever }; | ||
export { assert, assertNever, warning }; |
@@ -6,7 +6,4 @@ /** | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
export declare function assert(condition: boolean, message?: string, options?: { | ||
debug: boolean; | ||
}): asserts condition; | ||
export declare function assert(condition: boolean, message?: string): asserts condition; | ||
/** | ||
@@ -16,7 +13,9 @@ * Asserts that allegedly unreachable code has been executed. | ||
* @throws always | ||
* @returns void | ||
*/ | ||
export declare function assertNever(condition: never, options?: { | ||
debug: boolean; | ||
}): never; | ||
export declare function assertNever(condition: never): never; | ||
/** | ||
* Similar to `assert` but only logs a warning if the condition is not met. Only | ||
* logs in development. | ||
*/ | ||
export declare function warning(condition: boolean, message: string): void; | ||
//# sourceMappingURL=assertions.d.ts.map |
{ | ||
"name": "emery", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Utilities to help polish the rough parts of TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "dist/emery.cjs.js", |
@@ -1,3 +0,4 @@ | ||
import { assert, assertNever } from './assertions'; | ||
import { assert, assertNever, warning } from './assertions'; | ||
import { getErrorMessage } from './utils/error'; | ||
import { falsyValues, truthyValues } from './testing'; | ||
@@ -14,5 +15,2 @@ describe('assertions', () => { | ||
it('should expect TS error when called with non-boolean conditions', () => { | ||
const falsyValues = [0, -0, '', null, undefined, NaN]; | ||
const truthyValues = [1, -1, 'test', {}, [], Number.POSITIVE_INFINITY]; | ||
falsyValues.forEach(val => { | ||
@@ -66,2 +64,26 @@ // @ts-expect-error should not accept non-boolean conditions | ||
}); | ||
describe('warning', () => { | ||
beforeEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
}); | ||
afterEach(() => { | ||
// @ts-expect-error: mocked | ||
console.warn.mockRestore(); | ||
}); | ||
it('should not warn if the condition is true', () => { | ||
warning(true, 'test message'); | ||
expect(console.warn).not.toHaveBeenCalled(); | ||
}); | ||
it('should warn if the condition is false', () => { | ||
const message = 'test message'; | ||
warning(false, message); | ||
expect(console.warn).toHaveBeenCalledWith('Warning: ' + message); | ||
// @ts-expect-error: mocked | ||
console.warn.mockClear(); | ||
}); | ||
}); | ||
}); |
@@ -6,3 +6,2 @@ /** | ||
* @throws when the condition is `false` | ||
* @returns void | ||
*/ | ||
@@ -13,9 +12,4 @@ // NOTE: The narrow type of `boolean` instead of something like `unknown` is an | ||
// `''`, which can introduce "subtle" bugs. | ||
export function assert( | ||
condition: boolean, | ||
message = 'Assert failed', | ||
options = { debug: true }, | ||
): asserts condition { | ||
export function assert(condition: boolean, message = 'Assert failed'): asserts condition { | ||
if (!condition) { | ||
developmentDebugger(options.debug); | ||
throw new TypeError(message); | ||
@@ -29,17 +23,32 @@ } | ||
* @throws always | ||
* @returns void | ||
*/ | ||
export function assertNever(condition: never, options = { debug: true }): never { | ||
developmentDebugger(options.debug); | ||
export function assertNever(condition: never): never { | ||
throw new Error(`Unexpected call to assertNever: '${condition}'`); | ||
} | ||
/** Pause execution in development to aid debugging. */ | ||
function developmentDebugger(enabled?: boolean): void { | ||
if (!enabled || process.env.NODE_ENV === 'production') { | ||
return; | ||
/** | ||
* Similar to `assert` but only logs a warning if the condition is not met. Only | ||
* logs in development. | ||
*/ | ||
export function warning(condition: boolean, message: string) { | ||
if (!(process.env.NODE_ENV === 'production')) { | ||
if (condition) { | ||
return; | ||
} | ||
// follow message prefix convention | ||
const text = `Warning: ${message}`; | ||
// IE9 support, console only with open devtools | ||
if (typeof console !== 'undefined') { | ||
console.warn(text); | ||
} | ||
// NOTE: throw and catch immediately to provide a stack trace: | ||
// https://developer.chrome.com/blog/automatically-pause-on-any-exception/ | ||
try { | ||
throw Error(text); | ||
// eslint-disable-next-line no-empty | ||
} catch (x) {} | ||
} | ||
// eslint-disable-next-line no-debugger | ||
debugger; | ||
} |
@@ -15,4 +15,6 @@ import { typedKeys } from './utils/object'; | ||
// https://developer.mozilla.org/en-US/docs/Glossary/Falsy | ||
export const falsyValues = [false, 0, -0, '', null, undefined, NaN]; | ||
export const truthyValues = [true, 1, -1, 'test', {}, [], Number.POSITIVE_INFINITY]; | ||
// https://developer.mozilla.org/en-US/docs/Glossary/Truthy | ||
export const truthyValues = [true, 1, -1, 'test', {}, []]; | ||
@@ -19,0 +21,0 @@ export function getValuesByType(keyOrKeys: ValueKey | ValueKey[]) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77777
1769