From d73d331a873d1f254402d4f00c6e728d4394c1f4 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 7 Nov 2022 12:45:38 +0530 Subject: [PATCH] refactor: use NaN safe Number parsing where applicable --- dummy/index.ts | 6 ++--- fyo/core/converter.ts | 12 +++++----- fyo/utils/format.ts | 6 ++--- fyo/utils/index.ts | 6 ++--- models/baseModels/Invoice/Invoice.ts | 4 ++-- models/helpers.ts | 3 ++- reports/LedgerReport.ts | 7 +++--- src/components/Controls/ExchangeRate.vue | 3 ++- src/components/Controls/Float.vue | 4 ++-- src/components/SearchBar.vue | 3 ++- src/pages/Dashboard/UnpaidInvoices.vue | 3 ++- src/utils/export.ts | 4 ++-- src/utils/search.ts | 3 ++- utils/index.ts | 29 ++++++++++++++++++++++++ 14 files changed, 64 insertions(+), 29 deletions(-) diff --git a/dummy/index.ts b/dummy/index.ts index 987f2bd8..4cde7872 100644 --- a/dummy/index.ts +++ b/dummy/index.ts @@ -8,7 +8,7 @@ import { PurchaseInvoice } from 'models/baseModels/PurchaseInvoice/PurchaseInvoi import { SalesInvoice } from 'models/baseModels/SalesInvoice/SalesInvoice'; import { ModelNameEnum } from 'models/types'; import setupInstance from 'src/setup/setupInstance'; -import { getMapFromList } from 'utils'; +import { getMapFromList, safeParseInt } from 'utils'; import { getFiscalYear } from 'utils/misc'; import { flow, @@ -256,7 +256,7 @@ async function getSalesInvoices( notifier?.( `Creating Sales Invoices, ${d} out of ${dates.length}`, - parseInt(d) / dates.length + safeParseInt(d) / dates.length ); const customer = sample(customers); @@ -531,7 +531,7 @@ async function syncAndSubmit(docs: Doc[], notifier?: Notifier) { const doc = docs[i]; notifier?.( `Syncing ${nameMap[doc.schemaName]}, ${i} out of ${total}`, - parseInt(i) / total + safeParseInt(i) / total ); await doc.sync(); await doc.submit(); diff --git a/fyo/core/converter.ts b/fyo/core/converter.ts index 29c7a25d..e8dab2c4 100644 --- a/fyo/core/converter.ts +++ b/fyo/core/converter.ts @@ -5,7 +5,7 @@ import { ValueError } from 'fyo/utils/errors'; import { DateTime } from 'luxon'; import { Money } from 'pesa'; import { Field, FieldTypeEnum, RawValue, TargetField } from 'schemas/types'; -import { getIsNullOrUndef } from 'utils'; +import { getIsNullOrUndef, safeParseFloat, safeParseInt } from 'utils'; import { DatabaseHandler } from './dbHandler'; import { Attachment, DocValue, DocValueMap, RawValueMap } from './types'; @@ -231,7 +231,7 @@ function toDocInt(value: RawValue, field: Field): number { } if (typeof value === 'string') { - value = parseInt(value); + value = safeParseInt(value); } return toDocFloat(value, field); @@ -247,7 +247,7 @@ function toDocFloat(value: RawValue, field: Field): number { } if (typeof value === 'string') { - value = parseFloat(value); + value = safeParseFloat(value); } if (value === null) { @@ -267,7 +267,7 @@ function toDocCheck(value: RawValue, field: Field): boolean { } if (typeof value === 'string') { - return !!parseFloat(value); + return !!safeParseFloat(value); } if (typeof value === 'number') { @@ -317,7 +317,7 @@ function toRawCurrency(value: DocValue, fyo: Fyo, field: Field): string { function toRawInt(value: DocValue, field: Field): number { if (typeof value === 'string') { - return parseInt(value); + return safeParseInt(value); } if (getIsNullOrUndef(value)) { @@ -333,7 +333,7 @@ function toRawInt(value: DocValue, field: Field): number { function toRawFloat(value: DocValue, field: Field): number { if (typeof value === 'string') { - return parseFloat(value); + return safeParseFloat(value); } if (getIsNullOrUndef(value)) { diff --git a/fyo/utils/format.ts b/fyo/utils/format.ts index 65deb483..f89cc18a 100644 --- a/fyo/utils/format.ts +++ b/fyo/utils/format.ts @@ -4,7 +4,7 @@ import { Doc } from 'fyo/model/doc'; import { DateTime } from 'luxon'; import { Money } from 'pesa'; import { Field, FieldType, FieldTypeEnum } from 'schemas/types'; -import { getIsNullOrUndef } from 'utils'; +import { getIsNullOrUndef, safeParseFloat } from 'utils'; import { DEFAULT_CURRENCY, DEFAULT_DATE_FORMAT, @@ -95,11 +95,11 @@ function formatNumber(value: DocValue, fyo: Fyo): string { } if ((value as Money).round) { - const floatValue = parseFloat((value as Money).round()); + const floatValue = safeParseFloat((value as Money).round()); return numberFormatter.format(floatValue); } - const floatValue = parseFloat(value as string); + const floatValue = safeParseFloat(value as string); const formattedNumber = numberFormatter.format(floatValue); if (formattedNumber === 'NaN') { diff --git a/fyo/utils/index.ts b/fyo/utils/index.ts index d7f07075..4f2be722 100644 --- a/fyo/utils/index.ts +++ b/fyo/utils/index.ts @@ -1,9 +1,9 @@ import { Fyo } from 'fyo'; import { Doc } from 'fyo/model/doc'; -import { Action, DocStatus } from 'fyo/model/types'; +import { Action } from 'fyo/model/types'; import { Money } from 'pesa'; import { Field, OptionField, SelectOption } from 'schemas/types'; -import { getIsNullOrUndef } from 'utils'; +import { getIsNullOrUndef, safeParseInt } from 'utils'; export function slug(str: string) { return str @@ -24,7 +24,7 @@ export function unique(list: T[], key = (it: T) => String(it)) { export function getDuplicates(array: unknown[]) { const duplicates: unknown[] = []; for (const i in array) { - const previous = array[parseInt(i) - 1]; + const previous = array[safeParseInt(i) - 1]; const current = array[i]; if (current === previous) { diff --git a/models/baseModels/Invoice/Invoice.ts b/models/baseModels/Invoice/Invoice.ts index 7aeb2ede..d897d86f 100644 --- a/models/baseModels/Invoice/Invoice.ts +++ b/models/baseModels/Invoice/Invoice.ts @@ -14,7 +14,7 @@ import { Transactional } from 'models/Transactional/Transactional'; import { ModelNameEnum } from 'models/types'; import { Money } from 'pesa'; import { FieldTypeEnum, Schema } from 'schemas/types'; -import { getIsNullOrUndef } from 'utils'; +import { getIsNullOrUndef, safeParseFloat } from 'utils'; import { Defaults } from '../Defaults/Defaults'; import { InvoiceItem } from '../InvoiceItem/InvoiceItem'; import { Party } from '../Party/Party'; @@ -157,7 +157,7 @@ export abstract class Invoice extends Transactional { toCurrency: currency as string, }); - return parseFloat(exchangeRate.toFixed(2)); + return safeParseFloat(exchangeRate.toFixed(2)); } async getTaxSummary() { diff --git a/models/helpers.ts b/models/helpers.ts index 89df457c..137cc8f4 100644 --- a/models/helpers.ts +++ b/models/helpers.ts @@ -3,6 +3,7 @@ import { Doc } from 'fyo/model/doc'; import { Action, ColumnConfig, DocStatus, RenderData } from 'fyo/model/types'; import { DateTime } from 'luxon'; import { Money } from 'pesa'; +import { safeParseFloat } from 'utils/index'; import { Router } from 'vue-router'; import { AccountRootType, @@ -211,7 +212,7 @@ export async function getExchangeRate({ let exchangeRate = 0; if (localStorage) { - exchangeRate = parseFloat( + exchangeRate = safeParseFloat( localStorage.getItem(cacheKey as string) as string ); } diff --git a/reports/LedgerReport.ts b/reports/LedgerReport.ts index da8bd72b..ac06dd27 100644 --- a/reports/LedgerReport.ts +++ b/reports/LedgerReport.ts @@ -4,6 +4,7 @@ import { ModelNameEnum } from 'models/types'; import { Report } from 'reports/Report'; import { GroupedMap, LedgerEntry, RawLedgerEntry } from 'reports/types'; import { QueryFilter } from 'utils/db/types'; +import { safeParseFloat, safeParseInt } from 'utils/index'; import getCommonExportActions from './commonExporter'; type GroupByKey = 'account' | 'party' | 'referenceName'; @@ -101,11 +102,11 @@ export abstract class LedgerReport extends Report { this._rawData = entries.map((entry) => { return { - name: parseInt(entry.name), + name: safeParseInt(entry.name), account: entry.account, date: new Date(entry.date), - debit: parseFloat(entry.debit), - credit: parseFloat(entry.credit), + debit: safeParseFloat(entry.debit), + credit: safeParseFloat(entry.credit), balance: 0, referenceType: entry.referenceType, referenceName: entry.referenceName, diff --git a/src/components/Controls/ExchangeRate.vue b/src/components/Controls/ExchangeRate.vue index 73d3de66..47f484dd 100644 --- a/src/components/Controls/ExchangeRate.vue +++ b/src/components/Controls/ExchangeRate.vue @@ -35,6 +35,7 @@