reuse getSortValue

This commit is contained in:
j 2024-10-13 17:10:11 +01:00
parent c69ca372ee
commit f4bfe9294b
2 changed files with 31 additions and 36 deletions

View file

@ -1,35 +1,4 @@
const getSortValue = function(value) {
var sortValue = value;
function trim(value) {
return value.replace(/^\W+(?=\w)/, '');
}
if (
isEmpty(value)
|| isNull(value)
|| isUndefined(value)
) {
sortValue = null;
} else if (isString(value)) {
// make lowercase and remove leading non-word characters
sortValue = trim(value.toLowerCase());
// move leading articles to the end
// and remove leading non-word characters
['a', 'an', 'the'].forEach(function(article) {
if (new RegExp('^' + article + ' ').test(sortValue)) {
sortValue = trim(sortValue.slice(article.length + 1))
+ ', ' + sortValue.slice(0, article.length);
return false; // break
}
});
// remove thousand separators and pad numbers
sortValue = sortValue.replace(/(\d),(?=(\d{3}))/g, '$1')
.replace(/\d+/g, function(match) {
return match.padStart(64, '0')
});
}
return sortValue;
};
const sortByKey = function(array, by) { const sortByKey = function(array, by) {
return array.sort(function(a, b) { return array.sort(function(a, b) {

View file

@ -161,11 +161,37 @@ const getVideoURL = function(id, resolution, part, track, streamId) {
return prefix + '/' + getVideoURLName(id, resolution, part, track, streamId); return prefix + '/' + getVideoURLName(id, resolution, part, track, streamId);
}; };
function getSortValue(value) { const getSortValue = function(value) {
var getSortValue = Ox.cache(function getSortValue(value) {
var sortValue = value; var sortValue = value;
return sortValue; function trim(value) {
return value.replace(/^\W+(?=\w)/, '');
} }
if (
isEmpty(value)
|| isNull(value)
|| isUndefined(value)
) {
sortValue = null;
} else if (isString(value)) {
// make lowercase and remove leading non-word characters
sortValue = trim(value.toLowerCase());
// move leading articles to the end
// and remove leading non-word characters
['a', 'an', 'the'].forEach(function(article) {
if (new RegExp('^' + article + ' ').test(sortValue)) {
sortValue = trim(sortValue.slice(article.length + 1))
+ ', ' + sortValue.slice(0, article.length);
return false; // break
}
});
// remove thousand separators and pad numbers
sortValue = sortValue.replace(/(\d),(?=(\d{3}))/g, '$1')
.replace(/\d+/g, function(match) {
return match.padStart(64, '0')
});
}
return sortValue;
};
function sortBy(array, by, map) { function sortBy(array, by, map) {
return array.sort(function(a, b) { return array.sort(function(a, b) {