From 7c7b236e66eae6fef84445e022e2e093c9b3e482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aliz=C3=A9=20Debray?= Date: Fri, 24 Feb 2023 17:13:12 +0100 Subject: [PATCH] chore(components): update utils --- .../property-checkers/check-contains-only.ts | 4 ++-- .../utils/property-checkers/check-exists.ts | 4 ++-- .../utils/property-checkers/check-length.ts | 6 +++--- .../src/utils/property-checkers/index.ts | 2 +- .../tests/check-contains-only.spec.ts | 6 +++--- .../tests/check-exists.spec.ts | 6 +++--- .../tests/check-length.spec.ts | 20 +++++++++---------- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/components/src/utils/property-checkers/check-contains-only.ts b/packages/components/src/utils/property-checkers/check-contains-only.ts index 9ddd2b1be1..eeb8828b9c 100644 --- a/packages/components/src/utils/property-checkers/check-contains-only.ts +++ b/packages/components/src/utils/property-checkers/check-contains-only.ts @@ -1,3 +1,3 @@ -export function checkContainsOnly(array: T[], test: (item: T) => boolean, errorMessage: string) { - if (array.length && !array.every(test)) throw new Error(errorMessage); +export function checkContainsOnly(array: T[], test: (item: T) => boolean, error: string) { + if (array.length && !array.every(test)) throw new Error(error); } diff --git a/packages/components/src/utils/property-checkers/check-exists.ts b/packages/components/src/utils/property-checkers/check-exists.ts index 948ea3b47d..f49016056e 100644 --- a/packages/components/src/utils/property-checkers/check-exists.ts +++ b/packages/components/src/utils/property-checkers/check-exists.ts @@ -1,7 +1,7 @@ -export function checkExists(value: T, errorMessage: string) { +export function checkExists(value: T, error: string) { const valueIsUndefined = typeof value === 'undefined'; const valueIsNull = value === null; const valueIsNaN = typeof value === 'number' && isNaN(value); - if (valueIsUndefined || valueIsNull || valueIsNaN) throw new Error(errorMessage); + if (valueIsUndefined || valueIsNull || valueIsNaN) throw new Error(error); } diff --git a/packages/components/src/utils/property-checkers/check-length.ts b/packages/components/src/utils/property-checkers/check-length.ts index c9c3fd7ba0..b90ca47c19 100644 --- a/packages/components/src/utils/property-checkers/check-length.ts +++ b/packages/components/src/utils/property-checkers/check-length.ts @@ -4,13 +4,13 @@ export type LengthCondition = | {max: number} | {min: number, max: number}; -export function checkLength(array: T[], condition: LengthCondition, errorMessage: string) { +export function checkLength(array: T[], condition: LengthCondition, error: string) { if (typeof condition === 'number') { const expectedLength = condition; - if (array.length !== expectedLength) throw new Error(errorMessage); + if (array.length !== expectedLength) throw new Error(error); } else { const minLength = 'min' in condition ? condition.min : 0; const maxLength = 'max' in condition ? condition.max : Infinity; - if (array.length > maxLength || array.length < minLength) throw new Error(errorMessage); + if (array.length > maxLength || array.length < minLength) throw new Error(error); } } diff --git a/packages/components/src/utils/property-checkers/index.ts b/packages/components/src/utils/property-checkers/index.ts index fb8b005538..ac43482eeb 100644 --- a/packages/components/src/utils/property-checkers/index.ts +++ b/packages/components/src/utils/property-checkers/index.ts @@ -7,7 +7,7 @@ import { checkType } from './check-type'; export const checkEmptyOrOneOf = emptyOr(checkOneOf); export const checkEmptyOrType = emptyOr(checkType); export const checkEmptyOrContainsOnly = emptyOr(checkContainsOnly); -export const checkEmptyOrHasLength = emptyOr(checkLength); +export const checkEmptyOrLength = emptyOr(checkLength); export * from './check-contains-only'; export * from './check-exists'; diff --git a/packages/components/src/utils/property-checkers/tests/check-contains-only.spec.ts b/packages/components/src/utils/property-checkers/tests/check-contains-only.spec.ts index 34ec98c77d..41638ee931 100644 --- a/packages/components/src/utils/property-checkers/tests/check-contains-only.spec.ts +++ b/packages/components/src/utils/property-checkers/tests/check-contains-only.spec.ts @@ -2,9 +2,9 @@ import { checkContainsOnly } from "../check-contains-only"; describe('checkContainsOnly', () => { - const errorMessage = 'Does not only contain.'; const test = jest.fn(); - const runCheckForValue = array => () => checkContainsOnly(array, test, errorMessage); + const error = 'Does not only contain.'; + const runCheckForValue = array => () => checkContainsOnly(array, test, error); it('should not throw an error if all items of the provided array pass the provided test', () => { test.mockReturnValue(true); @@ -13,7 +13,7 @@ describe('checkContainsOnly', () => { it('should throw the provided error if some items of the provided array do not pass the provided test', () => { test.mockReturnValue(false); - expect(runCheckForValue(['mock item'])).toThrow(errorMessage); + expect(runCheckForValue(['mock item'])).toThrow(error); }); it('should not throw an error if the privided array is empty', () => { diff --git a/packages/components/src/utils/property-checkers/tests/check-exists.spec.ts b/packages/components/src/utils/property-checkers/tests/check-exists.spec.ts index 1f3923faf1..792bf9c1c3 100644 --- a/packages/components/src/utils/property-checkers/tests/check-exists.spec.ts +++ b/packages/components/src/utils/property-checkers/tests/check-exists.spec.ts @@ -2,8 +2,8 @@ import { checkExists } from "../check-exists"; describe('checkExists', () => { - const errorMessage = 'Does not exist.'; - const runCheckForValue = value => () => checkExists(value, errorMessage); + const error = 'Does not exist.'; + const runCheckForValue = value => () => checkExists(value, error); it('should not throw an error if the provided value in not nullable', () => { [0, '', false, [], {}, () => {/* empty */}].forEach(nonNullable => { @@ -13,7 +13,7 @@ describe('checkExists', () => { it('should throw the provided error if the provided value is nullable', () => { [undefined, null, NaN].forEach(nullable => { - expect(runCheckForValue(nullable)).toThrow(errorMessage); + expect(runCheckForValue(nullable)).toThrow(error); }); }); }); \ No newline at end of file diff --git a/packages/components/src/utils/property-checkers/tests/check-length.spec.ts b/packages/components/src/utils/property-checkers/tests/check-length.spec.ts index 871aeec78b..a9f8af9a47 100644 --- a/packages/components/src/utils/property-checkers/tests/check-length.spec.ts +++ b/packages/components/src/utils/property-checkers/tests/check-length.spec.ts @@ -2,13 +2,13 @@ import { checkLength, LengthCondition } from "../check-length"; describe('checkLength', () => { - let errorMessage: string; let condition: LengthCondition; - const runCheckForValue = array => () => checkLength(array, condition, errorMessage); + let error: string; + const runCheckForValue = array => () => checkLength(array, condition, error); describe('fixed length', () => { beforeAll(() => { - errorMessage = 'Incorrect length.'; + error = 'Incorrect length.'; condition = 1; }); @@ -18,14 +18,14 @@ describe('checkLength', () => { it('should throw the provided error if the provided array does not have the expected length', () => { [[], ['item 1', 'item 2']].forEach(incorrectArray => { - expect(runCheckForValue(incorrectArray)).toThrow(errorMessage); + expect(runCheckForValue(incorrectArray)).toThrow(error); }); }); }); describe('min length', () => { beforeAll(() => { - errorMessage = 'Too short.'; + error = 'Too short.'; condition = {min: 1}; }); @@ -36,13 +36,13 @@ describe('checkLength', () => { }); it('should throw the provided error if the provided array has a length less than the expected one', () => { - expect(runCheckForValue([])).toThrow(errorMessage); + expect(runCheckForValue([])).toThrow(error); }); }); describe('max length', () => { beforeAll(() => { - errorMessage = 'Too long.'; + error = 'Too long.'; condition = {max: 1}; }); @@ -53,13 +53,13 @@ describe('checkLength', () => { }); it('should throw the provided error if the provided array has a length greater than the expected one', () => { - expect(runCheckForValue(['item 1', 'item 2'])).toThrow(errorMessage); + expect(runCheckForValue(['item 1', 'item 2'])).toThrow(error); }); }); describe('min and max length', () => { beforeAll(() => { - errorMessage = 'Too short or too long.'; + error = 'Too short or too long.'; condition = {min: 1, max: 2}; }); @@ -71,7 +71,7 @@ describe('checkLength', () => { it('should throw the provided error if the provided array has a length outside the expected minimum and maximum', () => { [[], ['item 1', 'item 2', 'item 3']].forEach(correctArray => { - expect(runCheckForValue(correctArray)).toThrow(errorMessage); + expect(runCheckForValue(correctArray)).toThrow(error); }); }); });