diff --git a/contentUpdaters.js b/contentUpdaters.js index b529bfb..602c902 100644 --- a/contentUpdaters.js +++ b/contentUpdaters.js @@ -1,6 +1,7 @@ import 'core-js'; import * as lookups from './lookups.js'; import { updateCostAdditionalDetail, updateSummaryMeasurement, updateTariffRider, updateUsagePoint } from './objectUpdaters.js'; +import { ensureArray } from './utilities.js'; function updateApplicationInformationContent(content) { if (content === undefined) { return; @@ -19,18 +20,14 @@ function updateApplicationInformationContent(content) { content.thirdPartyApplicationUse_value = lookups.thirdPartyApplicationUses[content.thirdPartyApplicationUse]; } - if (!Array.isArray(content.grant_types)) { - content.grant_types = [content.grant_types]; - } - if (content.contacts !== undefined && !Array.isArray(content.contacts)) { - content.contacts = [content.contacts]; - } - if (!Array.isArray(content.scope)) { - content.scope = [content.scope]; - } - content.grant_types_values = []; - for (const grantType of content.grant_types) { - content.grant_types_values.push(lookups.grantTypes[grantType]); + ensureArray(content, 'contacts'); + ensureArray(content, 'scope'); + if (content.grant_types !== undefined) { + ensureArray(content, 'grant_types'); + content.grant_types_values = []; + for (const grantType of content.grant_types) { + content.grant_types_values.push(lookups.grantTypes[grantType]); + } } content.response_types_value = lookups.responseTypes[content.response_types]; } @@ -51,9 +48,7 @@ function updateBatchListContent(content) { if (content === undefined) { return; } - if (!Array.isArray(content.resources)) { - content.resources = [content.resources]; - } + ensureArray(content, 'resources'); } function updateCustomerContent(content) { if (content === undefined) { @@ -68,9 +63,7 @@ function updateCustomerAccountContent(content) { return; } if (content.notifications !== undefined) { - if (!Array.isArray(content.notifications)) { - content.notifications = [content.notifications]; - } + ensureArray(content, 'notifications'); for (const notification of content.notifications) { notification.methodKind_value = lookups.notificationMethodKinds[notification.methodKind]; @@ -82,9 +75,7 @@ function updateCustomerAgreementContent(content) { return; } if (content.DemandResponseProgram !== undefined) { - if (!Array.isArray(content.DemandResponseProgram)) { - content.DemandResponseProgram = [content.DemandResponseProgram]; - } + ensureArray(content, 'DemandResponseProgram'); for (const program of content.DemandResponseProgram) { if (program.enrollmentStatus !== undefined) { program.enrollmentStatus_value = @@ -94,10 +85,7 @@ function updateCustomerAgreementContent(content) { updateSummaryMeasurement(program.DRProgramNomination); } } - if (content.PricingStructures !== undefined && - !Array.isArray(content.PricingStructures)) { - content.PricingStructures = [content.PricingStructures]; - } + ensureArray(content, 'PricingStructures'); if (content.currency !== undefined) { content.currency_value = lookups.currencies[content.currency]; } @@ -106,15 +94,10 @@ function updateIntervalBlockContent(content) { if (content === undefined) { return; } - if (content.IntervalReading !== undefined && - !Array.isArray(content.IntervalReading)) { - content.IntervalReading = [content.IntervalReading]; - } + ensureArray(content, 'IntervalReading'); for (const reading of content.IntervalReading ?? []) { if (reading.ReadingQuality !== undefined) { - if (!Array.isArray(reading.ReadingQuality)) { - reading.ReadingQuality = [reading.ReadingQuality]; - } + ensureArray(reading, 'ReadingQuality'); reading.ReadingQuality_values = []; for (const quality of reading.ReadingQuality) { reading.ReadingQuality_values.push(lookups.readingQualities[quality]); @@ -127,9 +110,7 @@ function updateMeterContent(content) { return; } if (content.MeterMultipliers !== undefined) { - if (!Array.isArray(content.MeterMultipliers)) { - content.MeterMultipliers = [content.MeterMultipliers]; - } + ensureArray(content, 'MeterMultipliers'); for (const multiplier of content.MeterMultipliers) { if (multiplier.kind !== undefined) { multiplier.kind_value = lookups.meterMultiplierKinds[multiplier.kind]; @@ -186,14 +167,9 @@ function updateServiceLocationContent(content) { if (content === undefined) { return; } - if (content.positionPoints !== undefined && - !Array.isArray(content.positionPoints)) { - content.positionPoints = [content.positionPoints]; - } + ensureArray(content, 'positionPoints'); if (content.UsagePoints !== undefined) { - if (!Array.isArray(content.UsagePoints)) { - content.UsagePoints = [content.UsagePoints]; - } + ensureArray(content, 'UsagePoints'); for (const usagePoint of content.UsagePoints) { updateUsagePoint(usagePoint); } @@ -224,11 +200,7 @@ function updateUsageSummaryContent(content) { return; } if (content.costAdditionalDetailsLastPeriod !== undefined) { - if (!Array.isArray(content.costAdditionalDetailsLastPeriod)) { - content.costAdditionalDetailsLastPeriod = [ - content.costAdditionalDetailsLastPeriod - ]; - } + ensureArray(content, 'costAdditionalDetailsLastPeriod'); for (const additionalDetail of content.costAdditionalDetailsLastPeriod) { updateCostAdditionalDetail(additionalDetail); } @@ -254,11 +226,7 @@ function updateUsageSummaryContent(content) { content.commodity_value = lookups.commodities[content.commodity]; } if (content.tariffRiderRefs?.tariffRiderRef !== undefined) { - if (!Array.isArray(content.tariffRiderRefs.tariffRiderRef)) { - content.tariffRiderRefs.tariffRiderRef = [ - content.tariffRiderRefs.tariffRiderRef - ]; - } + ensureArray(content.tariffRiderRefs, 'tariffRiderRef'); for (const tariffRider of content.tariffRiderRefs.tariffRiderRef) { updateTariffRider(tariffRider); } diff --git a/contentUpdaters.ts b/contentUpdaters.ts index 6ff8e9c..c536504 100644 --- a/contentUpdaters.ts +++ b/contentUpdaters.ts @@ -24,6 +24,7 @@ import type { UsagePointContent, UsageSummaryContent } from './types/entryTypes.js' +import { ensureArray } from './utilities.js' function updateApplicationInformationContent( content?: ApplicationInformationContent @@ -52,22 +53,17 @@ function updateApplicationInformationContent( lookups.thirdPartyApplicationUses[content.thirdPartyApplicationUse] } - if (!Array.isArray(content.grant_types)) { - content.grant_types = [content.grant_types] - } - - if (content.contacts !== undefined && !Array.isArray(content.contacts)) { - content.contacts = [content.contacts] - } + ensureArray(content, 'contacts') + ensureArray(content, 'scope') - if (!Array.isArray(content.scope)) { - content.scope = [content.scope] - } + if (content.grant_types !== undefined) { + ensureArray(content, 'grant_types') - content.grant_types_values = [] + content.grant_types_values = [] - for (const grantType of content.grant_types) { - content.grant_types_values.push(lookups.grantTypes[grantType]) + for (const grantType of content.grant_types) { + content.grant_types_values.push(lookups.grantTypes[grantType]) + } } content.response_types_value = lookups.responseTypes[content.response_types] @@ -96,9 +92,7 @@ function updateBatchListContent(content?: BatchListContent): void { return } - if (!Array.isArray(content.resources)) { - content.resources = [content.resources] - } + ensureArray(content, 'resources') } function updateCustomerContent(content?: CustomerContent): void { @@ -117,9 +111,7 @@ function updateCustomerAccountContent(content?: CustomerAccountContent): void { } if (content.notifications !== undefined) { - if (!Array.isArray(content.notifications)) { - content.notifications = [content.notifications] - } + ensureArray(content, 'notifications') for (const notification of content.notifications) { notification.methodKind_value = @@ -136,9 +128,7 @@ function updateCustomerAgreementContent( } if (content.DemandResponseProgram !== undefined) { - if (!Array.isArray(content.DemandResponseProgram)) { - content.DemandResponseProgram = [content.DemandResponseProgram] - } + ensureArray(content, 'DemandResponseProgram') for (const program of content.DemandResponseProgram) { if (program.enrollmentStatus !== undefined) { @@ -151,12 +141,7 @@ function updateCustomerAgreementContent( } } - if ( - content.PricingStructures !== undefined && - !Array.isArray(content.PricingStructures) - ) { - content.PricingStructures = [content.PricingStructures] - } + ensureArray(content, 'PricingStructures') if (content.currency !== undefined) { content.currency_value = lookups.currencies[content.currency] @@ -168,18 +153,11 @@ function updateIntervalBlockContent(content?: IntervalBlockContent): void { return } - if ( - content.IntervalReading !== undefined && - !Array.isArray(content.IntervalReading) - ) { - content.IntervalReading = [content.IntervalReading] - } + ensureArray(content, 'IntervalReading') for (const reading of content.IntervalReading ?? []) { if (reading.ReadingQuality !== undefined) { - if (!Array.isArray(reading.ReadingQuality)) { - reading.ReadingQuality = [reading.ReadingQuality] - } + ensureArray(reading, 'ReadingQuality') reading.ReadingQuality_values = [] @@ -196,9 +174,7 @@ function updateMeterContent(content?: MeterContent): void { } if (content.MeterMultipliers !== undefined) { - if (!Array.isArray(content.MeterMultipliers)) { - content.MeterMultipliers = [content.MeterMultipliers] - } + ensureArray(content, 'MeterMultipliers') for (const multiplier of content.MeterMultipliers) { if (multiplier.kind !== undefined) { @@ -271,17 +247,10 @@ function updateServiceLocationContent(content?: ServiceLocationContent): void { return } - if ( - content.positionPoints !== undefined && - !Array.isArray(content.positionPoints) - ) { - content.positionPoints = [content.positionPoints] - } + ensureArray(content, 'positionPoints') if (content.UsagePoints !== undefined) { - if (!Array.isArray(content.UsagePoints)) { - content.UsagePoints = [content.UsagePoints] - } + ensureArray(content, 'UsagePoints') for (const usagePoint of content.UsagePoints) { updateUsagePoint(usagePoint) @@ -321,11 +290,7 @@ function updateUsageSummaryContent(content?: UsageSummaryContent): void { } if (content.costAdditionalDetailsLastPeriod !== undefined) { - if (!Array.isArray(content.costAdditionalDetailsLastPeriod)) { - content.costAdditionalDetailsLastPeriod = [ - content.costAdditionalDetailsLastPeriod - ] - } + ensureArray(content, 'costAdditionalDetailsLastPeriod') for (const additionalDetail of content.costAdditionalDetailsLastPeriod) { updateCostAdditionalDetail(additionalDetail) @@ -358,11 +323,7 @@ function updateUsageSummaryContent(content?: UsageSummaryContent): void { } if (content.tariffRiderRefs?.tariffRiderRef !== undefined) { - if (!Array.isArray(content.tariffRiderRefs.tariffRiderRef)) { - content.tariffRiderRefs.tariffRiderRef = [ - content.tariffRiderRefs.tariffRiderRef - ] - } + ensureArray(content.tariffRiderRefs, 'tariffRiderRef') for (const tariffRider of content.tariffRiderRefs.tariffRiderRef) { updateTariffRider(tariffRider) diff --git a/objectUpdaters.js b/objectUpdaters.js index 3277ba5..59ba288 100644 --- a/objectUpdaters.js +++ b/objectUpdaters.js @@ -1,4 +1,5 @@ import * as lookups from './lookups.js'; +import { ensureArray } from './utilities.js'; export function updateSummaryMeasurement(measurement) { if (measurement === undefined) { return; @@ -22,11 +23,7 @@ export function updateUsagePoint(usagePoint) { } if (usagePoint.ServiceDeliveryPoint?.tariffRiderRefs?.tariffRiderRef !== undefined) { - if (!Array.isArray(usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef)) { - usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef = [ - usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef - ]; - } + ensureArray(usagePoint.ServiceDeliveryPoint.tariffRiderRefs, 'tariffRiderRef'); for (const tariffRider of usagePoint.ServiceDeliveryPoint.tariffRiderRefs .tariffRiderRef) { updateTariffRider(tariffRider); @@ -48,19 +45,13 @@ export function updateUsagePoint(usagePoint) { updateSummaryMeasurement(usagePoint.ratedCurrent); updateSummaryMeasurement(usagePoint.ratedPower); if (usagePoint.pnodeRefs !== undefined) { - if (!Array.isArray(usagePoint.pnodeRefs.pnodeRef)) { - usagePoint.pnodeRefs.pnodeRef = [usagePoint.pnodeRefs.pnodeRef]; - } + ensureArray(usagePoint.pnodeRefs, 'pnodeRef'); for (const pnode of usagePoint.pnodeRefs.pnodeRef) { pnode.apnodeType_value = lookups.pnodeTypes[pnode.apnodeType]; } } if (usagePoint.aggregateNodeRefs !== undefined) { - if (!Array.isArray(usagePoint.aggregateNodeRefs.aggregateNodeRef)) { - usagePoint.aggregateNodeRefs.aggregateNodeRef = [ - usagePoint.aggregateNodeRefs.aggregateNodeRef - ]; - } + ensureArray(usagePoint.aggregateNodeRefs, 'aggregateNodeRef'); for (const anode of usagePoint.aggregateNodeRefs.aggregateNodeRef) { anode.anodeType_value = lookups.anodeTypes[anode.anodeType]; } diff --git a/objectUpdaters.ts b/objectUpdaters.ts index ae947cf..161d4da 100644 --- a/objectUpdaters.ts +++ b/objectUpdaters.ts @@ -5,6 +5,7 @@ import type { GreenButtonTariffRider, GreenButtonUsagePoint } from './types/objectTypes.js' +import { ensureArray } from './utilities.js' export function updateSummaryMeasurement( measurement?: GreenButtonSummaryMeasurement @@ -38,15 +39,10 @@ export function updateUsagePoint(usagePoint: GreenButtonUsagePoint): void { usagePoint.ServiceDeliveryPoint?.tariffRiderRefs?.tariffRiderRef !== undefined ) { - if ( - !Array.isArray( - usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef - ) - ) { - usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef = [ - usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef - ] - } + ensureArray( + usagePoint.ServiceDeliveryPoint.tariffRiderRefs, + 'tariffRiderRef' + ) for (const tariffRider of usagePoint.ServiceDeliveryPoint.tariffRiderRefs .tariffRiderRef) { @@ -75,9 +71,7 @@ export function updateUsagePoint(usagePoint: GreenButtonUsagePoint): void { updateSummaryMeasurement(usagePoint.ratedPower) if (usagePoint.pnodeRefs !== undefined) { - if (!Array.isArray(usagePoint.pnodeRefs.pnodeRef)) { - usagePoint.pnodeRefs.pnodeRef = [usagePoint.pnodeRefs.pnodeRef] - } + ensureArray(usagePoint.pnodeRefs, 'pnodeRef') for (const pnode of usagePoint.pnodeRefs.pnodeRef) { pnode.apnodeType_value = lookups.pnodeTypes[pnode.apnodeType] @@ -85,11 +79,7 @@ export function updateUsagePoint(usagePoint: GreenButtonUsagePoint): void { } if (usagePoint.aggregateNodeRefs !== undefined) { - if (!Array.isArray(usagePoint.aggregateNodeRefs.aggregateNodeRef)) { - usagePoint.aggregateNodeRefs.aggregateNodeRef = [ - usagePoint.aggregateNodeRefs.aggregateNodeRef - ] - } + ensureArray(usagePoint.aggregateNodeRefs, 'aggregateNodeRef') for (const anode of usagePoint.aggregateNodeRefs.aggregateNodeRef) { anode.anodeType_value = lookups.anodeTypes[anode.anodeType] diff --git a/types/entryTypes.d.ts b/types/entryTypes.d.ts index 8550baa..a11bf61 100644 --- a/types/entryTypes.d.ts +++ b/types/entryTypes.d.ts @@ -75,7 +75,7 @@ export interface ApplicationInformationContent { contacts?: string[]; token_endpoint_auth_method: string; scope: string[]; - grant_types: Array; + grant_types?: Array; grant_types_values?: Array<(typeof lookups.grantTypes)[keyof typeof lookups.grantTypes]>; response_types: keyof typeof lookups.responseTypes; response_types_value?: (typeof lookups.responseTypes)[keyof typeof lookups.responseTypes]; diff --git a/types/entryTypes.ts b/types/entryTypes.ts index a11ece6..f4ae16b 100644 --- a/types/entryTypes.ts +++ b/types/entryTypes.ts @@ -95,7 +95,7 @@ export interface ApplicationInformationContent { token_endpoint_auth_method: string scope: string[] - grant_types: Array + grant_types?: Array grant_types_values?: Array< (typeof lookups.grantTypes)[keyof typeof lookups.grantTypes] > diff --git a/types/objectTypes.d.ts b/types/objectTypes.d.ts index 94e2b55..fa5873b 100644 --- a/types/objectTypes.d.ts +++ b/types/objectTypes.d.ts @@ -147,7 +147,7 @@ export interface GreenButtonUsagePoint { tariffProfile?: string; customerAgreement?: string; tariffRiderRefs?: { - tariffRiderRef?: GreenButtonTariffRider | GreenButtonTariffRider[]; + tariffRiderRef?: GreenButtonTariffRider[]; }; }; amiBillingReady?: keyof typeof lookups.amiBillingReadyStatuses; diff --git a/types/objectTypes.ts b/types/objectTypes.ts index b17d465..5e7cf96 100644 --- a/types/objectTypes.ts +++ b/types/objectTypes.ts @@ -184,7 +184,7 @@ export interface GreenButtonUsagePoint { tariffProfile?: string customerAgreement?: string tariffRiderRefs?: { - tariffRiderRef?: GreenButtonTariffRider | GreenButtonTariffRider[] + tariffRiderRef?: GreenButtonTariffRider[] } } diff --git a/utilities.d.ts b/utilities.d.ts index 244ef68..dd422e0 100644 --- a/utilities.d.ts +++ b/utilities.d.ts @@ -6,4 +6,5 @@ type XmlProperty = string[] | Array<{ _: string; }>; export declare function getFirstXmlString(xmlProperty?: XmlProperty): string; +export declare function ensureArray(object: any, objectKey: string): void; export {}; diff --git a/utilities.js b/utilities.js index e54ce70..b0250bf 100644 --- a/utilities.js +++ b/utilities.js @@ -47,3 +47,8 @@ export function getFirstXmlString(xmlProperty) { } return first._; } +export function ensureArray(object, objectKey) { + if (object[objectKey] !== undefined && !Array.isArray(object[objectKey])) { + object[objectKey] = [object[objectKey]]; + } +} diff --git a/utilities.ts b/utilities.ts index b90d2c4..02584da 100644 --- a/utilities.ts +++ b/utilities.ts @@ -69,3 +69,10 @@ export function getFirstXmlString(xmlProperty?: XmlProperty): string { return first._ } + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function ensureArray(object: any, objectKey: string): void { + if (object[objectKey] !== undefined && !Array.isArray(object[objectKey])) { + object[objectKey] = [object[objectKey]] + } +}