Skip to content

Commit

Permalink
Makes it so the when sorting and ignoring prefixes, they are actually…
Browse files Browse the repository at this point in the history
… ignored
  • Loading branch information
ruoti committed Nov 3, 2022
1 parent c6e9fe6 commit 473257f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
2 changes: 1 addition & 1 deletion server/controllers/LibraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class LibraryController {
} else if (payload.sortBy === 'addedAt') {
return se.addedAt
} else { // sort by name
return this.db.serverSettings.sortingIgnorePrefix ? se.nameIgnorePrefix : se.name
return this.db.serverSettings.sortingIgnorePrefix ? se.nameIgnorePrefixSort : se.name
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions server/objects/metadata/BookMetadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Logger = require('../../Logger')
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix } = require('../../utils/index')
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
const parseNameString = require('../../utils/parsers/parseNameString')
class BookMetadata {
constructor(metadata) {
Expand Down Expand Up @@ -62,7 +62,7 @@ class BookMetadata {
toJSONMinified() {
return {
title: this.title,
titleIgnorePrefix: this.titleIgnorePrefix,
titleIgnorePrefix: this.titlePrefixAtEnd,
subtitle: this.subtitle,
authorName: this.authorName,
authorNameLF: this.authorNameLF,
Expand All @@ -83,7 +83,7 @@ class BookMetadata {
toJSONExpanded() {
return {
title: this.title,
titleIgnorePrefix: this.titleIgnorePrefix,
titleIgnorePrefix: this.titlePrefixAtEnd,
subtitle: this.subtitle,
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
narrators: [...this.narrators],
Expand Down Expand Up @@ -111,6 +111,9 @@ class BookMetadata {
get titleIgnorePrefix() {
return getTitleIgnorePrefix(this.title)
}
get titlePrefixAtEnd() {
return getTitlePrefixAtEnd(this.title)
}
get authorName() {
if (!this.authors.length) return ''
return this.authors.map(au => au.name).join(', ')
Expand All @@ -133,6 +136,13 @@ class BookMetadata {
return `${getTitleIgnorePrefix(se.name)} #${se.sequence}`
}).join(', ')
}
get seriesNamePrefixAtEnd() {
if (!this.series.length) return ''
return this.series.map(se => {
if (!se.sequence) return getTitlePrefixAtEnd(se.name)
return `${getTitlePrefixAtEnd(se.name)} #${se.sequence}`
}).join(', ')
}
get firstSeriesName() {
if (!this.series.length) return ''
return this.series[0].name
Expand Down
18 changes: 7 additions & 11 deletions server/objects/metadata/PodcastMetadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Logger = require('../../Logger')
const { areEquivalent, copyValue, cleanStringForSearch } = require('../../utils/index')
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')

class PodcastMetadata {
constructor(metadata) {
Expand Down Expand Up @@ -56,7 +56,7 @@ class PodcastMetadata {
toJSONMinified() {
return {
title: this.title,
titleIgnorePrefix: this.titleIgnorePrefix,
titleIgnorePrefix: this.titlePrefixAtEnd,
author: this.author,
description: this.description,
releaseDate: this.releaseDate,
Expand All @@ -80,15 +80,11 @@ class PodcastMetadata {
}

get titleIgnorePrefix() {
if (!this.title) return ''
var prefixesToIgnore = global.ServerSettings.sortingPrefixes || []
for (const prefix of prefixesToIgnore) {
// e.g. for prefix "the". If title is "The Book Title" return "Book Title, The"
if (this.title.toLowerCase().startsWith(`${prefix} `)) {
return this.title.substr(prefix.length + 1) + `, ${prefix.substr(0, 1).toUpperCase() + prefix.substr(1)}`
}
}
return this.title
return getTitleIgnorePrefix(this.title)
}

get titlePrefixAtEnd() {
return getTitlePrefixAtEnd(this.title)
}

searchQuery(query) { // Returns key if match is found
Expand Down
18 changes: 7 additions & 11 deletions server/objects/metadata/VideoMetadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Logger = require('../../Logger')
const { areEquivalent, copyValue } = require('../../utils/index')
const { areEquivalent, copyValue, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')

class VideoMetadata {
constructor(metadata) {
Expand Down Expand Up @@ -32,7 +32,7 @@ class VideoMetadata {
toJSONMinified() {
return {
title: this.title,
titleIgnorePrefix: this.titleIgnorePrefix,
titleIgnorePrefix: this.titlePrefixAtEnd,
description: this.description,
explicit: this.explicit,
language: this.language
Expand All @@ -48,15 +48,11 @@ class VideoMetadata {
}

get titleIgnorePrefix() {
if (!this.title) return ''
var prefixesToIgnore = global.ServerSettings.sortingPrefixes || []
for (const prefix of prefixesToIgnore) {
// e.g. for prefix "the". If title is "The Book Title" return "Book Title, The"
if (this.title.toLowerCase().startsWith(`${prefix} `)) {
return this.title.substr(prefix.length + 1) + `, ${prefix.substr(0, 1).toUpperCase() + prefix.substr(1)}`
}
}
return this.title
return getTitleIgnorePrefix(this.title)
}

get titlePrefixAtEnd() {
return getTitlePrefixAtEnd(this.title)
}

searchQuery(query) { // Returns key if match is found
Expand Down
18 changes: 14 additions & 4 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,24 @@ module.exports.cleanStringForSearch = (str) => {
return str.toLowerCase().replace(/[\'\.\`\",]/g, '').trim()
}

module.exports.getTitleIgnorePrefix = (title) => {
if (!title) return ''
const getTitleParts = (title) => {
if (!title) return ['', null]
var prefixesToIgnore = global.ServerSettings.sortingPrefixes || []
prefixes = []
for (const prefix of prefixesToIgnore) {
// e.g. for prefix "the". If title is "The Book" return "Book, The"
if (title.toLowerCase().startsWith(`${prefix} `)) {
return title.substr(prefix.length + 1) + `, ${prefix.substr(0, 1).toUpperCase() + prefix.substr(1)}`
return [title.substr(prefix.length + 1), `${prefix.substr(0, 1).toUpperCase() + prefix.substr(1)}`]
}
}
return title
return [title, null]
}

module.exports.getTitleIgnorePrefix = (title) => {
return getTitleParts(title)[0]
}

module.exports.getTitlePrefixAtEnd = (title) => {
let [sort, prefix] = getTitleParts(title)
return prefix ? `${sort}, ${prefix}` : title
}
5 changes: 3 additions & 2 deletions server/utils/libraryHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { sort, createNewSortInstance } = require('../libs/fastSort')
const { getTitleIgnorePrefix, isNullOrNaN } = require('../utils/index')
const { getTitlePrefixAtEnd, isNullOrNaN, getTitleIgnorePrefix } = require('../utils/index')
const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
})
Expand Down Expand Up @@ -183,7 +183,8 @@ module.exports = {
_series[bookSeriesObj.id] = {
id: bookSeriesObj.id,
name: bookSeriesObj.name,
nameIgnorePrefix: getTitleIgnorePrefix(bookSeriesObj.name),
nameIgnorePrefix: getTitlePrefixAtEnd(bookSeriesObj.name),
nameIgnorePrefixSort: getTitleIgnorePrefix(bookSeriesObj.name),
type: 'series',
books: [abJson],
addedAt: series ? series.addedAt : 0,
Expand Down

0 comments on commit 473257f

Please sign in to comment.