Skip to content

Commit

Permalink
adds new posnumber type and tests - todo add throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
biomadeira committed Nov 5, 2024
1 parent e4586a0 commit f7611e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/custom-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ export interface CoordsValues {
startSubjPixels?: number;
endSubjPixels?: number;
}

export type posnumber = number & { readonly _positive: unique symbol };

export function toPositiveNumber(value: number): posnumber {
if (value < 0) {
throw new Error(`${value} is not a positive number`);
}
return value as posnumber;
}
23 changes: 23 additions & 0 deletions tests/custom-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
textDefaults,
rectDefaults,
lineDefaults,
toPositiveNumber,
posnumber,
} from '../src/custom-types';

// Test suite for ColorSchemeEnum
Expand Down Expand Up @@ -112,3 +114,24 @@ describe('CoordsValues Interface', () => {
expect(coords.end).toBe(90);
});
});

describe('toPositiveNumber', () => {
it('should return a posnumber for a positive input', () => {
const value = 5;
const result: posnumber = toPositiveNumber(value);
expect(result).toBe(value); // Check if the returned value is the input itself
expect(typeof result).toBe('number'); // Ensure the type is still a number
});

it('should return a posnumber for zero', () => {
const value = 0;
const result: posnumber = toPositiveNumber(value);
expect(result).toBe(value); // Check if the returned value is zero
expect(typeof result).toBe('number'); // Ensure the type is still a number
});

it('should throw an error for a negative input', () => {
const value = -3;
expect(() => toPositiveNumber(value)).toThrow(`${value} is not a positive number`);
});
});

0 comments on commit f7611e4

Please sign in to comment.