2021-08-24 14:15:56 +02:00
|
|
|
const Path = require('path')
|
2023-12-21 20:52:42 +01:00
|
|
|
const uuid = require('uuid')
|
2021-08-26 00:36:54 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-11-09 03:05:12 +01:00
|
|
|
const { parseString } = require("xml2js")
|
2022-03-12 02:46:32 +01:00
|
|
|
const areEquivalent = require('./areEquivalent')
|
2021-08-24 14:15:56 +02:00
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
const levenshteinDistance = (str1, str2, caseSensitive = false) => {
|
2023-05-19 00:07:58 +02:00
|
|
|
str1 = String(str1)
|
|
|
|
str2 = String(str2)
|
2021-08-21 16:15:44 +02:00
|
|
|
if (!caseSensitive) {
|
|
|
|
str1 = str1.toLowerCase()
|
|
|
|
str2 = str2.toLowerCase()
|
|
|
|
}
|
|
|
|
const track = Array(str2.length + 1).fill(null).map(() =>
|
2023-12-17 19:00:11 +01:00
|
|
|
Array(str1.length + 1).fill(null))
|
2021-08-21 16:15:44 +02:00
|
|
|
for (let i = 0; i <= str1.length; i += 1) {
|
2023-12-17 19:00:11 +01:00
|
|
|
track[0][i] = i
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
for (let j = 0; j <= str2.length; j += 1) {
|
2023-12-17 19:00:11 +01:00
|
|
|
track[j][0] = j
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
for (let j = 1; j <= str2.length; j += 1) {
|
|
|
|
for (let i = 1; i <= str1.length; i += 1) {
|
2023-12-17 19:00:11 +01:00
|
|
|
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1
|
2021-08-21 16:15:44 +02:00
|
|
|
track[j][i] = Math.min(
|
|
|
|
track[j][i - 1] + 1, // deletion
|
|
|
|
track[j - 1][i] + 1, // insertion
|
|
|
|
track[j - 1][i - 1] + indicator, // substitution
|
2023-12-17 19:00:11 +01:00
|
|
|
)
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-17 19:00:11 +01:00
|
|
|
return track[str2.length][str1.length]
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
2021-08-22 15:52:37 +02:00
|
|
|
module.exports.levenshteinDistance = levenshteinDistance
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
module.exports.isObject = (val) => {
|
|
|
|
return val !== null && typeof val === 'object'
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
module.exports.comparePaths = (path1, path2) => {
|
|
|
|
return path1 === path2 || Path.normalize(path1) === Path.normalize(path2)
|
2021-08-24 14:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
module.exports.isNullOrNaN = (num) => {
|
|
|
|
return num === null || isNaN(num)
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
const xmlToJSON = (xml) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
parseString(xml, (err, results) => {
|
|
|
|
if (err) {
|
|
|
|
Logger.error(`[xmlToJSON] Error`, err)
|
|
|
|
resolve(null)
|
|
|
|
} else {
|
|
|
|
resolve(results)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
module.exports.xmlToJSON = xmlToJSON
|
2021-11-16 03:09:42 +01:00
|
|
|
|
|
|
|
module.exports.getId = (prepend = '') => {
|
|
|
|
var _id = Math.random().toString(36).substring(2, 8) + Math.random().toString(36).substring(2, 8) + Math.random().toString(36).substring(2, 8)
|
|
|
|
if (prepend) return prepend + '_' + _id
|
|
|
|
return _id
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function elapsedPretty(seconds) {
|
2023-10-21 00:46:18 +02:00
|
|
|
if (seconds > 0 && seconds < 1) {
|
|
|
|
return `${Math.floor(seconds * 1000)} ms`
|
|
|
|
}
|
2022-05-12 00:20:32 +02:00
|
|
|
if (seconds < 60) {
|
|
|
|
return `${Math.floor(seconds)} sec`
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
if (minutes < 70) {
|
|
|
|
return `${minutes} min`
|
|
|
|
}
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
if (!minutes) {
|
|
|
|
return `${hours} hr`
|
|
|
|
}
|
|
|
|
return `${hours} hr ${minutes} min`
|
|
|
|
}
|
|
|
|
module.exports.elapsedPretty = elapsedPretty
|
|
|
|
|
2022-09-25 22:56:06 +02:00
|
|
|
function secondsToTimestamp(seconds, includeMs = false, alwaysIncludeHours = false) {
|
2021-11-25 03:15:50 +01:00
|
|
|
var _seconds = seconds
|
|
|
|
var _minutes = Math.floor(seconds / 60)
|
|
|
|
_seconds -= _minutes * 60
|
|
|
|
var _hours = Math.floor(_minutes / 60)
|
|
|
|
_minutes -= _hours * 60
|
|
|
|
|
|
|
|
var ms = _seconds - Math.floor(seconds)
|
|
|
|
_seconds = Math.floor(_seconds)
|
|
|
|
|
|
|
|
var msString = '.' + (includeMs ? ms.toFixed(3) : '0.0').split('.')[1]
|
2022-09-25 22:56:06 +02:00
|
|
|
if (alwaysIncludeHours) {
|
|
|
|
return `${_hours.toString().padStart(2, '0')}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}${msString}`
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
if (!_hours) {
|
|
|
|
return `${_minutes}:${_seconds.toString().padStart(2, '0')}${msString}`
|
|
|
|
}
|
|
|
|
return `${_hours}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}${msString}`
|
|
|
|
}
|
|
|
|
module.exports.secondsToTimestamp = secondsToTimestamp
|
|
|
|
|
2021-12-13 00:15:37 +01:00
|
|
|
module.exports.reqSupportsWebp = (req) => {
|
|
|
|
if (!req || !req.headers || !req.headers.accept) return false
|
|
|
|
return req.headers.accept.includes('image/webp') || req.headers.accept === '*/*'
|
2022-03-12 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.areEquivalent = areEquivalent
|
|
|
|
|
|
|
|
module.exports.copyValue = (val) => {
|
2024-02-28 00:17:33 +01:00
|
|
|
if (val === undefined || val === '') return null
|
|
|
|
else if (!val) return val
|
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
if (!this.isObject(val)) return val
|
|
|
|
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
return val.map(this.copyValue)
|
|
|
|
} else {
|
|
|
|
var final = {}
|
|
|
|
for (const key in val) {
|
|
|
|
final[key] = this.copyValue(val[key])
|
|
|
|
}
|
|
|
|
return final
|
|
|
|
}
|
2022-03-27 00:23:33 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:52:37 +02:00
|
|
|
module.exports.toNumber = (val, fallback = 0) => {
|
|
|
|
if (isNaN(val) || val === null) return fallback
|
|
|
|
return Number(val)
|
2022-06-26 22:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.cleanStringForSearch = (str) => {
|
|
|
|
if (!str) return ''
|
|
|
|
// Remove ' . ` " ,
|
|
|
|
return str.toLowerCase().replace(/[\'\.\`\",]/g, '').trim()
|
2022-07-30 23:18:26 +02:00
|
|
|
}
|
|
|
|
|
2022-11-03 05:14:07 +01:00
|
|
|
const getTitleParts = (title) => {
|
|
|
|
if (!title) return ['', null]
|
2023-07-29 01:03:31 +02:00
|
|
|
const prefixesToIgnore = global.ServerSettings.sortingPrefixes || []
|
2022-07-30 23:18:26 +02:00
|
|
|
for (const prefix of prefixesToIgnore) {
|
|
|
|
// e.g. for prefix "the". If title is "The Book" return "Book, The"
|
|
|
|
if (title.toLowerCase().startsWith(`${prefix} `)) {
|
2022-11-03 05:14:07 +01:00
|
|
|
return [title.substr(prefix.length + 1), `${prefix.substr(0, 1).toUpperCase() + prefix.substr(1)}`]
|
2022-07-30 23:18:26 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 05:14:07 +01:00
|
|
|
return [title, null]
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:01:17 +02:00
|
|
|
/**
|
|
|
|
* Remove sortingPrefixes from title
|
|
|
|
* @example "The Good Book" => "Good Book"
|
|
|
|
* @param {string} title
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-11-03 05:14:07 +01:00
|
|
|
module.exports.getTitleIgnorePrefix = (title) => {
|
|
|
|
return getTitleParts(title)[0]
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:01:17 +02:00
|
|
|
/**
|
|
|
|
* Put sorting prefix at the end of title
|
|
|
|
* @example "The Good Book" => "Good Book, The"
|
|
|
|
* @param {string} title
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-11-03 05:14:07 +01:00
|
|
|
module.exports.getTitlePrefixAtEnd = (title) => {
|
|
|
|
let [sort, prefix] = getTitleParts(title)
|
|
|
|
return prefix ? `${sort}, ${prefix}` : title
|
2023-10-06 00:00:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* to lower case for only ascii characters
|
|
|
|
* used to handle sqlite that doesnt support unicode lower
|
|
|
|
* @see https://github.com/advplyr/audiobookshelf/issues/2187
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
module.exports.asciiOnlyToLowerCase = (str) => {
|
|
|
|
if (!str) return ''
|
|
|
|
|
|
|
|
let temp = ''
|
|
|
|
for (let chars of str) {
|
|
|
|
let value = chars.charCodeAt()
|
|
|
|
if (value >= 65 && value <= 90) {
|
|
|
|
temp += String.fromCharCode(value + 32)
|
|
|
|
} else {
|
|
|
|
temp += chars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return temp
|
2023-10-30 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape string used in RegExp
|
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
module.exports.escapeRegExp = (str) => {
|
|
|
|
if (typeof str !== 'string') return ''
|
|
|
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
2023-12-17 19:00:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate url string with URL class
|
|
|
|
*
|
|
|
|
* @param {string} rawUrl
|
|
|
|
* @returns {string} null if invalid
|
|
|
|
*/
|
|
|
|
module.exports.validateUrl = (rawUrl) => {
|
|
|
|
if (!rawUrl || typeof rawUrl !== 'string') return null
|
|
|
|
try {
|
|
|
|
return new URL(rawUrl).toString()
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`Invalid URL "${rawUrl}"`, error)
|
|
|
|
return null
|
|
|
|
}
|
2023-12-21 20:52:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a string is a valid UUID
|
|
|
|
*
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
module.exports.isUUID = (str) => {
|
|
|
|
if (!str || typeof str !== 'string') return false
|
|
|
|
return uuid.validate(str)
|
2021-12-13 00:15:37 +01:00
|
|
|
}
|