From f383a3ca8907b47df3489ac0e74060d9173e938f Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Fri, 20 Feb 2015 23:04:57 +0100 Subject: [PATCH] Refactored Episoder #190 --- lib/utils/episoder.js | 61 +++++++++++---------------------------- spec/lib/episoder.spec.js | 5 ++++ 2 files changed, 22 insertions(+), 44 deletions(-) diff --git a/lib/utils/episoder.js b/lib/utils/episoder.js index 9ed23e030..7d78bbc17 100755 --- a/lib/utils/episoder.js +++ b/lib/utils/episoder.js @@ -1,45 +1,31 @@ -//From https://github.com/rohanorton/episoder +//From https://github.com/rohanorton/episoder var path = require("path"), titleCase = require("to-title-case"); // filename: string // options: object function parseFilename(filename, options) { - var ext = path.extname(filename).toLowerCase(), // the following regex should match: // Community S01E04.mp4 // Community s01e04.mp4 // Community 1x04.mp4 // Community 1-04.mp4 - re = /(.*)\D(\d{1,2})[ex\-](\d{1,2})/i, - searchResults = filename.match(re), - show, - season, - episode, - offset, - episodeObject = {}; + // Community Season 01 Episode 04.mp4 + var re = /(\w+)\s*\D(?:Season\s*)?(\d{1,2})(?:[ex\-]|\s*Episode\s*)(\d{1,2})/i; + var searchResults = filename.match(re); - if (options === undefined) { + if (!options) { options = {}; } - offset = options.offset || 0; - if (searchResults === null) { - // this regex should match: - // Community Season 1 Episode 4.mp4 - // (case insensitive) - re = /(.*)Season.*?(\d{1,2}).*Episode\D*?(\d{1,2})/i; - searchResults = filename.match(re); - } - - if (searchResults === null) { + if (!searchResults) { // this regex should match: // Community 104.mp4 re = /(.*)\D(\d)(\d\d)\D/; searchResults = filename.match(re); } - if (searchResults === null && options.season) { + if (!searchResults && options.season) { // this regex should match: // Community 04.mp4 // but only if we've specified a season with season flag @@ -47,7 +33,7 @@ function parseFilename(filename, options) { searchResults = filename.match(re); } - if (searchResults === null && options.season && options.show) { + if (!searchResults && options.season && options.show) { // this regex should match: // 04.mp4 // but only if we've specified a season and show with flags @@ -55,39 +41,26 @@ function parseFilename(filename, options) { searchResults = filename.match(re); } - try { - show = options.show || searchResults[1]; - } catch (e) { + if (!searchResults) { return null; } - show = titleCase(show - // remove hanging characters - .replace(/^[\-.\s]+|[\-.\s]+$/g, "") - .trim()); - if (options.episode) { - episode = options.episode + offset; - if (searchResults !== null) { - searchResults.pop(); - } - } else { - try { - episode = Number(searchResults.pop()) + offset; - } catch (e) { - return null; - } - } + var show = options.show || searchResults[1]; + show = titleCase(show.replace(/^[\-.\s]+|[\-.\s]+$/g, "").trim()); + + var season = options.season || parseInt(searchResults[2], 10); - season = options.season || Number(searchResults.pop()); + var offset = options.offset || 0; + var episode = (options.episode || parseInt(searchResults[3], 10)) + offset; - episodeObject = { + var ext = path.extname(filename).toLowerCase(); + return { originalFilename: filename, show: show, season: season, episode: episode, extension: ext }; - return episodeObject; } exports.parseFilename = parseFilename; diff --git a/spec/lib/episoder.spec.js b/spec/lib/episoder.spec.js index c4726c0fd..a86b38017 100644 --- a/spec/lib/episoder.spec.js +++ b/spec/lib/episoder.spec.js @@ -41,4 +41,9 @@ describe('Episoder', function () { var result = episoder.parseFilename('Test S01E04', { offset: 2 }); assert.equal(result.episode, 6); }); + + it('should not fail for show name only in filename', function() { + var result = episoder.parseFilename('Community'); + assert.equal(result, null); + }); });