expand boolean helpers to support more than just true and false

This commit is contained in:
iPromKnight
2024-02-06 11:17:46 +00:00
committed by iPromKnight
parent 815c70709d
commit dcf62fe651
2 changed files with 37 additions and 5 deletions

View File

@@ -1,8 +1,40 @@
export const BooleanHelpers = {
parseBool: function(value: string | undefined, defaultValue: boolean) {
if (value === undefined) {
return defaultValue;
parseBool: function(value: string | number | undefined, defaultValue: boolean): boolean {
switch (typeof value) {
case 'string':
return parseStringToBool(value, defaultValue);
case 'boolean':
return value;
case 'number':
return parseNumberToBool(value, defaultValue);
default:
return defaultValue;
}
return value.toLowerCase().trim() === 'true';
}
}
const parseStringToBool = (input: string, defaultVal: boolean): boolean => {
switch (input.toLowerCase().trim()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
default:
return defaultVal
}
}
const parseNumberToBool = (input: number, defaultVal: boolean): boolean => {
switch (input) {
case 1:
return true;
case 0:
return false;
default:
return defaultVal;
}
}

View File

@@ -8,7 +8,7 @@ import { parsingService } from './services/parsing_service';
import { torrentFileService } from './services/torrent_file_service';
import { torrentSubtitleService } from './services/torrent_subtitle_service';
export async function createTorrentEntry(torrent, overwrite = false) : Promise<void> {
export async function createTorrentEntry(torrent, overwrite = false) {
const titleInfo = parse(torrent.title);
if (!torrent.imdbId && torrent.type !== TorrentType.Anime) {