diff --git a/src/app/core/utils/json.ts b/src/app/core/utils/json.ts deleted file mode 100644 index d506ca2bc..000000000 --- a/src/app/core/utils/json.ts +++ /dev/null @@ -1,28 +0,0 @@ -export function safeParse(raw: string | null | undefined, defaultValue: T): T { - if (!raw) return defaultValue; - try { - return JSON.parse(raw) as T; - } catch { - return defaultValue; - } -} - -export function safeParseObject(raw: string | null | undefined, defaultValue: T): T { - if (!raw) return defaultValue; - try { - const parsed = JSON.parse(raw); - return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? (parsed as T) : defaultValue; - } catch { - return defaultValue; - } -} - -export function safeParseArray(raw: string | null | undefined, defaultValue: T[] = []): T[] { - if (!raw) return defaultValue; - try { - const parsed = JSON.parse(raw); - return Array.isArray(parsed) ? (parsed as T[]) : defaultValue; - } catch { - return defaultValue; - } -} diff --git a/src/app/settings/settings.service.ts b/src/app/settings/settings.service.ts index e9bd505fc..228c574cf 100644 --- a/src/app/settings/settings.service.ts +++ b/src/app/settings/settings.service.ts @@ -2,7 +2,6 @@ import { Injectable } from '@angular/core'; import { AlertService } from 'app/core/alert/alert.service'; import { Dates } from 'app/core/utils/dates'; -import { safeParse, safeParseArray, safeParseObject } from 'app/core/utils/json'; /** Environment Imports */ import { environment } from '../../environments/environment'; @@ -109,9 +108,8 @@ export class SettingsService { /** * Returns date format setting. */ - get dateFormat(): string { - const parsed = safeParse(localStorage.getItem('mifosXDateFormat'), null); - return typeof parsed === 'string' && parsed.length > 0 ? parsed : 'dd MMMM yyyy'; + get dateFormat() { + return JSON.parse(localStorage.getItem('mifosXDateFormat')); } /** @@ -121,10 +119,7 @@ export class SettingsService { if (!localStorage.getItem('mifosXLanguage')) { this.setDefaultLanguage(); } - return safeParseObject<{ name: string; code: string } | undefined>( - localStorage.getItem('mifosXLanguage'), - undefined - ); + return JSON.parse(localStorage.getItem('mifosXLanguage')); } get languageCode() { @@ -152,7 +147,7 @@ export class SettingsService { * Returns list of default server */ get servers() { - return safeParseArray(localStorage.getItem('mifosXServers'), []); + return JSON.parse(localStorage.getItem('mifosXServers')); } /** @@ -165,7 +160,7 @@ export class SettingsService { if (environment.baseApiUrl && environment.baseApiUrl !== '') { return environment.baseApiUrl; } else { - return this.servers[0]; + return this.servers()[0]; } } @@ -222,7 +217,7 @@ export class SettingsService { * Returns list of Tenant Identifiers */ get tenantIdentifiers(): any { - return safeParseArray(localStorage.getItem('mifosXTenantIdentifiers'), []); + return JSON.parse(localStorage.getItem('mifosXTenantIdentifiers')); } /** @@ -265,6 +260,6 @@ export class SettingsService { } get themeDarkEnabled(): boolean { - return safeParse(localStorage.getItem('mifosXThemeDarkEnabled'), false); + return JSON.parse(localStorage.getItem('mifosXThemeDarkEnabled')); } } diff --git a/src/app/shared/theme-picker/theme-storage.service.ts b/src/app/shared/theme-picker/theme-storage.service.ts index 876ab149a..8fc4d9adf 100644 --- a/src/app/shared/theme-picker/theme-storage.service.ts +++ b/src/app/shared/theme-picker/theme-storage.service.ts @@ -1,5 +1,4 @@ import { Injectable, EventEmitter } from '@angular/core'; -import { safeParseObject } from 'app/core/utils/json'; import { Theme } from './theme.model'; import { ThemeManagerService } from './theme-manager.service'; @@ -20,7 +19,7 @@ export class ThemeStorageService { } getTheme(): Theme { - return safeParseObject(localStorage.getItem(this.themeStorageKey), null); + return JSON.parse(localStorage.getItem(this.themeStorageKey)); } clearTheme() { diff --git a/src/app/system/configurations/business-date-tab/business-date-tab.component.ts b/src/app/system/configurations/business-date-tab/business-date-tab.component.ts index 715127552..0fbbd7d98 100644 --- a/src/app/system/configurations/business-date-tab/business-date-tab.component.ts +++ b/src/app/system/configurations/business-date-tab/business-date-tab.component.ts @@ -46,7 +46,7 @@ export class BusinessDateTabComponent implements OnInit { businessDateData: any; dateIndex = 0; - userDateFormat: string = ''; + userDateFormat: ''; isBusinessDateEnabled = false; isEditInProgress = false; diff --git a/src/app/tasks/view-checker-inbox/view-checker-inbox.component.ts b/src/app/tasks/view-checker-inbox/view-checker-inbox.component.ts index bd1683387..627789eb4 100644 --- a/src/app/tasks/view-checker-inbox/view-checker-inbox.component.ts +++ b/src/app/tasks/view-checker-inbox/view-checker-inbox.component.ts @@ -15,7 +15,6 @@ import { MatDivider } from '@angular/material/divider'; import { NgIf, NgFor, KeyValuePipe } from '@angular/common'; import { DateFormatPipe } from '../../pipes/date-format.pipe'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; -import { safeParseObject } from 'app/core/utils/json'; @Component({ selector: 'mifosx-view-checker-inbox', @@ -53,7 +52,7 @@ export class ViewCheckerInboxComponent { ) { this.route.data.subscribe((data: { checkerInboxDetail: any }) => { this.checkerInboxDetail = data.checkerInboxDetail; - this.jsondata = safeParseObject(this.checkerInboxDetail.commandAsJson, {}); + this.jsondata = JSON.parse(this.checkerInboxDetail.commandAsJson); this.displayJSONData = !_.isEmpty(this.jsondata); }); } diff --git a/src/app/web-app.component.ts b/src/app/web-app.component.ts index 74d8149d0..d70c97daf 100644 --- a/src/app/web-app.component.ts +++ b/src/app/web-app.component.ts @@ -31,7 +31,6 @@ import { Dates } from './core/utils/dates'; import { animate, style, transition, trigger } from '@angular/animations'; import { I18nService } from './core/i18n/i18n.service'; import { ThemingService } from './shared/theme-toggle/theming.service'; -import { safeParseArray } from 'app/core/utils/json'; import { AuthService } from './zitadel/auth.service'; @@ -196,7 +195,7 @@ export class WebAppComponent implements OnInit, OnDestroy { // Stores top 100 user activites as local storage object. let activities: string[] = []; if (localStorage.getItem('mifosXLocation')) { - const activitiesArray = safeParseArray(localStorage.getItem('mifosXLocation'), []); + const activitiesArray: string[] = JSON.parse(localStorage.getItem('mifosXLocation')); const length = activitiesArray.length; activities = length > 100 ? activitiesArray.slice(length - 100) : activitiesArray; }