Skip to content

Commit

Permalink
add sort method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Sep 11, 2024
1 parent d8ca73a commit b423c3c
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 4 deletions.
Binary file modified .DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/response_massager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const LocationLabelUpdater = require('./location_label_updater')
const AvailabilityResolver = require('./availability_resolver.js')
const parallelFieldsExtractor = require('./parallel-fields-extractor')
const { isAeonUrl } = require('../lib/util')
const { isAeonUrl, sortOnPropWithUndefinedLast } = require('../lib/util')
const FulfillmentResolver = require('./fulfillment_resolver')
const RequestabilityResolver = require('./requestability_resolver')

Expand All @@ -22,10 +22,10 @@ class ResponseMassager {
processInnerHitsProperties (response, sortOnEnumerationChronology) {
response.hits.hits.forEach((hit) => {
if (sortOnEnumerationChronology) {
hit._source.items.sort((a, b) => a.enumerationChronology_sort[0] > b.enumerationChronology_sort[0] ? -1 : 1)
hit._source.items.sort(sortOnPropWithUndefinedLast('enumerationChronology_sort'))
}
// Process "items" inner_hits
if (hit.inner_hits && hit.inner_hits.items) {
if (hit?.inner_hits?.items) {
// Reassign items inner_hits to .items
hit._source.items = hit.inner_hits.items.hits.hits.map((itemHit) => itemHit._source)

Expand All @@ -40,7 +40,7 @@ class ResponseMassager {
hit._source.numItemsTotal = [unfilteredItems.hits.total]
}
// Process "electronicResources" inner_hits
if (hit.inner_hits && hit.inner_hits.electronicResources) {
if (hit?.inner_hits?.electronicResources) {
// If record doesn't have indexed electronicResources...
if (!hit._source.electronicResources || hit._source.electronicResources.length === 0) {
// Gather up all records in the electronicResources inner_hit,
Expand Down
4 changes: 4 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const logger = require('./logger')
const isItemNyplOwned = require('./ownership_determination').isItemNyplOwned

exports.sortOnPropWithUndefinedLast = (property) => {
return (a, b) => a[property]?.[0] || Number.MAX_SAFE_INTEGER > b[property]?.[0] || Number.MAX_SAFE_INTEGER ? -1 : 1
}

exports.buildJsonLdContext = function (prefixes) {
const context = JSON.parse(JSON.stringify(prefixes))
delete context.urn
Expand Down
Binary file added test/.DS_Store
Binary file not shown.
Loading

0 comments on commit b423c3c

Please sign in to comment.