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

@@ -8,7 +8,8 @@
> `http://192.168.78.4:8090` - correct > `http://192.168.78.4:8090` - correct
> >
> `http://192.168.78.4:8090/` - wrong > `http://192.168.78.4:8090/` - wrong
3. `yarn start` 3. in `.env` file add TMDB api key
4. `yarn start`
### Eslint ### Eslint
> Prettier will fix the code every time the code is saved > Prettier will fix the code every time the code is saved

View File

@@ -1,9 +1,8 @@
import { playlistAllHost } from 'utils/Hosts'
import Divider from '@material-ui/core/Divider' import Divider from '@material-ui/core/Divider'
import ListItem from '@material-ui/core/ListItem' import ListItem from '@material-ui/core/ListItem'
import ListItemIcon from '@material-ui/core/ListItemIcon' import ListItemIcon from '@material-ui/core/ListItemIcon'
import ListItemText from '@material-ui/core/ListItemText' import ListItemText from '@material-ui/core/ListItemText'
import { CreditCard as CreditCardIcon, List as ListIcon, Language as LanguageIcon } from '@material-ui/icons' import { CreditCard as CreditCardIcon, Language as LanguageIcon } from '@material-ui/icons'
import List from '@material-ui/core/List' import List from '@material-ui/core/List'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import useChangeLanguage from 'utils/useChangeLanguage' import useChangeLanguage from 'utils/useChangeLanguage'
@@ -24,12 +23,6 @@ export default function Sidebar({ isDrawerOpen, setIsDonationDialogOpen }) {
<List> <List>
<AddDialogButton /> <AddDialogButton />
<RemoveAll /> <RemoveAll />
<ListItem button component='a' target='_blank' href={playlistAllHost()}>
<ListItemIcon>
<ListIcon />
</ListItemIcon>
<ListItemText primary={t('PlaylistAll')} />
</ListItem>
</List> </List>
<Divider /> <Divider />
@@ -37,20 +30,25 @@ export default function Sidebar({ isDrawerOpen, setIsDonationDialogOpen }) {
<List> <List>
<SettingsDialog /> <SettingsDialog />
<ListItem button onClick={() => (currentLang === 'en' ? changeLang('ru') : changeLang('en'))}>
<ListItemIcon>
<LanguageIcon />
</ListItemIcon>
<ListItemText primary={t('ChooseLanguage')} />
</ListItem>
<AboutDialog />
<CloseServer /> <CloseServer />
</List> </List>
<Divider /> <Divider />
<List> <List>
<ListItem button onClick={() => (currentLang === 'en' ? changeLang('ru') : changeLang('en'))}>
<ListItemIcon>
<LanguageIcon />
</ListItemIcon>
<ListItemText primary={t('ChooseLanguage')} />
</ListItem>
</List>
<Divider />
<List>
<AboutDialog />
<ListItem button onClick={() => setIsDonationDialogOpen(true)}> <ListItem button onClick={() => setIsDonationDialogOpen(true)}>
<ListItemIcon> <ListItemIcon>
<CreditCardIcon /> <CreditCardIcon />

View File

@@ -1,5 +1,5 @@
import { NoImageIcon } from 'icons' import { NoImageIcon } from 'icons'
import { humanizeSize } from 'utils/Utils' import { humanizeSize, removeRedundantCharacters } from 'utils/Utils'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { Button, ButtonGroup } from '@material-ui/core' import { Button, ButtonGroup } from '@material-ui/core'
import ptt from 'parse-torrent-title' import ptt from 'parse-torrent-title'
@@ -102,21 +102,27 @@ export default function DialogTorrentDetailsContent({ closeDialog, torrent }) {
const bufferSize = settings?.PreloadBuffer ? Capacity : 33554432 // Default is 32mb if PreloadBuffer is false const bufferSize = settings?.PreloadBuffer ? Capacity : 33554432 // Default is 32mb if PreloadBuffer is false
const getParsedTitle = () => { const getParsedTitle = () => {
const newNameStrings = [] const newNameStringArr = []
const torrentParsedName = name && ptt.parse(name) const torrentParsedName = name && ptt.parse(name)
if (title !== name) { if (title !== name) {
newNameStrings.push(title) newNameStringArr.push(removeRedundantCharacters(title))
} else if (torrentParsedName?.title) newNameStrings.push(torrentParsedName?.title) } else if (torrentParsedName?.title) newNameStringArr.push(torrentParsedName?.title)
// These 2 checks are needed to get year and resolution from torrent name if title does not have this info // These 2 checks are needed to get year and resolution from torrent name if title does not have this info
if (torrentParsedName?.year && !title.includes(torrentParsedName?.year)) if (torrentParsedName?.year && !title.includes(torrentParsedName?.year))
newNameStrings.push(torrentParsedName?.year) newNameStringArr.push(torrentParsedName?.year)
if (torrentParsedName?.resolution && !title.includes(torrentParsedName?.resolution)) if (torrentParsedName?.resolution && !title.includes(torrentParsedName?.resolution))
newNameStrings.push(torrentParsedName?.resolution) newNameStringArr.push(torrentParsedName?.resolution)
return newNameStrings.join('. ') const newNameString = newNameStringArr.join('. ')
// removeRedundantCharacters is returning ".." if it was "..."
const lastDotShouldBeAdded =
newNameString[newNameString.length - 1] === '.' && newNameString[newNameString.length - 2] === '.'
return lastDotShouldBeAdded ? `${newNameString}.` : newNameString
} }
return ( return (

View File

@@ -6,7 +6,7 @@ import {
Close as CloseIcon, Close as CloseIcon,
Delete as DeleteIcon, Delete as DeleteIcon,
} from '@material-ui/icons' } from '@material-ui/icons'
import { getPeerString, humanizeSize } from 'utils/Utils' import { getPeerString, humanizeSize, removeRedundantCharacters } from 'utils/Utils'
import { torrentsHost } from 'utils/Hosts' import { torrentsHost } from 'utils/Hosts'
import { NoImageIcon } from 'icons' import { NoImageIcon } from 'icons'
import DialogTorrentDetailsContent from 'components/DialogTorrentDetailsContent' import DialogTorrentDetailsContent from 'components/DialogTorrentDetailsContent'
@@ -45,7 +45,7 @@ const Torrent = ({ torrent }) => {
const titleStrings = [] const titleStrings = []
let parsedTitle = parse('title') let parsedTitle = removeRedundantCharacters(parse('title'))
const parsedYear = parse('year') const parsedYear = parse('year')
const parsedResolution = parse('resolution') const parsedResolution = parse('resolution')
if (parsedTitle) titleStrings.push(parsedTitle) if (parsedTitle) titleStrings.push(parsedTitle)

View File

@@ -17,7 +17,7 @@
"Cache": "Cache", "Cache": "Cache",
"CacheSize": "Cache Size (Megabytes)", "CacheSize": "Cache Size (Megabytes)",
"Cancel": "Cancel", "Cancel": "Cancel",
"ChooseLanguage": "Russian", "ChooseLanguage": "Ru",
"Clear": "Clear", "Clear": "Clear",
"Close": "Close", "Close": "Close",
"CloseServer?": "Do you want to turn off server?", "CloseServer?": "Do you want to turn off server?",
@@ -59,12 +59,11 @@
"Offline": "Offline", "Offline": "Offline",
"OK": "OK", "OK": "OK",
"OpenLink": "Open link", "OpenLink": "Open link",
"Peers": "[Connected] Peers", "Peers": "Peers",
"PeersListenPort": "Peers Listen Port", "PeersListenPort": "Peers Listen Port",
"PEX": "PEX (Peer Exchange)", "PEX": "PEX (Peer Exchange)",
"PiecesCount": "Pieces count", "PiecesCount": "Pieces count",
"PiecesLength": "Pieces length", "PiecesLength": "Pieces length",
"PlaylistAll": "Playlist All",
"Preload": "Preload", "Preload": "Preload",
"PreloadBuffer": "Preload Buffer", "PreloadBuffer": "Preload Buffer",
"ReaderReadAHead": "Reader Read Ahead (5-100%)", "ReaderReadAHead": "Reader Read Ahead (5-100%)",

View File

@@ -17,7 +17,7 @@
"Cache": "Кеш", "Cache": "Кеш",
"CacheSize": "Размер кеша (Мегабайты)", "CacheSize": "Размер кеша (Мегабайты)",
"Cancel": "Отмена", "Cancel": "Отмена",
"ChooseLanguage": "Английский", "ChooseLanguage": "En",
"Clear": "Очистить", "Clear": "Очистить",
"Close": "Закрыть", "Close": "Закрыть",
"CloseServer?": "Хотите выключить сервер?", "CloseServer?": "Хотите выключить сервер?",
@@ -59,12 +59,11 @@
"Offline": "Сервер не доступен", "Offline": "Сервер не доступен",
"OK": "OK", "OK": "OK",
"OpenLink": "Открыть", "OpenLink": "Открыть",
"Peers": "[Подкл.] Пиры", "Peers": "Пиры",
"PeersListenPort": "Порт для входящих подключений", "PeersListenPort": "Порт для входящих подключений",
"PEX": "PEX (Peer Exchange)", "PEX": "PEX (Peer Exchange)",
"PiecesCount": "Кол-во блоков", "PiecesCount": "Кол-во блоков",
"PiecesLength": "Размер блока", "PiecesLength": "Размер блока",
"PlaylistAll": "Плейлист всех",
"Preload": "Предзагр.", "Preload": "Предзагр.",
"PreloadBuffer": "Наполнять кеш перед началом воспроизведения", "PreloadBuffer": "Наполнять кеш перед началом воспроизведения",
"ReaderReadAHead": "Кеш предзагрузки (5-100%, рек. 95%)", "ReaderReadAHead": "Кеш предзагрузки (5-100%, рек. 95%)",

View File

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

View File

@@ -11,3 +11,33 @@ export function getPeerString(torrent) {
export const shortenText = (text, sympolAmount) => export const shortenText = (text, sympolAmount) =>
text ? text.slice(0, sympolAmount) + (text.length > 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
}