Skip to content

Commit 5ab4782

Browse files
committed
centralize ensureArray code
1 parent 5de71f5 commit 5ab4782

11 files changed

+68
-145
lines changed

contentUpdaters.js

+20-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'core-js';
22
import * as lookups from './lookups.js';
33
import { updateCostAdditionalDetail, updateSummaryMeasurement, updateTariffRider, updateUsagePoint } from './objectUpdaters.js';
4+
import { ensureArray } from './utilities.js';
45
function updateApplicationInformationContent(content) {
56
if (content === undefined) {
67
return;
@@ -19,18 +20,14 @@ function updateApplicationInformationContent(content) {
1920
content.thirdPartyApplicationUse_value =
2021
lookups.thirdPartyApplicationUses[content.thirdPartyApplicationUse];
2122
}
22-
if (!Array.isArray(content.grant_types)) {
23-
content.grant_types = [content.grant_types];
24-
}
25-
if (content.contacts !== undefined && !Array.isArray(content.contacts)) {
26-
content.contacts = [content.contacts];
27-
}
28-
if (!Array.isArray(content.scope)) {
29-
content.scope = [content.scope];
30-
}
31-
content.grant_types_values = [];
32-
for (const grantType of content.grant_types) {
33-
content.grant_types_values.push(lookups.grantTypes[grantType]);
23+
ensureArray(content, 'contacts');
24+
ensureArray(content, 'scope');
25+
if (content.grant_types !== undefined) {
26+
ensureArray(content, 'grant_types');
27+
content.grant_types_values = [];
28+
for (const grantType of content.grant_types) {
29+
content.grant_types_values.push(lookups.grantTypes[grantType]);
30+
}
3431
}
3532
content.response_types_value = lookups.responseTypes[content.response_types];
3633
}
@@ -51,9 +48,7 @@ function updateBatchListContent(content) {
5148
if (content === undefined) {
5249
return;
5350
}
54-
if (!Array.isArray(content.resources)) {
55-
content.resources = [content.resources];
56-
}
51+
ensureArray(content, 'resources');
5752
}
5853
function updateCustomerContent(content) {
5954
if (content === undefined) {
@@ -68,9 +63,7 @@ function updateCustomerAccountContent(content) {
6863
return;
6964
}
7065
if (content.notifications !== undefined) {
71-
if (!Array.isArray(content.notifications)) {
72-
content.notifications = [content.notifications];
73-
}
66+
ensureArray(content, 'notifications');
7467
for (const notification of content.notifications) {
7568
notification.methodKind_value =
7669
lookups.notificationMethodKinds[notification.methodKind];
@@ -82,9 +75,7 @@ function updateCustomerAgreementContent(content) {
8275
return;
8376
}
8477
if (content.DemandResponseProgram !== undefined) {
85-
if (!Array.isArray(content.DemandResponseProgram)) {
86-
content.DemandResponseProgram = [content.DemandResponseProgram];
87-
}
78+
ensureArray(content, 'DemandResponseProgram');
8879
for (const program of content.DemandResponseProgram) {
8980
if (program.enrollmentStatus !== undefined) {
9081
program.enrollmentStatus_value =
@@ -94,10 +85,7 @@ function updateCustomerAgreementContent(content) {
9485
updateSummaryMeasurement(program.DRProgramNomination);
9586
}
9687
}
97-
if (content.PricingStructures !== undefined &&
98-
!Array.isArray(content.PricingStructures)) {
99-
content.PricingStructures = [content.PricingStructures];
100-
}
88+
ensureArray(content, 'PricingStructures');
10189
if (content.currency !== undefined) {
10290
content.currency_value = lookups.currencies[content.currency];
10391
}
@@ -106,15 +94,10 @@ function updateIntervalBlockContent(content) {
10694
if (content === undefined) {
10795
return;
10896
}
109-
if (content.IntervalReading !== undefined &&
110-
!Array.isArray(content.IntervalReading)) {
111-
content.IntervalReading = [content.IntervalReading];
112-
}
97+
ensureArray(content, 'IntervalReading');
11398
for (const reading of content.IntervalReading ?? []) {
11499
if (reading.ReadingQuality !== undefined) {
115-
if (!Array.isArray(reading.ReadingQuality)) {
116-
reading.ReadingQuality = [reading.ReadingQuality];
117-
}
100+
ensureArray(reading, 'ReadingQuality');
118101
reading.ReadingQuality_values = [];
119102
for (const quality of reading.ReadingQuality) {
120103
reading.ReadingQuality_values.push(lookups.readingQualities[quality]);
@@ -127,9 +110,7 @@ function updateMeterContent(content) {
127110
return;
128111
}
129112
if (content.MeterMultipliers !== undefined) {
130-
if (!Array.isArray(content.MeterMultipliers)) {
131-
content.MeterMultipliers = [content.MeterMultipliers];
132-
}
113+
ensureArray(content, 'MeterMultipliers');
133114
for (const multiplier of content.MeterMultipliers) {
134115
if (multiplier.kind !== undefined) {
135116
multiplier.kind_value = lookups.meterMultiplierKinds[multiplier.kind];
@@ -186,14 +167,9 @@ function updateServiceLocationContent(content) {
186167
if (content === undefined) {
187168
return;
188169
}
189-
if (content.positionPoints !== undefined &&
190-
!Array.isArray(content.positionPoints)) {
191-
content.positionPoints = [content.positionPoints];
192-
}
170+
ensureArray(content, 'positionPoints');
193171
if (content.UsagePoints !== undefined) {
194-
if (!Array.isArray(content.UsagePoints)) {
195-
content.UsagePoints = [content.UsagePoints];
196-
}
172+
ensureArray(content, 'UsagePoints');
197173
for (const usagePoint of content.UsagePoints) {
198174
updateUsagePoint(usagePoint);
199175
}
@@ -224,11 +200,7 @@ function updateUsageSummaryContent(content) {
224200
return;
225201
}
226202
if (content.costAdditionalDetailsLastPeriod !== undefined) {
227-
if (!Array.isArray(content.costAdditionalDetailsLastPeriod)) {
228-
content.costAdditionalDetailsLastPeriod = [
229-
content.costAdditionalDetailsLastPeriod
230-
];
231-
}
203+
ensureArray(content, 'costAdditionalDetailsLastPeriod');
232204
for (const additionalDetail of content.costAdditionalDetailsLastPeriod) {
233205
updateCostAdditionalDetail(additionalDetail);
234206
}
@@ -254,11 +226,7 @@ function updateUsageSummaryContent(content) {
254226
content.commodity_value = lookups.commodities[content.commodity];
255227
}
256228
if (content.tariffRiderRefs?.tariffRiderRef !== undefined) {
257-
if (!Array.isArray(content.tariffRiderRefs.tariffRiderRef)) {
258-
content.tariffRiderRefs.tariffRiderRef = [
259-
content.tariffRiderRefs.tariffRiderRef
260-
];
261-
}
229+
ensureArray(content.tariffRiderRefs, 'tariffRiderRef');
262230
for (const tariffRider of content.tariffRiderRefs.tariffRiderRef) {
263231
updateTariffRider(tariffRider);
264232
}

contentUpdaters.ts

+20-59
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
UsagePointContent,
2525
UsageSummaryContent
2626
} from './types/entryTypes.js'
27+
import { ensureArray } from './utilities.js'
2728

2829
function updateApplicationInformationContent(
2930
content?: ApplicationInformationContent
@@ -52,22 +53,17 @@ function updateApplicationInformationContent(
5253
lookups.thirdPartyApplicationUses[content.thirdPartyApplicationUse]
5354
}
5455

55-
if (!Array.isArray(content.grant_types)) {
56-
content.grant_types = [content.grant_types]
57-
}
58-
59-
if (content.contacts !== undefined && !Array.isArray(content.contacts)) {
60-
content.contacts = [content.contacts]
61-
}
56+
ensureArray(content, 'contacts')
57+
ensureArray(content, 'scope')
6258

63-
if (!Array.isArray(content.scope)) {
64-
content.scope = [content.scope]
65-
}
59+
if (content.grant_types !== undefined) {
60+
ensureArray(content, 'grant_types')
6661

67-
content.grant_types_values = []
62+
content.grant_types_values = []
6863

69-
for (const grantType of content.grant_types) {
70-
content.grant_types_values.push(lookups.grantTypes[grantType])
64+
for (const grantType of content.grant_types) {
65+
content.grant_types_values.push(lookups.grantTypes[grantType])
66+
}
7167
}
7268

7369
content.response_types_value = lookups.responseTypes[content.response_types]
@@ -96,9 +92,7 @@ function updateBatchListContent(content?: BatchListContent): void {
9692
return
9793
}
9894

99-
if (!Array.isArray(content.resources)) {
100-
content.resources = [content.resources]
101-
}
95+
ensureArray(content, 'resources')
10296
}
10397

10498
function updateCustomerContent(content?: CustomerContent): void {
@@ -117,9 +111,7 @@ function updateCustomerAccountContent(content?: CustomerAccountContent): void {
117111
}
118112

119113
if (content.notifications !== undefined) {
120-
if (!Array.isArray(content.notifications)) {
121-
content.notifications = [content.notifications]
122-
}
114+
ensureArray(content, 'notifications')
123115

124116
for (const notification of content.notifications) {
125117
notification.methodKind_value =
@@ -136,9 +128,7 @@ function updateCustomerAgreementContent(
136128
}
137129

138130
if (content.DemandResponseProgram !== undefined) {
139-
if (!Array.isArray(content.DemandResponseProgram)) {
140-
content.DemandResponseProgram = [content.DemandResponseProgram]
141-
}
131+
ensureArray(content, 'DemandResponseProgram')
142132

143133
for (const program of content.DemandResponseProgram) {
144134
if (program.enrollmentStatus !== undefined) {
@@ -151,12 +141,7 @@ function updateCustomerAgreementContent(
151141
}
152142
}
153143

154-
if (
155-
content.PricingStructures !== undefined &&
156-
!Array.isArray(content.PricingStructures)
157-
) {
158-
content.PricingStructures = [content.PricingStructures]
159-
}
144+
ensureArray(content, 'PricingStructures')
160145

161146
if (content.currency !== undefined) {
162147
content.currency_value = lookups.currencies[content.currency]
@@ -168,18 +153,11 @@ function updateIntervalBlockContent(content?: IntervalBlockContent): void {
168153
return
169154
}
170155

171-
if (
172-
content.IntervalReading !== undefined &&
173-
!Array.isArray(content.IntervalReading)
174-
) {
175-
content.IntervalReading = [content.IntervalReading]
176-
}
156+
ensureArray(content, 'IntervalReading')
177157

178158
for (const reading of content.IntervalReading ?? []) {
179159
if (reading.ReadingQuality !== undefined) {
180-
if (!Array.isArray(reading.ReadingQuality)) {
181-
reading.ReadingQuality = [reading.ReadingQuality]
182-
}
160+
ensureArray(reading, 'ReadingQuality')
183161

184162
reading.ReadingQuality_values = []
185163

@@ -196,9 +174,7 @@ function updateMeterContent(content?: MeterContent): void {
196174
}
197175

198176
if (content.MeterMultipliers !== undefined) {
199-
if (!Array.isArray(content.MeterMultipliers)) {
200-
content.MeterMultipliers = [content.MeterMultipliers]
201-
}
177+
ensureArray(content, 'MeterMultipliers')
202178

203179
for (const multiplier of content.MeterMultipliers) {
204180
if (multiplier.kind !== undefined) {
@@ -271,17 +247,10 @@ function updateServiceLocationContent(content?: ServiceLocationContent): void {
271247
return
272248
}
273249

274-
if (
275-
content.positionPoints !== undefined &&
276-
!Array.isArray(content.positionPoints)
277-
) {
278-
content.positionPoints = [content.positionPoints]
279-
}
250+
ensureArray(content, 'positionPoints')
280251

281252
if (content.UsagePoints !== undefined) {
282-
if (!Array.isArray(content.UsagePoints)) {
283-
content.UsagePoints = [content.UsagePoints]
284-
}
253+
ensureArray(content, 'UsagePoints')
285254

286255
for (const usagePoint of content.UsagePoints) {
287256
updateUsagePoint(usagePoint)
@@ -321,11 +290,7 @@ function updateUsageSummaryContent(content?: UsageSummaryContent): void {
321290
}
322291

323292
if (content.costAdditionalDetailsLastPeriod !== undefined) {
324-
if (!Array.isArray(content.costAdditionalDetailsLastPeriod)) {
325-
content.costAdditionalDetailsLastPeriod = [
326-
content.costAdditionalDetailsLastPeriod
327-
]
328-
}
293+
ensureArray(content, 'costAdditionalDetailsLastPeriod')
329294

330295
for (const additionalDetail of content.costAdditionalDetailsLastPeriod) {
331296
updateCostAdditionalDetail(additionalDetail)
@@ -358,11 +323,7 @@ function updateUsageSummaryContent(content?: UsageSummaryContent): void {
358323
}
359324

360325
if (content.tariffRiderRefs?.tariffRiderRef !== undefined) {
361-
if (!Array.isArray(content.tariffRiderRefs.tariffRiderRef)) {
362-
content.tariffRiderRefs.tariffRiderRef = [
363-
content.tariffRiderRefs.tariffRiderRef
364-
]
365-
}
326+
ensureArray(content.tariffRiderRefs, 'tariffRiderRef')
366327

367328
for (const tariffRider of content.tariffRiderRefs.tariffRiderRef) {
368329
updateTariffRider(tariffRider)

objectUpdaters.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as lookups from './lookups.js';
2+
import { ensureArray } from './utilities.js';
23
export function updateSummaryMeasurement(measurement) {
34
if (measurement === undefined) {
45
return;
@@ -22,11 +23,7 @@ export function updateUsagePoint(usagePoint) {
2223
}
2324
if (usagePoint.ServiceDeliveryPoint?.tariffRiderRefs?.tariffRiderRef !==
2425
undefined) {
25-
if (!Array.isArray(usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef)) {
26-
usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef = [
27-
usagePoint.ServiceDeliveryPoint.tariffRiderRefs.tariffRiderRef
28-
];
29-
}
26+
ensureArray(usagePoint.ServiceDeliveryPoint.tariffRiderRefs, 'tariffRiderRef');
3027
for (const tariffRider of usagePoint.ServiceDeliveryPoint.tariffRiderRefs
3128
.tariffRiderRef) {
3229
updateTariffRider(tariffRider);
@@ -48,19 +45,13 @@ export function updateUsagePoint(usagePoint) {
4845
updateSummaryMeasurement(usagePoint.ratedCurrent);
4946
updateSummaryMeasurement(usagePoint.ratedPower);
5047
if (usagePoint.pnodeRefs !== undefined) {
51-
if (!Array.isArray(usagePoint.pnodeRefs.pnodeRef)) {
52-
usagePoint.pnodeRefs.pnodeRef = [usagePoint.pnodeRefs.pnodeRef];
53-
}
48+
ensureArray(usagePoint.pnodeRefs, 'pnodeRef');
5449
for (const pnode of usagePoint.pnodeRefs.pnodeRef) {
5550
pnode.apnodeType_value = lookups.pnodeTypes[pnode.apnodeType];
5651
}
5752
}
5853
if (usagePoint.aggregateNodeRefs !== undefined) {
59-
if (!Array.isArray(usagePoint.aggregateNodeRefs.aggregateNodeRef)) {
60-
usagePoint.aggregateNodeRefs.aggregateNodeRef = [
61-
usagePoint.aggregateNodeRefs.aggregateNodeRef
62-
];
63-
}
54+
ensureArray(usagePoint.aggregateNodeRefs, 'aggregateNodeRef');
6455
for (const anode of usagePoint.aggregateNodeRefs.aggregateNodeRef) {
6556
anode.anodeType_value = lookups.anodeTypes[anode.anodeType];
6657
}

0 commit comments

Comments
 (0)