Cleanup normalizeGroupCallTimestamp non-number handling
This commit is contained in:
parent
168397d8ef
commit
c70a8955f9
2 changed files with 5 additions and 69 deletions
|
@ -6,53 +6,13 @@ import { assert } from 'chai';
|
||||||
import { normalizeGroupCallTimestamp } from '../../../util/ringrtc/normalizeGroupCallTimestamp';
|
import { normalizeGroupCallTimestamp } from '../../../util/ringrtc/normalizeGroupCallTimestamp';
|
||||||
|
|
||||||
describe('normalizeGroupCallTimestamp', () => {
|
describe('normalizeGroupCallTimestamp', () => {
|
||||||
it('returns undefined if passed NaN', () => {
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(NaN));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns undefined if passed 0', () => {
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(0));
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(-0));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns undefined if passed a negative number', () => {
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(-1));
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(-123));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns undefined if passed a string that cannot be parsed as a number', () => {
|
it('returns undefined if passed a string that cannot be parsed as a number', () => {
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(''));
|
assert.isUndefined(normalizeGroupCallTimestamp(''));
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp('uhhh'));
|
assert.isUndefined(normalizeGroupCallTimestamp('uhhh'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns undefined if passed a BigInt of 0', () => {
|
it('returns undefined if passed 0', () => {
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(BigInt(0)));
|
assert.isUndefined(normalizeGroupCallTimestamp('0'));
|
||||||
});
|
|
||||||
|
|
||||||
it('returns undefined if passed a negative BigInt', () => {
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(BigInt(-1)));
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(BigInt(-123)));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns undefined if passed a non-parseable type', () => {
|
|
||||||
[
|
|
||||||
undefined,
|
|
||||||
null,
|
|
||||||
{},
|
|
||||||
[],
|
|
||||||
[123],
|
|
||||||
Symbol('123'),
|
|
||||||
{ [Symbol.toPrimitive]: () => 123 },
|
|
||||||
// eslint-disable-next-line no-new-wrappers
|
|
||||||
new Number(123),
|
|
||||||
].forEach(value => {
|
|
||||||
assert.isUndefined(normalizeGroupCallTimestamp(value));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns positive numbers passed in', () => {
|
|
||||||
assert.strictEqual(normalizeGroupCallTimestamp(1), 1);
|
|
||||||
assert.strictEqual(normalizeGroupCallTimestamp(123), 123);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses strings as numbers', () => {
|
it('parses strings as numbers', () => {
|
||||||
|
@ -66,9 +26,4 @@ describe('normalizeGroupCallTimestamp', () => {
|
||||||
123456789012345
|
123456789012345
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('converts positive BigInts to numbers', () => {
|
|
||||||
assert.strictEqual(normalizeGroupCallTimestamp(BigInt(1)), 1);
|
|
||||||
assert.strictEqual(normalizeGroupCallTimestamp(BigInt(123)), 123);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,31 +2,12 @@
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes group call timestamps (`addedTime` and `speakerTime`) into numbers. We
|
* Normalizes RingRTC group call timestamps (`addedTime` and `speakerTime`) into numbers.
|
||||||
* expect RingRTC to send a string, but it sends a malformed number as of this writing,
|
|
||||||
* RingRTC 2.8.3.
|
|
||||||
*
|
|
||||||
* We could probably safely do `Number(fromRingRtc)` and be done, but this is extra-
|
|
||||||
* careful.
|
|
||||||
*/
|
*/
|
||||||
export function normalizeGroupCallTimestamp(
|
export function normalizeGroupCallTimestamp(
|
||||||
fromRingRtc: unknown
|
fromRingRtc: string
|
||||||
): undefined | number {
|
): undefined | number {
|
||||||
let asNumber: number;
|
const asNumber = parseInt(fromRingRtc.slice(0, 15), 10);
|
||||||
|
|
||||||
switch (typeof fromRingRtc) {
|
|
||||||
case 'number':
|
|
||||||
asNumber = fromRingRtc;
|
|
||||||
break;
|
|
||||||
case 'string':
|
|
||||||
asNumber = parseInt(fromRingRtc.slice(0, 15), 10);
|
|
||||||
break;
|
|
||||||
case 'bigint':
|
|
||||||
asNumber = Number(fromRingRtc);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Number.isNaN(asNumber) || asNumber <= 0) {
|
if (Number.isNaN(asNumber) || asNumber <= 0) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
Loading…
Reference in a new issue