From dab29a85d35e2225eb3ee5eb28486ff4e2905341 Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Wed, 8 Mar 2023 10:01:50 +1100 Subject: [PATCH 1/3] feat: update adds improved jsdoc based message --- src/tiny-invariant.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/tiny-invariant.ts b/src/tiny-invariant.ts index 6c82392..ed53ba8 100644 --- a/src/tiny-invariant.ts +++ b/src/tiny-invariant.ts @@ -1,9 +1,20 @@ const isProduction: boolean = process.env.NODE_ENV === 'production'; const prefix: string = 'Invariant failed'; -// Throw an error if the condition fails -// Strip out error messages for production -// > Not providing an inline default argument for message as the result is smaller +/** + * Throw an error if the condition fails. Strip out error messages for production. + * + * @param condition A boolean condition - if falsey will thrown an error. + * @param message The message provided to accompany the invariant. No inline default argument for message as the result is smaller. + * + * @example + * ```tsx + * import invariant from 'tiny-invariant'; + * + * // throws when 1 no longer equals 1 + * invariant(1 === 1, 'Maths is broken'); + * ``` + */ export default function invariant( condition: any, // Can provide a string, or a function that returns a string for cases where From deb8d6e7986630bdb099405327dfbb2400270fcb Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Sun, 3 Dec 2023 14:03:14 +1100 Subject: [PATCH 2/3] chore: improve / amend type and jsdoc information --- src/tiny-invariant.flow.js | 17 +++++++++++++++++ src/tiny-invariant.ts | 13 +++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/tiny-invariant.flow.js b/src/tiny-invariant.flow.js index 0f534cc..a8175c8 100644 --- a/src/tiny-invariant.flow.js +++ b/src/tiny-invariant.flow.js @@ -4,6 +4,23 @@ const prefix: string = 'Invariant failed'; +/** + * __invariant__ + * + * The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw. + * + * @param {boolean} condition A boolean condition - if falsey will thrown an error. + * @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute. + * + * @returns {asserts condition is true} + * @example + * ```tsx + * import invariant from 'tiny-invariant'; + * + * // throws when 1 no longer equals 1 + * invariant(1 === 1, 'Maths is broken'); + * ``` + */ export default function invariant(condition: mixed, message?: string | (() => string)) { if (condition) { return; diff --git a/src/tiny-invariant.ts b/src/tiny-invariant.ts index ed53ba8..e047e16 100644 --- a/src/tiny-invariant.ts +++ b/src/tiny-invariant.ts @@ -2,11 +2,14 @@ const isProduction: boolean = process.env.NODE_ENV === 'production'; const prefix: string = 'Invariant failed'; /** - * Throw an error if the condition fails. Strip out error messages for production. - * - * @param condition A boolean condition - if falsey will thrown an error. - * @param message The message provided to accompany the invariant. No inline default argument for message as the result is smaller. + * __invariant__ + * + * The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw. * + * @param {boolean} condition A boolean condition - if falsey will thrown an error. + * @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute. + * + * @returns {asserts condition is true} * @example * ```tsx * import invariant from 'tiny-invariant'; @@ -17,8 +20,6 @@ const prefix: string = 'Invariant failed'; */ export default function invariant( condition: any, - // Can provide a string, or a function that returns a string for cases where - // the message takes a fair amount of effort to compute message?: string | (() => string), ): asserts condition { if (condition) { From 39666f267a483de364bbb3e73049821ab1ed9bfb Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Wed, 14 Feb 2024 14:57:49 +1100 Subject: [PATCH 3/3] docs: typos, and explicit descriptions --- src/tiny-invariant.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tiny-invariant.ts b/src/tiny-invariant.ts index e047e16..6c42275 100644 --- a/src/tiny-invariant.ts +++ b/src/tiny-invariant.ts @@ -6,10 +6,10 @@ const prefix: string = 'Invariant failed'; * * The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw. * - * @param {boolean} condition A boolean condition - if falsey will thrown an error. + * @param {*} condition A boolean condition - if falsy will throw an error. * @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute. * - * @returns {asserts condition is true} + * @returns {asserts condition is true} Asserts condition is truthy. * @example * ```tsx * import invariant from 'tiny-invariant';