Add the utilCompareIDs() function

This commit is contained in:
Thomas Petillon
2020-04-11 16:26:17 +02:00
parent 23b3bc27b6
commit fab6dfa3dd
3 changed files with 60 additions and 29 deletions
+1
View File
@@ -35,6 +35,7 @@ export { utilHighlightEntities } from './util';
export { utilKeybinding } from './keybinding';
export { utilNoAuto } from './util';
export { utilObjectOmit } from './object';
export { utilCompareIDs } from './util';
export { utilOldestID } from './util';
export { utilPrefixCSSProperty } from './util';
export { utilPrefixDOMProperty } from './util';
+27 -29
View File
@@ -582,6 +582,29 @@ export function utilUnicodeCharsTruncated(str, limit) {
return Array.from(str).slice(0, limit).join('');
}
function toNumericID(id) {
var match = id.match(/^[cnwr](-?\d+)$/);
if (match) {
return parseInt(match[1], 10);
}
return NaN;
}
function compareNumericIDs(left, right) {
if (isNaN(left) && isNaN(right)) return -1;
if (isNaN(left)) return 1;
if (isNaN(right)) return -1;
if (Math.sign(left) !== Math.sign(right)) return -Math.sign(left);
if (Math.sign(left) < 0) return Math.sign(right - left);
return Math.sign(left - right);
}
// Returns -1 if the first parameter ID is older than the second,
// 1 if the second parameter is older, 0 if they are the same.
// If both IDs are test IDs, the function returns -1.
export function utilCompareIDs(left, right) {
return compareNumericIDs(toNumericID(left), toNumericID(right));
}
// Returns the chronologically oldest ID in the list.
// Database IDs (with positive numbers) before editor ones (with negative numbers).
@@ -592,40 +615,15 @@ export function utilOldestID(ids) {
return undefined;
}
var idPattern = /^[cnwr](-?)(\d+)$/;
var decomposeID = (id) => {
var res = {
id: id,
num: NaN
};
var match = id.match(idPattern);
if (match) {
res.num = parseInt(match[2], 10);
res.isNew = !!match[1];
}
return res;
};
var compareDecomposed = (left, right) => {
if (isNaN(left.num) && isNaN(right.num)) return -1;
if (isNaN(left.num)) return 1;
if (isNaN(right.num)) return -1;
if (left.isNew && !right.isNew) return 1;
if (!left.isNew && right.isNew) return -1;
return Math.sign(left.num - right.num);
};
var oldestIDIndex = 0;
var oldestID = decomposeID(ids[0]);
var oldestID = toNumericID(ids[0]);
for (var i = 1; i < ids.length; i++) {
var decomposed = decomposeID(ids[i]);
var num = toNumericID(ids[i]);
if (compareDecomposed(oldestID, decomposed) === 1) {
if (compareNumericIDs(oldestID, num) === 1) {
oldestIDIndex = i;
oldestID = decomposed;
oldestID = num;
}
}
+32
View File
@@ -226,6 +226,38 @@ describe('iD.util', function() {
});
});
describe('utilCompareIDs', function() {
it('sorts existing IDs numerically in ascending order', function() {
expect(iD.utilCompareIDs('w100', 'w200')).to.eql(-1);
expect(iD.utilCompareIDs('w100', 'w50')).to.eql(1);
expect(iD.utilCompareIDs('w100', 'w100')).to.eql(0);
});
it('sorts new IDs numerically in descending order', function() {
expect(iD.utilCompareIDs('w-100', 'w-200')).to.eql(-1);
expect(iD.utilCompareIDs('w-100', 'w-50')).to.eql(1);
expect(iD.utilCompareIDs('w-100', 'w-100')).to.eql(0);
});
it('sorts existing IDs before new IDs', function() {
expect(iD.utilCompareIDs('w-1', 'w1')).to.eql(1);
expect(iD.utilCompareIDs('w1', 'w-1')).to.eql(-1);
expect(iD.utilCompareIDs('w-100', 'w1')).to.eql(1);
expect(iD.utilCompareIDs('w100', 'w-1')).to.eql(-1);
expect(iD.utilCompareIDs('w-1', 'w100')).to.eql(1);
expect(iD.utilCompareIDs('w1', 'w-100')).to.eql(-1);
});
it('sorts existing and new IDs before anything else', function() {
expect(iD.utilCompareIDs('w1', 'asdf')).to.eql(-1);
expect(iD.utilCompareIDs('asdf', 'w1')).to.eql(1);
expect(iD.utilCompareIDs('w-1', 'asdf')).to.eql(-1);
expect(iD.utilCompareIDs('asdf', 'w-1')).to.eql(1);
});
it('returns -1 for other strings', function() {
expect(iD.utilCompareIDs('aaa', 'b')).to.eql(-1);
expect(iD.utilCompareIDs('b', 'aaa')).to.eql(-1);
expect(iD.utilCompareIDs('a', 'a')).to.eql(-1);
});
});
describe('utilDisplayName', function() {
it('returns the name if tagged with a name', function() {
expect(iD.utilDisplayName({tags: {name: 'East Coast Greenway'}})).to.eql('East Coast Greenway');