{"_id":"@brawdunoir/parse-torrent-filename","name":"@brawdunoir/parse-torrent-filename","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@brawdunoir/parse-torrent-filename","version":"1.0.0","description":"Parse torrent filenames to extract useful information","main":"index.js","type":"module","directories":{"test":"test"},"devDependencies":{"@eslint/js":"^9.14.0","chai":"^5.1.2","eslint":"^9.14.0","eslint-plugin-import":"^2.31.0","globals":"^15.12.0","mocha":"^10.8.2","prettier":"3.3.3","prettier-eslint":"^16.3.0"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"git+https://github.com/Brawdunoir/parse-torrent-filename.git"},"keywords":["parse","torrent","title","name","extract","information","es6","esm"],"license":"MIT","types":"./index.d.ts","bugs":{"url":"https://github.com/Brawdunoir/parse-torrent-filename/issues"},"homepage":"https://github.com/Brawdunoir/parse-torrent-filename#readme","dependencies":{"dayjs":"^1.11.13"},"_id":"@brawdunoir/parse-torrent-filename@1.0.0","gitHead":"3f5efa0dedf18a24d1f6f07c717a9a1cabb142e8","_nodeVersion":"22.11.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-ufvfcyUDtklYGmL8PeTa52WJIWn3ODTRB8YHpMR2exzRfzCuxIMz1ZUF1JIKXkfr3SItVbjRDz1DpoqkP8JPWg==","shasum":"b7125b5394a662055fe39aaf4183746e614e2bba","tarball":"https://registry.npmjs.org/@brawdunoir/parse-torrent-filename/-/parse-torrent-filename-1.0.0.tgz","fileCount":10,"unpackedSize":58221,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCtbH96lbEXyc+VgizJ+nRALs7jdR7zpD76FjyoKwWjQwIhAOMgrI18BmsmGExDzVOwT0kW+Pm4efexQu1JHEj3mwPe"}]},"_npmUser":{"name":"brawdunoir","email":"yann.lacroix.dev@gmail.com"},"maintainers":[{"name":"brawdunoir","email":"yann.lacroix.dev@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/parse-torrent-filename_1.0.0_1731118379913_0.13607236628415786"},"_hasShrinkwrap":false}},"time":{"created":"2024-11-09T02:12:59.799Z","1.0.0":"2024-11-09T02:13:00.080Z","modified":"2024-11-09T02:13:00.392Z"},"maintainers":[{"name":"brawdunoir","email":"yann.lacroix.dev@gmail.com"}],"description":"Parse torrent filenames to extract useful information","homepage":"https://github.com/Brawdunoir/parse-torrent-filename#readme","keywords":["parse","torrent","title","name","extract","information","es6","esm"],"repository":{"type":"git","url":"git+https://github.com/Brawdunoir/parse-torrent-filename.git"},"bugs":{"url":"https://github.com/Brawdunoir/parse-torrent-filename/issues"},"license":"MIT","readme":"# parse-torrent-filename\n\nForked from [parse-torrent-title](https://github.com/TheBeastLT/parse-torrent-title) but migrated to ES6.\n\nThis package helps you extract information from a torrent name such as language, resolution and codec.\n\n## Installation\n\nYou can install it using npm:\n\n```bash\nnpm install @brawdunoir/parse-torrent-filename\n```\n\nYou should use Node 18.0 or higher to use this package.\n\n## Usage\n\nA simple usage is as follows:\n\n```javascript\nimport { parse } from '@brawdunoir/parse-torrent-filename/index.js'\nconst information = parse(\"Game.of.Thrones.S01E01.720p.HDTV.x264-CTU\");\n\nconsole.log(information.title); // Game of Thrones\nconsole.log(information.season); // 1\nconsole.log(information.episode); // 1\nconsole.log(information.resolution); // 720p\nconsole.log(information.codec); // x264\nconsole.log(information.source); // HDTV\nconsole.log(information.group); // CTU\n```\n\n## Advanced usage\n\nThis module is configurable and extendable using handlers and regular expressions.\n\n### Regular expressions\n\nIf you want an extra field to be populated, you can use a regular expression as follow:\n\n```javascript\nimport { parse, addHandler } from '@brawdunoir/parse-torrent-filename/index.js'\n\n// Add a handler for the \"part\" information\naddHandler(\"part\", /Part[. ]([0-9])/i, { type: \"integer\" });\nconst information = parse(\"Silent.Witness.S18E03.Falling.Angels.Part.1.720p.HDTV.x264-FTP\",);\n\nconsole.log(information)\n// {\n//   resolution: '720p',\n//   source: 'HDTV',\n//   codec: 'x264',\n//   group: 'FTP',\n//   seasons: [ 18 ],\n//   season: 18,\n//   episodes: [ 3 ],\n//   episode: 3,\n//   part: '1',    <----- new field\n//   title: 'Silent Witness'\n// }\n```\n\nIf you want to keep only a part of the matched regular expression, you should use capturing groups\n[explained here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).\n\nFor regular expressions, the following options are available:\n\n- `skipIfAlreadyFound` (default to `true`) which will skip the regular expression if a previous handler for the same\n  information already found something.\n- `type` (default to `string`) which indicates what is the expected output of the regular expression.\n  It can be:\n  - `string`: does nothing\n  - `integer`: convert the matching part into an integer\n  - `lowercase`: convert the matching part to lowercase\n  - `boolean`: convert to true if there is a matching part\n- `value` (default to undefined) which, if defined, set the specified value instead of the matching part as the result\n\n### Handlers\n\nA handler is a function with the title and the resulting information as parameters.\nIt can modify the result in any wanted way.\nIf the matched string is not part of the title, it should return the beginning of it.\n\n```javascript\nimport { parse, addHandler } from '@brawdunoir/parse-torrent-filename/index.js'\n\nconst information = parse(\n  \"[REQ] Harry Potter And The Prisoner Of Azkaban 2004 1080p BluRay DTS x264-hV\",\n);\nconsole.log(information.isHarryPotterRelated); // undefined\n\naddHandler(({ title, result }) => {\n  const match = title.match(/harry.potter/i);\n  if (match) {\n    result.isHarryPotterRelated = true;\n  }\n});\n\nconst information2 = parse(\n  \"[REQ] Harry Potter And The Prisoner Of Azkaban 2004 1080p BluRay DTS x264-hV\",\n);\nconsole.log(information2.isHarryPotterRelated); // true\n```\n\n### Multiple parsers\n\nYou may want several parsers within the same project.\nTo do that, you can simply create new parsers:\n\n```javascript\nimport { Parser } from '@brawdunoir/parse-torrent-filename/index.js'\nconst parser = new Parser();\nconst anotherParser = new Parser();\n```\n\nBy default, a freshly created parser has no handler.\nIf you want to add default handlers to a parser, you can do so using the specific method:\n\n```javascript\nimport { Parser, addDefaults } from '@brawdunoir/parse-torrent-filename/index.js'\nconst myParser = new Parser();\naddDefaults(myParser); // myParser is now ready\n```\n\n### Usage with TypeScript\n\nIf you add new properties with `addHandler` in TypeScript, the result type `DefaultParserResult` will not be updated,\nand you will encounter a TS2339 error.\n\nTo prevent this error and have autocomplete,\nyou should create a `ParserResult` interface with the expected properties.\nIt is possible to extend the `DefaultParserResult` interface to get all the default properties.\nThen, create a new `Parser` object using the above interface.\nFinally, use the `parse` function as usual.\n\nExample:\n\n```ts\nimport { Parser, DefaultParserResult, addDefaults } from \"@brawdunoir/parse-torrent-filename/index.js\";\n\ninterface ParserResult extends DefaultParserResult {\n  part?: number;\n}\n\nconst parser = new Parser<ParserResult>();\naddDefaults(parser);\nparser.addHandler(\"part\", /(?:Part|CD)[. ]?([0-9])/i, { type: \"integer\" });\nconst parse = parser.parse;\n\nconst result = parse(\n  \"Watergate.2018.Part1.DOC.SUBFRENCH.1080p.HDTV.H264-ELEARNiNG\",\n);\nconsole.log(result.year); // 2018 - it works as before\nconsole.log(result.part); // 1 - `part` is now a known property of the `result` object\n```\n","readmeFilename":"README.md"}