Skip to content

Commit

Permalink
feat: accept int in isOnlyDigits
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Dec 8, 2023
1 parent 8517970 commit 7d35896
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 11 additions & 7 deletions src/predicates.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { isLabId, isRackBarcode, PredicateFn } from './predicates';
import { isNotNullish } from './typeGuards';
import {
isLabId,
isOnlyDigits,
isRackBarcode,
PredicateFn,
} from './predicates';

export function assertFalse(
predicate: PredicateFn,
Expand All @@ -25,13 +29,13 @@ describe('predicates', () => {
assertFalse(isLabId, ['asdf', '12345678', '123123123123']);
});

describe('isOnlyDigits', () => {
assertTrue(isOnlyDigits, ['123', 123]);
assertFalse(isOnlyDigits, ['12x', 'x12', '1,2', '1.2', 1.2]);
});

describe('isRackBarcode', () => {
assertTrue(isRackBarcode, ['FE12345678']);
assertFalse(isRackBarcode, ['FE123456789', 'fe12345678', '12345678']);
});

describe('isNotNullish', () => {
assertTrue(isNotNullish, ['', [], {}, false]);
assertFalse(isNotNullish, [null, undefined]);
});
});
22 changes: 11 additions & 11 deletions src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ export const isEmail: PredicateFn = function isEmail(value): value is string {
return isString(value) && EMAIL_REGEX.test(value);
};

export const isLabId: PredicateFn = function isLabId(value): value is string {
return isString(value) && /^\d{2}-\d{6}$/.test(value);
};

export const isOnlyDigits: PredicateFn = function isOnlyDigits(
value,
): value is string | number {
return (isString(value) && /^\d+$/.test(value)) || Number.isInteger(value);
};

export const isRackBarcode: PredicateFn = function isRackBarcode(
value,
): value is string {
return isString(value) && /^\d+$/.test(value);
return isString(value) && /^[A-Z]{2}\d{8}$/.test(value);
};

export const isURL: PredicateFn = function isURL(value): value is string {
Expand All @@ -53,13 +63,3 @@ export const isURL: PredicateFn = function isURL(value): value is string {
export const isWord: PredicateFn = function isWord(value): value is string {
return isString(value) && /^[\w-]+$/.test(value);
};

export const isLabId: PredicateFn = function isLabId(value): value is string {
return isString(value) && /^\d{2}-\d{6}$/.test(value);
};

export const isRackBarcode: PredicateFn = function isRackBarcode(
value,
): value is string {
return isString(value) && /^[A-Z]{2}\d{8}$/.test(value);
};

0 comments on commit 7d35896

Please sign in to comment.