Skip to content

Commit bd1edc0

Browse files
author
Scott Switzer
committed
added tests for wankdanker#53, modified the place where a single index is being modified. further cleanup is needed
1 parent 0766113 commit bd1edc0

File tree

2 files changed

+238
-2
lines changed

2 files changed

+238
-2
lines changed

src/object-mapper.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ function update_arr(dest, key, data, keys, context)
257257
return dest
258258
}
259259

260+
// Just update a single array node
261+
if (key.ix !== '') {
262+
let o
263+
if (dest !== null && typeof dest !== 'undefined' && typeof dest[key.ix] !== 'undefined')
264+
o = (keys.length) ? update(dest[key.ix], data, keys, context) : data
265+
else
266+
o = (keys.length) ? update(null, data, keys, context) : data
267+
268+
// Only update (and create if needed) dest if there is data to be saved
269+
if (o !== null) {
270+
dest = dest || []
271+
dest[key.ix] = o
272+
}
273+
return dest
274+
}
275+
260276
// If the data is in an array format then make sure that there is a dest index for each data index
261277
if (Array.isArray(data)) {
262278
dest = dest || []

test/test.js

Lines changed: 222 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ test('set value - array to object inside simple array', function (t) {
527527
});
528528
test('set value - object inside simple array defined index', function (t) {
529529
var key = '[3].foo';
530-
var value = 'bar';
530+
var data = 'bar';
531531

532532
var expect = [, , , {
533533
foo: 'bar'
534534
}];
535535

536-
var result = om.setKeyValue(null, key, value);
536+
var result = om.setKeyValue(null, key, data);
537537

538538
t.deepEqual(result, expect);
539539
t.end();
@@ -2290,3 +2290,223 @@ test('Make sure no objects are created without data #48', function (t) {
22902290
// t.deepEqual(result, expect);
22912291
// t.end();
22922292
// });
2293+
2294+
test('Should correctly map to a subelement of indexed array item.', function (t) {
2295+
var obj = {
2296+
"nodes": [
2297+
{
2298+
"type": "db",
2299+
"image": "mongodb"
2300+
},
2301+
{
2302+
"type": "app",
2303+
"image": "nginx"
2304+
}
2305+
]
2306+
};
2307+
2308+
var expect = {
2309+
"result": [ {
2310+
"env": {
2311+
"nodes": [
2312+
{
2313+
"type": "db",
2314+
"image": "mongodb"
2315+
},
2316+
{
2317+
"type": "app",
2318+
"image": "nginx"
2319+
}]
2320+
}
2321+
}]
2322+
};
2323+
var map = {
2324+
'nodes[].type': 'result[0].env.nodes[].type',
2325+
'nodes[].image': 'result[0].env.nodes[].image'
2326+
};
2327+
2328+
var result = om(obj, map);
2329+
2330+
t.deepEqual(result, expect);
2331+
t.end();
2332+
});
2333+
2334+
test('Should correctly map to a subelement of indexed array item deep in the object hierarchy.', function (t) {
2335+
2336+
var obj = {
2337+
policyNumber: "PN1",
2338+
status: "ST1",
2339+
productName: "PN1",
2340+
productVersion: "PV1",
2341+
productType: "PT1",
2342+
startDate: "SD1",
2343+
endDate: "ED1",
2344+
policyContacts: [
2345+
{
2346+
contactType: "CT11",
2347+
contactId: "CI11"
2348+
},
2349+
{
2350+
contactType: "CT12",
2351+
contactId: "CI12"
2352+
}
2353+
],
2354+
currency: "CUR1",
2355+
covers: [
2356+
{
2357+
coverName: "CN11",
2358+
coverId: "CID11",
2359+
startDate: "SD11",
2360+
endDate: "ED11",
2361+
excess: "EX11",
2362+
insuredAmount: "IA11",
2363+
yearlyPremium: "YP11",
2364+
status: "ST11"
2365+
}
2366+
],
2367+
riskObjects: [
2368+
{
2369+
assetId: "AID11",
2370+
type: "T11",
2371+
assetDescription: "AD11",
2372+
address: {
2373+
city: "C11",
2374+
houseNumber: "HN11",
2375+
country: "CN11",
2376+
zipCode: "ZC11",
2377+
}
2378+
}
2379+
]
2380+
2381+
};
2382+
2383+
var expect = {
2384+
policyHeader: [
2385+
{
2386+
policiesList: {
2387+
policies: {
2388+
externalPolicyNr: "PN1",
2389+
policyLobsList: {
2390+
policyLobs: {
2391+
lobRef: {
2392+
value: "PT1"
2393+
},
2394+
policyLobAssetsList: {
2395+
policyLobAssets: [
2396+
{
2397+
coversList: {
2398+
covers: [
2399+
{
2400+
coverPerilsList: {
2401+
coverPerils: [
2402+
{
2403+
perilRef: {
2404+
value: "CN11"
2405+
}
2406+
}
2407+
]
2408+
},
2409+
externalNumber: "CID11",
2410+
startDate: "SD11",
2411+
endDate: "ED11",
2412+
excessAmount: "EX11",
2413+
insuranceAmount: "IA11",
2414+
basicYearlyPremiumAmount: "YP11",
2415+
coverStatusRef: {
2416+
value: "ST11"
2417+
}
2418+
}
2419+
]
2420+
},
2421+
originalLobAssetId: "AID11",
2422+
asset: {
2423+
assetTypeRef: {
2424+
value: "T11"
2425+
},
2426+
propertyOccupationTypeRef: "AD11",
2427+
address: {
2428+
cityName: "C11",
2429+
houseNr: "HN11",
2430+
countryRef: {
2431+
value: "CN11"
2432+
},
2433+
zipCode: "ZC11"
2434+
2435+
}
2436+
2437+
}
2438+
}
2439+
]
2440+
2441+
}
2442+
}
2443+
},
2444+
policyContactsList: {
2445+
policyContacts: [
2446+
{
2447+
policyContactRoleRef: {
2448+
value: "CT11"
2449+
},
2450+
contactExtNum: "CI11"
2451+
},
2452+
{
2453+
policyContactRoleRef: {
2454+
value: "CT12"
2455+
},
2456+
contactExtNum: "CI12"
2457+
},
2458+
]
2459+
}
2460+
}
2461+
},
2462+
statusCodeRef: {
2463+
value: "ST1"
2464+
},
2465+
productRef: {
2466+
value: "PN1"
2467+
},
2468+
productVersionRef: {
2469+
value: "PV1"
2470+
},
2471+
policyStartDate: "SD1",
2472+
policyEndDate: "ED1",
2473+
currencyRef: {
2474+
value: "CUR1"
2475+
}
2476+
}
2477+
]
2478+
};
2479+
2480+
var map = {
2481+
"covers[].coverId": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].externalNumber",
2482+
"covers[].coverName": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].coverPerilsList.coverPerils[0].perilRef.value",
2483+
"covers[].endDate": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].endDate",
2484+
"covers[].excess": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].excessAmount",
2485+
"covers[].insuredAmount": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].insuranceAmount",
2486+
"covers[].startDate": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].startDate",
2487+
"covers[].status": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].coverStatusRef.value",
2488+
"covers[].yearlyPremium": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].coversList.covers[].basicYearlyPremiumAmount",
2489+
"currency": "policyHeader[0].currencyRef.value",
2490+
"endDate": "policyHeader[0].policyEndDate",
2491+
"policyContacts[].contactId": "policyHeader[0].policiesList.policies.policyContactsList.policyContacts[].contactExtNum",
2492+
"policyContacts[].contactType": "policyHeader[0].policiesList.policies.policyContactsList.policyContacts[].policyContactRoleRef.value",
2493+
"policyNumber": "policyHeader[0].policiesList.policies.externalPolicyNr",
2494+
"productName": "policyHeader[0].productRef.value",
2495+
"productType": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.lobRef.value",
2496+
"productVersion": "policyHeader[0].productVersionRef.value",
2497+
"riskObjects[].address.city": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.address.cityName",
2498+
"riskObjects[].address.country": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.address.countryRef.value",
2499+
"riskObjects[].address.houseNumber": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.address.houseNr",
2500+
"riskObjects[].address.zipCode": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.address.zipCode",
2501+
"riskObjects[].assetDescription": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.propertyOccupationTypeRef",
2502+
"riskObjects[].assetId": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].originalLobAssetId",
2503+
"riskObjects[].type": "policyHeader[0].policiesList.policies.policyLobsList.policyLobs.policyLobAssetsList.policyLobAssets[].asset.assetTypeRef.value",
2504+
"startDate": "policyHeader[0].policyStartDate",
2505+
"status": "policyHeader[0].statusCodeRef.value"
2506+
};
2507+
2508+
var result = om(obj, map);
2509+
2510+
t.deepEqual(result, expect);
2511+
t.end();
2512+
});

0 commit comments

Comments
 (0)