narrow-state: Return undefined for a channel ID that would be NaN.

When the channel operand would return NaN for the ID value, we now
return undefined, so that there is only one invalid value being
returned by narrow_state.stream_id.
This commit is contained in:
Lauryn Menard 2025-03-05 17:03:32 +01:00 committed by Tim Abbott
parent edc9853a3e
commit 61fa5ccbf1
2 changed files with 13 additions and 1 deletions

View File

@ -130,7 +130,10 @@ export let stream_id = (current_filter: Filter | undefined = filter()): number |
}
const stream_operands = current_filter.operands("channel");
if (stream_operands.length === 1 && stream_operands[0] !== undefined) {
return Number.parseInt(stream_operands[0], 10);
const id = Number.parseInt(stream_operands[0], 10);
if (!Number.isNaN(id)) {
return id;
}
}
return undefined;
};

View File

@ -40,6 +40,15 @@ test("stream", () => {
assert.ok(!narrow_state.filter());
assert.equal(narrow_state.stream_id(), undefined);
// hash_util.decode_operand returns an empty string when
// stream_data.slug_to_stream_id returns undefined, e.g., the
// stream name in the URL no longer exists or is inaccessible.
set_filter([["channel", ""]]);
assert.ok(narrow_state.filter());
assert.equal(narrow_state.stream_name(), undefined);
assert.equal(narrow_state.stream_id(), undefined);
assert.equal(narrow_state.stream_sub(), undefined);
const test_stream_id = 15;
const test_stream = {name: "Test", stream_id: test_stream_id};
stream_data.add_sub(test_stream);