parse-torrent-title
This package helps you extract information from a torrent name such as language, resolution and codec.
Installation
You can install it using npm:
npm install parse-torrent-title
You should use Node 8.0 or higher to use this package.
Usage
A simple usage is as follows:
const ptt = ;const information = ptt; console; // Game of Thronesconsole; // 1console; // 1console; // 720pconsole; // x264console; // HDTVconsole; // CTU
Advanced usage
This module is configurable and extendable using handlers and regular expressions.
Regular expressions
If you want an extra field to be populated, you can use a regular expression as follow:
const ptt = ;ptt;const information = ptt; console; // 1
If you want to keep only a part of the matched regular expression, you should use capturing groups explained here.
For regular expressions, the following options are available:
skipIfAlreadyFound
(default totrue
) which will skip the regular expression if a previous handler for the same information already found something.type
(default tostring
) which indicates what is the expected output of the regular expression. It can be:string
: does nothinginteger
: convert the matching part into an integerlowercase
: convert the matching part to lowercaseboolean
: convert to true if there is a matching part
value
(default to undefined) which, if defined, set the specified value instead of the matching part as the result
Handlers
A handler is a function with the title and the resulting information as parameters. It can modify the result in any wanted way. If the matched string is not part of the title, it should return the beginning of it.
const ptt = ;const information = ptt;console; // undefined ptt; const information2 = ptt;console; // true
Multiple parsers
You may want several parsers within the same project. To do that, you can simply create new parsers:
const Parser = ;const parser = ;const anotherParser = ;
By default, a freshly created parser has no handler. If you want to add default handlers to a parser, you can do so using the specific method:
const Parser addDefaults = ;const parser = ;; // parser is now ready
Usage with TypeScript
If you add new properties with addHandler
in TypeScript, the result type DefaultParserResult
will not be updated,
and you will encounter a TS2339 error.
To prevent this error and have autocomplete,
you should create a ParserResult
interface with the expected properties.
It is possible to extend the DefaultParserResult
interface to get all the default properties.
Then, create a new Parser
object using the above interface.
Finally, use the parse
function as usual.
Example:
; ;addDefaultsparser;parser.addHandler'part', /[. ]?/i, ; ;console.logresult.year; // 2018 - it works as beforeconsole.logresult.part; // 1 - `part` is now a known property of the `result` object