added function to trim redundant {,[,( symbols if there is no closing brackets

This commit is contained in:
Daniel Shleifman
2021-06-16 16:57:05 +03:00
parent d56fe89ac4
commit 6841fb2521
9 changed files with 65 additions and 33 deletions

View File

@@ -10,7 +10,6 @@ export const settingsHost = () => `${torrserverHost}/settings`
export const streamHost = () => `${torrserverHost}/stream`
export const shutdownHost = () => `${torrserverHost}/shutdown`
export const echoHost = () => `${torrserverHost}/echo`
export const playlistAllHost = () => `${torrserverHost}/playlistall/all.m3u`
export const playlistTorrHost = () => `${torrserverHost}/stream`
export const getTorrServerHost = () => torrserverHost

View File

@@ -11,3 +11,33 @@ export function getPeerString(torrent) {
export const shortenText = (text, sympolAmount) =>
text ? text.slice(0, sympolAmount) + (text.length > sympolAmount ? '…' : '') : ''
export const removeRedundantCharacters = string => {
let newString = string
const brackets = [
['(', ')'],
['[', ']'],
['{', '}'],
]
brackets.forEach(el => {
const leftBracketRegexFormula = `\\${el[0]}`
const leftBracketRegex = new RegExp(leftBracketRegexFormula, 'g')
const leftBracketAmount = [...newString.matchAll(leftBracketRegex)].length
const rightBracketRegexFormula = `\\${el[1]}`
const rightBracketRegex = new RegExp(rightBracketRegexFormula, 'g')
const rightBracketAmount = [...newString.matchAll(rightBracketRegex)].length
if (leftBracketAmount !== rightBracketAmount) {
const removeFormula = `(\\${el[0]})(?!.*\\1).*`
const removeRegex = new RegExp(removeFormula, 'g')
newString = newString.replace(removeRegex, '')
}
})
const hasThreeDotsAtTheEnd = !!newString.match(/\.{3}$/g)
const trimmedString = newString.replace(/[\\.| ]+$/g, '').trim()
return hasThreeDotsAtTheEnd ? `${trimmedString}..` : trimmedString
}