Skip to content

Commit

Permalink
chore(components): update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alizedebray committed Feb 24, 2023
1 parent f78e4e5 commit 7c7b236
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function checkContainsOnly<T = unknown>(array: T[], test: (item: T) => boolean, errorMessage: string) {
if (array.length && !array.every(test)) throw new Error(errorMessage);
export function checkContainsOnly<T = unknown>(array: T[], test: (item: T) => boolean, error: string) {
if (array.length && !array.every(test)) throw new Error(error);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function checkExists<T = unknown>(value: T, errorMessage: string) {
export function checkExists<T = unknown>(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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export type LengthCondition =
| {max: number}
| {min: number, max: number};

export function checkLength<T = unknown>(array: T[], condition: LengthCondition, errorMessage: string) {
export function checkLength<T = unknown>(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);
}
}
2 changes: 1 addition & 1 deletion packages/components/src/utils/property-checkers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand All @@ -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};
});

Expand All @@ -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};
});

Expand All @@ -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};
});

Expand All @@ -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);
});
});
});
Expand Down

0 comments on commit 7c7b236

Please sign in to comment.