Skip to content

Commit

Permalink
feat: assertion refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
wusteven815 committed Feb 5, 2024
1 parent a47b9ff commit 15bca84
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import type { DebouncedFunc } from 'lodash';
import {
TextUtils,
assertNotEmpty,
assertNotNaN,
assertNotNull,
copyToClipboard,
} from '@deephaven/utils';
Expand Down Expand Up @@ -550,14 +551,11 @@ class IrisGridContextMenuHandler extends GridMouseHandler {
while (rowCount > MAX_MULTISELECT_ROWS) {
const lastRow = selectedRanges.pop();
// should never occur, sanity check
if (lastRow === undefined) {
throw Error('Selected ranges should not be empty');
}
assertNotNull(lastRow, 'Selected ranges should not be empty');

const lastRowSize = GridRange.rowCount([lastRow]);
// should never occur, sanity check
if (Number.isNaN(lastRowSize)) {
throw Error('Selected ranges should not be unbounded');
}
assertNotNaN(lastRowSize, 'Selected ranges should not be unbounded');

// if removing the last rows makes it dip below the max, then need to
// bring it back but truncated
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/src/Asserts.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
assertNever,
assertNotEmpty,
assertNotNaN,
assertNotNull,
getOrThrow,
} from './Asserts';
Expand Down Expand Up @@ -33,6 +34,11 @@ describe('assertNotEmpty', () => {
expect(() => assertNotEmpty([1, 2])).not.toThrowError('Size of value is 0');
});

describe('assertNotNaN', () => {
expect(() => assertNotNaN(NaN)).toThrowError('Value is NaN');
expect(() => assertNotNaN(0)).not.toThrowError('Value is NaN');
});

describe('getOrThrow', () => {
const MAP = new Map([
[5, 10],
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/Asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function assertNotEmpty(
}
}

export function assertNotNaN(value: number, message = 'Value is NaN'): void {
if (Number.isNaN(value)) throw new Error(message);
}

/**
* Retrieve a value from a map. If the value is not found and no default value is provided, throw.
* Use when the value _must_ be present
Expand Down

0 comments on commit 15bca84

Please sign in to comment.