spee.ch/server/helpers/sequelizeHelpers.js

26 lines
931 B
JavaScript
Raw Normal View History

2017-11-04 01:10:08 +01:00
module.exports = {
returnShortId: function (claimsArray, longId) {
2017-11-04 01:10:08 +01:00
let claimIndex;
let shortId = longId.substring(0, 1); // default short id is the first letter
2017-11-04 01:10:08 +01:00
let shortIdLength = 0;
// find the index of this claim id
claimIndex = claimsArray.findIndex(element => {
2017-11-04 01:10:08 +01:00
return element.claimId === longId;
});
if (claimIndex < 0) {
throw new Error('claim id not found in claims list');
}
// get an array of all claims with lower height
let possibleMatches = claimsArray.slice(0, claimIndex);
2017-11-04 01:10:08 +01:00
// remove certificates with the same prefixes until none are left.
while (possibleMatches.length > 0) {
shortIdLength += 1;
shortId = longId.substring(0, shortIdLength);
possibleMatches = possibleMatches.filter(element => {
return (element.claimId && (element.claimId.substring(0, shortIdLength) === shortId));
2017-11-04 01:10:08 +01:00
});
}
return shortId;
},
};