refactor: replace ignored numeric literals with constants
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { Injectable, ServiceUnavailableException } from '@nestjs/common';
|
||||
import { constants as httpStatus } from 'node:http2';
|
||||
|
||||
const MIN_PRICE_PERCENT_EXCLUSIVE = 0;
|
||||
const FIRST_PAGE_OFFSET = 0;
|
||||
const EXPECTED_CURSOR_ROWS = 1;
|
||||
const EMPTY_PAGE_SIZE = 0;
|
||||
const MIN_CURSOR_VALUE = 0;
|
||||
|
||||
const FALLBACK_PAGE_SIZE = 100;
|
||||
|
||||
const baseUrl = 'https://iss.moex.com/iss';
|
||||
@@ -52,7 +58,7 @@ export class MoexClient {
|
||||
if (
|
||||
value === null ||
|
||||
pricePercent === null ||
|
||||
Number(pricePercent) <= 0 ||
|
||||
Number(pricePercent) <= MIN_PRICE_PERCENT_EXCLUSIVE ||
|
||||
!date ||
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(date) ||
|
||||
!Number.isFinite(Date.parse(date)) ||
|
||||
@@ -114,7 +120,7 @@ export class MoexClient {
|
||||
paginated = true,
|
||||
): Promise<Record<string, unknown>[]> {
|
||||
const rows: Record<string, unknown>[] = [];
|
||||
let start = 0;
|
||||
let start = FIRST_PAGE_OFFSET;
|
||||
for (;;) {
|
||||
const query = new URLSearchParams({
|
||||
'iss.meta': 'off',
|
||||
@@ -139,11 +145,11 @@ export class MoexClient {
|
||||
const total = integer(cursor?.total);
|
||||
const pageSize = integer(cursor?.pagesize);
|
||||
if (
|
||||
cursors.length !== 1 ||
|
||||
cursors.length !== EXPECTED_CURSOR_ROWS ||
|
||||
index !== start ||
|
||||
total === null ||
|
||||
pageSize === null ||
|
||||
pageSize === 0 ||
|
||||
pageSize === EMPTY_PAGE_SIZE ||
|
||||
current.length !== Math.min(pageSize, total - start)
|
||||
) {
|
||||
throw new ServiceUnavailableException({ code: 'MOEX_UNAVAILABLE' });
|
||||
@@ -207,5 +213,5 @@ function currencyText(value: unknown): null | string {
|
||||
|
||||
function integer(value: unknown): null | number {
|
||||
const result = Number(numberText(value) ?? NaN);
|
||||
return Number.isSafeInteger(result) && result >= 0 ? result : null;
|
||||
return Number.isSafeInteger(result) && result >= MIN_CURSOR_VALUE ? result : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user