Skip to content

Commit

Permalink
update sort method
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Sep 12, 2024
1 parent b423c3c commit 13a17b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ 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
return function (a, b) {
// equal items sort equally
if (a[property]?.[0] === b[property]?.[0]) {
return 0
}
// nulls sort after anything else
if (!a[property]?.[0]) {
return 1
}
if (!b[property]?.[0]) {
return -1
}
return a[property]?.[0] > b[property]?.[0] ? -1 : 1
}
}

exports.buildJsonLdContext = function (prefixes) {
Expand Down
2 changes: 1 addition & 1 deletion test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mangledEnumerationChronologyItems = require('./fixtures/mangled_enumeratio
const util = require('../lib/util')

describe('Util', function () {
describe.only('sortOnPropWithUndefinedLast', () => {
describe('sortOnPropWithUndefinedLast', () => {
it('sorts undefined last', () => {
const sortedItemEnums = mangledEnumerationChronologyItems
.sort(util.sortOnPropWithUndefinedLast('enumerationChronology_sort'))
Expand Down

0 comments on commit 13a17b8

Please sign in to comment.