Skip to content

Commit

Permalink
centralize ensureArray code
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Aug 2, 2023
1 parent 5de71f5 commit 5ab4782
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 145 deletions.
72 changes: 20 additions & 52 deletions contentUpdaters.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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];
}
Expand All @@ -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) {
Expand All @@ -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];
Expand All @@ -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 =
Expand All @@ -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];
}
Expand All @@ -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]);
Expand All @@ -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];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
79 changes: 20 additions & 59 deletions contentUpdaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
UsagePointContent,
UsageSummaryContent
} from './types/entryTypes.js'
import { ensureArray } from './utilities.js'

function updateApplicationInformationContent(
content?: ApplicationInformationContent
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand All @@ -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 =
Expand All @@ -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) {
Expand All @@ -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]
Expand All @@ -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 = []

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 4 additions & 13 deletions objectUpdaters.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as lookups from './lookups.js';
import { ensureArray } from './utilities.js';
export function updateSummaryMeasurement(measurement) {
if (measurement === undefined) {
return;
Expand All @@ -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);
Expand All @@ -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];
}
Expand Down
Loading

0 comments on commit 5ab4782

Please sign in to comment.