As you can see below , I have a query that checks if the filename already exist and just add a timestamp to make it unique , now the timestamp is too long , I supposed wanted to use ordinal suffix instead of timestamp. How do we add an oridinal suffix everytime an existing file is found ?
Something like hellofilename-1st , hellofileaname-2nd. etc
Btw filename is the column in the database.
#CODE
// check if filename already exists
const file = await context.service.Model.findOne({
where: { humanId: record.id, filename: data.filename },
paranoid: false,
});
if (file) {
const prefix = Date.now().toString();
// eslint-disable-next-line no-undef
const fileParts = data.filename.split('.');
filename = `${fileParts[0]}-${prefix}.${fileParts[1]}`;
}
#code 2
function ordinal_suffix_of(i) {
var j = i % 10,
k = i % 100;
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}