UNPKG

@ar.io/sdk

Version:

[![codecov](https://codecov.io/gh/ar-io/ar-io-sdk/graph/badge.svg?token=7dXKcT7dJy)](https://codecov.io/gh/ar-io/ar-io-sdk)

40 lines (39 loc) 1.97 kB
/** * Sorts ANT records by priority and then lexicographically. * * Note: javascript guarantees that the order of objects in an object is persistent. Still, adding index to each record is useful for enforcing against undername limits. * * Reference: https://github.com/ar-io/ar-io-node/blob/e0a9ec56559cad1b3e35d668563871afb8649913/docs/madr/003-arns-undername-limits.md * * @param antRecords - The ANT records to sort. */ export const sortANTRecords = (antRecords) => { const sortedEntries = Object.entries(antRecords).sort(([a, aRecord], [b, bRecord]) => { // '@' is the root name and should be resolved first if (a === '@') { return -1; } if (b === '@') { return 1; } // if a record has a priority, it should be resolved before any other record without a priority if ('priority' in aRecord && !('priority' in bRecord)) { return -1; } if (!('priority' in aRecord) && 'priority' in bRecord) { return 1; } // if both records have a priority, sort by priority and fallback to lexicographic sorting if (aRecord.priority !== undefined && bRecord.priority !== undefined) { if (aRecord.priority === bRecord.priority) { // use deterministic comparison instead of localeCompare to avoid locale-specific sorting return a < b ? -1 : a > b ? 1 : 0; } return aRecord.priority - bRecord.priority; } // all other records are sorted lexicographically, using deterministic comparison instead of localeCompare to avoid locale-specific sorting return a < b ? -1 : a > b ? 1 : 0; }); // now that they are sorted, add the index to each record - this is their position in the sorted list and is used to enforce undername limits return Object.fromEntries(sortedEntries.map(([a, aRecord], index) => [a, { ...aRecord, index }])); };