Skip to content

Commit 762b583

Browse files
committed
feat: move parseNonNegativeInt and parsePositiveInt functions to wsConnector for better organization
1 parent 0936dc0 commit 762b583

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

nodejs/src/client/wsConnector.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import { OnMessageType, WsEventCallback } from "./wsEventCallback";
1010
import logger from "../common/log";
1111
import {
1212
maskSensitiveForLog,
13-
maskUrlForLog,
14-
parseNonNegativeInt,
15-
parsePositiveInt
13+
maskUrlForLog
1614
} from "../common/utils";
1715

1816
interface InflightRequest {
@@ -94,6 +92,29 @@ const NETWORK_ERROR_MESSAGE_PATTERNS = [
9492
"connection closed",
9593
];
9694

95+
function parseNonNegativeInt(value: string | undefined | null, fallback: number): number {
96+
return parseIntWithValidation(value, fallback, (parsed) => parsed >= 0);
97+
}
98+
99+
function parsePositiveInt(value: string | undefined | null, fallback: number): number {
100+
return parseIntWithValidation(value, fallback, (parsed) => parsed > 0);
101+
}
102+
103+
function parseIntWithValidation(
104+
value: string | undefined | null,
105+
fallback: number,
106+
isValid: (parsed: number) => boolean
107+
): number {
108+
if (value === undefined || value === null || value.length === 0) {
109+
return fallback;
110+
}
111+
const parsed = Number.parseInt(value, 10);
112+
if (Number.isNaN(parsed) || !isValid(parsed)) {
113+
return fallback;
114+
}
115+
return parsed;
116+
}
117+
97118
export class RetryConfig {
98119
readonly retries: number;
99120
readonly retryBackoffMs: number;

nodejs/src/common/utils.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -241,25 +241,3 @@ export function maskTmqConfigForLog(config: TmqConfig): string {
241241
});
242242
}
243243

244-
export function parseNonNegativeInt(value: string | undefined | null, fallback: number): number {
245-
return parseInt(value, fallback, (parsed) => parsed >= 0);
246-
}
247-
248-
export function parsePositiveInt(value: string | undefined | null, fallback: number): number {
249-
return parseInt(value, fallback, (parsed) => parsed > 0);
250-
}
251-
252-
function parseInt(
253-
value: string | undefined | null,
254-
fallback: number,
255-
isValid: (parsed: number) => boolean
256-
): number {
257-
if (value === undefined || value === null || value.length === 0) {
258-
return fallback;
259-
}
260-
const parsed = Number.parseInt(value, 10);
261-
if (Number.isNaN(parsed) || !isValid(parsed)) {
262-
return fallback;
263-
}
264-
return parsed;
265-
}

0 commit comments

Comments
 (0)