7digital-api
Advanced tools
Comparing version 0.25.0 to 0.25.1
@@ -32,8 +32,10 @@ 'use strict'; | ||
'taggedResults.taggedItem', | ||
'cardTypes.cardType' | ||
'cardTypes.cardType', | ||
//NB: the outermost item to be arrayified must come first | ||
'locker.lockerReleases.lockerRelease', | ||
'locker.lockerReleases.lockerRelease.lockerTracks.lockerTrack', | ||
'release.formats.format', | ||
'list.listItems.listItem', | ||
'basket.basketItems.basketItem' | ||
]).each(function checkLength(item) { | ||
var parts = item.split('.'), | ||
parent = parts[0], | ||
child = parts[1]; | ||
// e.g. an artist/byTag/top with only one result then: | ||
@@ -44,31 +46,27 @@ // typeof response.taggedResults.taggedItem === 'object' | ||
// We want these to always be arrays for convenience | ||
if (response[parent] && response[parent][child] && | ||
!response[parent][child].length) { | ||
response[parent][child] = [response[parent][child]]; | ||
} | ||
}); | ||
if (response.release && response.release.formats | ||
&& response.release.formats.format) { | ||
formats = response.release.formats.format; | ||
response.release.formats.format = arrayify(formats); | ||
} | ||
var parts = item.split('.'); | ||
var allPartsButLast = _.initial(parts); | ||
var lastPart = _.last(parts); | ||
if (response.list && response.list.listItems | ||
&& response.list.listItems.listItem) { | ||
listItems = response.list.listItems.listItem; | ||
response.list.listItems.listItem = arrayify(listItems); | ||
} | ||
var parents = _.reduce(allPartsButLast, function (chain, part) { | ||
return chain.pluck(part).compact().flatten(); | ||
}, _([response])).value(); | ||
// basket is a special case: we want to fold basketItem into basketItems | ||
// which is a redundant wrapper. | ||
if (response.basket && response.basket.basketItems && | ||
response.basket.basketItems.basketItem) { | ||
parents.map(function (parent) { | ||
if (parent[lastPart]) { | ||
parent[lastPart] = arrayify(parent[lastPart]); | ||
} else { | ||
parent[lastPart] = []; | ||
} | ||
}); | ||
}); | ||
items = response.basket.basketItems.basketItem; | ||
items = arrayify(items); | ||
response.basket.basketItems = items; | ||
} else if (response.basket && response.basket.basketItems !== undefined && | ||
!_.isArray(response.basket.basketItems)) { | ||
response.basket.basketItems = []; | ||
var basket = response.basket; | ||
if (basket) { | ||
if (basket.basketItems === '') { | ||
basket.basketItems = []; | ||
} else if (basket.basketItems.basketItem) { | ||
basket.basketItems = basket.basketItems.basketItem; | ||
} | ||
} | ||
@@ -75,0 +73,0 @@ |
{ | ||
"name": "7digital-api", | ||
"version": "0.25.0", | ||
"version": "0.25.1", | ||
"description": "7digital API client for nodeJS", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/raoulmillais/node-7digital-api", |
@@ -90,2 +90,49 @@ 'use strict'; | ||
it('normalises single locker responses into an array', function () { | ||
var response, callbackSpy = sinon.spy(); | ||
var xml = fs.readFileSync(path.join(__dirname + | ||
'/responses/locker-single-release-and-track.xml'), 'utf8'); | ||
parser.parse(xml, createOptsWithFormat('js'), callbackSpy); | ||
assert(callbackSpy.calledOnce); | ||
response = callbackSpy.lastCall.args[1]; | ||
var lockerReleaseArray = response.locker.lockerReleases.lockerRelease; | ||
assert.instanceOf(lockerReleaseArray, Array); | ||
var lockerTrackArray = lockerReleaseArray[0].lockerTracks.lockerTrack; | ||
assert.instanceOf(lockerTrackArray, Array); | ||
}); | ||
it('normalises release formats', function () { | ||
var response, callbackSpy = sinon.spy(); | ||
var xml = fs.readFileSync(path.join(__dirname + | ||
'/responses/release-single-format.xml'), 'utf8'); | ||
parser.parse(xml, createOptsWithFormat('js'), callbackSpy); | ||
assert(callbackSpy.calledOnce); | ||
response = callbackSpy.lastCall.args[1]; | ||
assert.instanceOf(response.release.formats.format, Array); | ||
}); | ||
it('normalises editorial list items', function () { | ||
var response, callbackSpy = sinon.spy(); | ||
var xml = fs.readFileSync(path.join(__dirname + | ||
'/responses/list-single.xml'), 'utf8'); | ||
parser.parse(xml, createOptsWithFormat('js'), callbackSpy); | ||
assert(callbackSpy.calledOnce); | ||
response = callbackSpy.lastCall.args[1]; | ||
assert.instanceOf(response.list.listItems.listItem, Array); | ||
}); | ||
it('leaves list items alone if they are already arrays', function () { | ||
var response, callbackSpy = sinon.spy(); | ||
var xml = fs.readFileSync(path.join(__dirname + | ||
'/responses/list-multiple.xml'), 'utf8'); | ||
parser.parse(xml, createOptsWithFormat('js'), callbackSpy); | ||
assert(callbackSpy.calledOnce); | ||
response = callbackSpy.lastCall.args[1]; | ||
assert.instanceOf(response.list.listItems.listItem, Array); | ||
}); | ||
// Note that basket items are one level deeper than other arrays, hence | ||
@@ -92,0 +139,0 @@ // the separate test. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148717
65
2615