parse-torrent-path 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Charles Aldarondo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # parse-torrent-path
2
+
3
+ Modular, handler-based media metadata parser for filenames and paths. Optimized for general media library organization and complex naming conventions.
4
+
5
+ ## Features
6
+
7
+ - **Modular Handler System**: Extensible architecture using regex and functional handlers.
8
+ - **Extended Pattern Support**: Specialized patterns (e.g., `S1 - E01`, `- N -`).
9
+ - **Path Context**: Automatically extracts season info from parent folder names (e.g., `Season 01/`).
10
+ - **Extras Detection**: Identifies bonus content, deleted scenes, and featurettes.
11
+ - **Episode Title Extraction**: Captures episode titles embedded in filenames.
12
+ - **Movie Year Fallbacks**: Advanced detection for embedded broadcast years.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install parse-torrent-path
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Simple Parsing
23
+
24
+ ```typescript
25
+ import { parsePath } from 'parse-torrent-path';
26
+
27
+ const info = parsePath('Show.Name.S01E02.1080p.mkv');
28
+ // {
29
+ // title: 'Show Name',
30
+ // season: 1,
31
+ // episode: 2,
32
+ // resolution: '1080p'
33
+ // }
34
+ ```
35
+
36
+ ### Path Context Awareness
37
+
38
+ ```typescript
39
+ // Uses folder name to infer missing season
40
+ const info = parsePath('Season 05/Episode 01.mkv');
41
+ // {
42
+ // title: 'Episode 01',
43
+ // season: 5,
44
+ // episode: 1
45
+ // }
46
+ ```
47
+
48
+ ### Alternate Patterns
49
+
50
+ ```typescript
51
+ const info = parsePath('Show.Name.S1 - E01 - Pilot.mp4');
52
+ // {
53
+ // title: 'Show Name',
54
+ // season: 1,
55
+ // episode: 1,
56
+ // episodeTitle: 'Pilot'
57
+ // }
58
+ ```
59
+
60
+ ## Advanced Usage
61
+
62
+ You can leverage the exported `getDefaultParser()` which comes pre-loaded with all standard `parse-torrent-title` rules and our extended path-aware handlers, or you can create a fresh `Parser` and add your own custom logic:
63
+
64
+ ```typescript
65
+ import { getDefaultParser } from 'parse-torrent-path';
66
+
67
+ const parser = getDefaultParser();
68
+
69
+ // Add custom logic
70
+ parser.addHandler('customTag', /\[(TAG)\]/i, { type: 'lowercase' });
71
+
72
+ const info = parser.parse('Show [TAG] S01E01.mkv');
73
+ ```
74
+
75
+ ## Acknowledgements
76
+
77
+ This package is heavily inspired by and built upon the excellent [parse-torrent-title](https://github.com/clement-escolano/parse-torrent-title) package by clement-escolano. It adapts its core "Handlers" architecture while adding context-aware path parsing and extended matching rules.
78
+
79
+ ## License
80
+
81
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Parser: () => Parser2,
34
+ getDefaultParser: () => getDefaultParser,
35
+ parsePath: () => parsePath
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+ var import_path = __toESM(require("path"), 1);
39
+ var ptt = __toESM(require("parse-torrent-title"), 1);
40
+
41
+ // src/defaults.ts
42
+ function addExtendedHandlers(parser) {
43
+ parser.addHandler("year", /^(.*\D)(\d{4})(?=\s|[-_]|$)/, { type: "integer", skipIfAlreadyFound: true });
44
+ parser.addHandler("season", /S([0-9]{1,2})\s*-\s*E[0-9]{1,2}/i, { type: "integer" });
45
+ parser.addHandler("episode", /S[0-9]{1,2}\s*-\s*E([0-9]{1,5})/i, { type: "integer" });
46
+ parser.addHandler("episode", /(?:^|\s)-\s*(\d{1,3})\s*-\s+/i, { type: "integer" });
47
+ parser.addHandler("season", /(\d{1,2})x(\d{2,3})/i, { type: "integer" });
48
+ parser.addHandler("episode", /\d{1,2}x(\d{2,3})/i, { type: "integer" });
49
+ parser.addHandler("season", /Season\s+(\d+)\s+Episode\s+\d+/i, { type: "integer" });
50
+ parser.addHandler("episode", /Season\s+\d+\s+Episode\s+(\d+)/i, { type: "integer" });
51
+ parser.addHandler("episode", /(?:^|[.\s-_])(?:Episode|Ep)[.\s-_]?(\d{1,3})(?:[.\s-_]|$)/i, { type: "integer", skipIfAlreadyFound: true });
52
+ const EXTRAS_PATTERN = /\b(?:extras?|bts|featurettes?|behind[\s_-]the[\s_-]scenes?|deleted[\s_-]scenes?|interviews?|trailers?|bloopers?|outtakes?|shorts?|making[\s_-]of|bonus)\b/i;
53
+ parser.addHandler("isExtras", ({ title, result }) => {
54
+ if (title && result && EXTRAS_PATTERN.test(title)) {
55
+ result.isExtras = true;
56
+ }
57
+ });
58
+ parser.addHandler("episodeTitle", ({ title, result }) => {
59
+ if (title && result && result.episode !== void 0) {
60
+ const patterns = [
61
+ /[Ss]\d{1,2}[Ee]\d{1,3}[\s._-]+(.*)/,
62
+ /[Ss]\d{1,2}\s*-\s*[Ee]\d{1,3}[\s._-]+(.*)/,
63
+ /(?:^|\s)-\s*\d{1,3}\s*-\s+(.*)/,
64
+ /\d{1,2}x\d{2,3}[\s._-]+(.*)/,
65
+ /Season\s+\d+\s+Episode\s+\d+[\s._-]+(.*)/
66
+ ];
67
+ for (const p of patterns) {
68
+ const match = title.match(p);
69
+ if (match && match[1]) {
70
+ const cleaned = match[1].replace(/\.(mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm)$/i, "").trim();
71
+ result.episodeTitle = cleaned;
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ });
77
+ }
78
+
79
+ // src/index.ts
80
+ var Parser2 = ptt.Parser;
81
+ var defaultParser = null;
82
+ function getDefaultParser() {
83
+ if (!defaultParser) {
84
+ defaultParser = new ptt.Parser();
85
+ ptt.addDefaults(defaultParser);
86
+ addExtendedHandlers(defaultParser);
87
+ }
88
+ return defaultParser;
89
+ }
90
+ function parsePath(filePath, options = {}) {
91
+ const parser = options.includeDefaults !== false ? getDefaultParser() : new ptt.Parser();
92
+ const basename = import_path.default.basename(filePath);
93
+ const folderName = import_path.default.dirname(filePath) !== "." ? import_path.default.basename(import_path.default.dirname(filePath)) : "";
94
+ const result = parser.parse(basename);
95
+ if (result.season === void 0 && folderName) {
96
+ const seasonMatch = folderName.match(/^(?:Season\s+|S)([0-9]{1,2})$/i);
97
+ if (seasonMatch) {
98
+ result.season = parseInt(seasonMatch[1], 10);
99
+ }
100
+ }
101
+ return result;
102
+ }
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ Parser,
106
+ getDefaultParser,
107
+ parsePath
108
+ });
109
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/defaults.ts"],"sourcesContent":["import path from 'path';\r\nimport * as ptt from 'parse-torrent-title';\r\nimport { addExtendedHandlers } from './defaults.js';\r\nimport { ParsedInfo, ParserOptions } from './types.js';\r\n\r\nexport const Parser = ptt.Parser;\r\nexport * from './types.js';\r\n\r\nlet defaultParser: ptt.Parser<ParsedInfo> | null = null;\r\n\r\n/**\r\n * Get the default parser with standard and extended handlers.\r\n */\r\nexport function getDefaultParser(): ptt.Parser<ParsedInfo> {\r\n if (!defaultParser) {\r\n defaultParser = new ptt.Parser<ParsedInfo>();\r\n ptt.addDefaults(defaultParser);\r\n addExtendedHandlers(defaultParser);\r\n }\r\n return defaultParser;\r\n}\r\n\r\n/**\r\n * High-level function to parse a filename or path into metadata.\r\n * @param filePath The filename or full path to parse.\r\n * @param options Options for the parser.\r\n */\r\nexport function parsePath(filePath: string, options: ParserOptions = {}): ParsedInfo {\r\n const parser = options.includeDefaults !== false ? getDefaultParser() : new ptt.Parser<ParsedInfo>();\r\n\r\n const basename = path.basename(filePath);\r\n const folderName = path.dirname(filePath) !== '.' ? path.basename(path.dirname(filePath)) : '';\r\n\r\n // Parse the basename first\r\n const result = parser.parse(basename) as ParsedInfo;\r\n\r\n // If it's a \"Season\" folder or we need more context from the path, we can add it here.\r\n // For example, if season is missing but folder is \"Season 01\"\r\n if (result.season === undefined && folderName) {\r\n const seasonMatch = folderName.match(/^(?:Season\\s+|S)([0-9]{1,2})$/i);\r\n if (seasonMatch) {\r\n result.season = parseInt(seasonMatch[1], 10);\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n\r\n","import { Parser as PTTParser } from 'parse-torrent-title';\r\nimport { ParsedInfo, HandlerContext } from './types.js';\r\n\r\nexport function addExtendedHandlers(parser: PTTParser<ParsedInfo>): void {\r\n // Embedded year fallback: \"ShowName2006\"\r\n parser.addHandler('year', /^(.*\\D)(\\d{4})(?=\\s|[-_]|$)/, { type: 'integer', skipIfAlreadyFound: true });\r\n\r\n // Alternate Style: S1 - E01\r\n parser.addHandler('season', /S([0-9]{1,2})\\s*-\\s*E[0-9]{1,2}/i, { type: 'integer' });\r\n parser.addHandler('episode', /S[0-9]{1,2}\\s*-\\s*E([0-9]{1,5})/i, { type: 'integer' });\r\n\r\n // Alternate Style: - N - (standalone episode)\r\n parser.addHandler('episode', /(?:^|\\s)-\\s*(\\d{1,3})\\s*-\\s+/i, { type: 'integer' });\r\n\r\n // NxNN pattern (e.g. 1x01)\r\n parser.addHandler('season', /(\\d{1,2})x(\\d{2,3})/i, { type: 'integer' });\r\n parser.addHandler('episode', /\\d{1,2}x(\\d{2,3})/i, { type: 'integer' });\r\n\r\n // Spelled out: Season N Episode N\r\n parser.addHandler('season', /Season\\s+(\\d+)\\s+Episode\\s+\\d+/i, { type: 'integer' });\r\n parser.addHandler('episode', /Season\\s+\\d+\\s+Episode\\s+(\\d+)/i, { type: 'integer' });\r\n\r\n // Standalone Episode N (fallback)\r\n parser.addHandler('episode', /(?:^|[.\\s-_])(?:Episode|Ep)[.\\s-_]?(\\d{1,3})(?:[.\\s-_]|$)/i, { type: 'integer', skipIfAlreadyFound: true });\r\n\r\n // --- Extras Detection ---\r\n const EXTRAS_PATTERN = /\\b(?:extras?|bts|featurettes?|behind[\\s_-]the[\\s_-]scenes?|deleted[\\s_-]scenes?|interviews?|trailers?|bloopers?|outtakes?|shorts?|making[\\s_-]of|bonus)\\b/i;\r\n parser.addHandler('isExtras', ({ title, result }: HandlerContext) => {\r\n if (title && result && EXTRAS_PATTERN.test(title)) {\r\n result.isExtras = true;\r\n }\r\n });\r\n\r\n // --- Episode Title Cleanup ---\r\n // If we have an episode, try to find the remainder as the episode title\r\n parser.addHandler('episodeTitle', ({ title, result }: HandlerContext) => {\r\n if (title && result && result.episode !== undefined) {\r\n // Look for common separators after the episode number\r\n const patterns = [\r\n /[Ss]\\d{1,2}[Ee]\\d{1,3}[\\s._-]+(.*)/,\r\n /[Ss]\\d{1,2}\\s*-\\s*[Ee]\\d{1,3}[\\s._-]+(.*)/,\r\n /(?:^|\\s)-\\s*\\d{1,3}\\s*-\\s+(.*)/,\r\n /\\d{1,2}x\\d{2,3}[\\s._-]+(.*)/,\r\n /Season\\s+\\d+\\s+Episode\\s+\\d+[\\s._-]+(.*)/\r\n ];\r\n\r\n for (const p of patterns) {\r\n const match = title.match(p);\r\n if (match && match[1]) {\r\n const cleaned = match[1]\r\n .replace(/\\.(mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm)$/i, '')\r\n .trim();\r\n result.episodeTitle = cleaned;\r\n break;\r\n }\r\n }\r\n }\r\n });\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,kBAAiB;AACjB,UAAqB;;;ACEd,SAAS,oBAAoB,QAAqC;AAErE,SAAO,WAAW,QAAQ,+BAA+B,EAAE,MAAM,WAA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C,IAAI,gBAAY,aAAa;AAC7B,wBAAoB,aAAa;AAAA,EACrC;AACA,SAAO;AACX;AAOO,SAAS,UAAU,UAAkB,UAAyB,CAAC,GAAe;AACjF,QAAM,SAAS,QAAQ,oBAAoB,QAAQ,iBAAiB,IAAI,IAAQ,WAAmB;AAEnG,QAAM,WAAW,YAAAC,QAAK,SAAS,QAAQ;AACvC,QAAM,aAAa,YAAAA,QAAK,QAAQ,QAAQ,MAAM,MAAM,YAAAA,QAAK,SAAS,YAAAA,QAAK,QAAQ,QAAQ,CAAC,IAAI;AAG5F,QAAM,SAAS,OAAO,MAAM,QAAQ;AAIpC,MAAI,OAAO,WAAW,UAAa,YAAY;AAC3C,UAAM,cAAc,WAAW,MAAM,gCAAgC;AACrE,QAAI,aAAa;AACb,aAAO,SAAS,SAAS,YAAY,CAAC,GAAG,EAAE;AAAA,IAC/C;AAAA,EACJ;AAEA,SAAO;AACX;","names":["Parser","Parser","path"]}
@@ -0,0 +1,32 @@
1
+ import * as ptt from 'parse-torrent-title';
2
+ import { DefaultParserResult } from 'parse-torrent-title';
3
+
4
+ interface ParsedInfo extends DefaultParserResult {
5
+ episodeTitle?: string;
6
+ showTitle?: string;
7
+ isExtras?: boolean;
8
+ [key: string]: any;
9
+ }
10
+ interface HandlerContext {
11
+ title?: string;
12
+ result?: ParsedInfo;
13
+ }
14
+ type HandlerFunction = (context: HandlerContext) => void;
15
+ interface ParserOptions {
16
+ includeDefaults?: boolean;
17
+ }
18
+
19
+ declare const Parser: typeof ptt.Parser;
20
+
21
+ /**
22
+ * Get the default parser with standard and extended handlers.
23
+ */
24
+ declare function getDefaultParser(): ptt.Parser<ParsedInfo>;
25
+ /**
26
+ * High-level function to parse a filename or path into metadata.
27
+ * @param filePath The filename or full path to parse.
28
+ * @param options Options for the parser.
29
+ */
30
+ declare function parsePath(filePath: string, options?: ParserOptions): ParsedInfo;
31
+
32
+ export { type HandlerContext, type HandlerFunction, type ParsedInfo, Parser, type ParserOptions, getDefaultParser, parsePath };
@@ -0,0 +1,32 @@
1
+ import * as ptt from 'parse-torrent-title';
2
+ import { DefaultParserResult } from 'parse-torrent-title';
3
+
4
+ interface ParsedInfo extends DefaultParserResult {
5
+ episodeTitle?: string;
6
+ showTitle?: string;
7
+ isExtras?: boolean;
8
+ [key: string]: any;
9
+ }
10
+ interface HandlerContext {
11
+ title?: string;
12
+ result?: ParsedInfo;
13
+ }
14
+ type HandlerFunction = (context: HandlerContext) => void;
15
+ interface ParserOptions {
16
+ includeDefaults?: boolean;
17
+ }
18
+
19
+ declare const Parser: typeof ptt.Parser;
20
+
21
+ /**
22
+ * Get the default parser with standard and extended handlers.
23
+ */
24
+ declare function getDefaultParser(): ptt.Parser<ParsedInfo>;
25
+ /**
26
+ * High-level function to parse a filename or path into metadata.
27
+ * @param filePath The filename or full path to parse.
28
+ * @param options Options for the parser.
29
+ */
30
+ declare function parsePath(filePath: string, options?: ParserOptions): ParsedInfo;
31
+
32
+ export { type HandlerContext, type HandlerFunction, type ParsedInfo, Parser, type ParserOptions, getDefaultParser, parsePath };
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ // src/index.ts
2
+ import path from "path";
3
+ import * as ptt from "parse-torrent-title";
4
+
5
+ // src/defaults.ts
6
+ function addExtendedHandlers(parser) {
7
+ parser.addHandler("year", /^(.*\D)(\d{4})(?=\s|[-_]|$)/, { type: "integer", skipIfAlreadyFound: true });
8
+ parser.addHandler("season", /S([0-9]{1,2})\s*-\s*E[0-9]{1,2}/i, { type: "integer" });
9
+ parser.addHandler("episode", /S[0-9]{1,2}\s*-\s*E([0-9]{1,5})/i, { type: "integer" });
10
+ parser.addHandler("episode", /(?:^|\s)-\s*(\d{1,3})\s*-\s+/i, { type: "integer" });
11
+ parser.addHandler("season", /(\d{1,2})x(\d{2,3})/i, { type: "integer" });
12
+ parser.addHandler("episode", /\d{1,2}x(\d{2,3})/i, { type: "integer" });
13
+ parser.addHandler("season", /Season\s+(\d+)\s+Episode\s+\d+/i, { type: "integer" });
14
+ parser.addHandler("episode", /Season\s+\d+\s+Episode\s+(\d+)/i, { type: "integer" });
15
+ parser.addHandler("episode", /(?:^|[.\s-_])(?:Episode|Ep)[.\s-_]?(\d{1,3})(?:[.\s-_]|$)/i, { type: "integer", skipIfAlreadyFound: true });
16
+ const EXTRAS_PATTERN = /\b(?:extras?|bts|featurettes?|behind[\s_-]the[\s_-]scenes?|deleted[\s_-]scenes?|interviews?|trailers?|bloopers?|outtakes?|shorts?|making[\s_-]of|bonus)\b/i;
17
+ parser.addHandler("isExtras", ({ title, result }) => {
18
+ if (title && result && EXTRAS_PATTERN.test(title)) {
19
+ result.isExtras = true;
20
+ }
21
+ });
22
+ parser.addHandler("episodeTitle", ({ title, result }) => {
23
+ if (title && result && result.episode !== void 0) {
24
+ const patterns = [
25
+ /[Ss]\d{1,2}[Ee]\d{1,3}[\s._-]+(.*)/,
26
+ /[Ss]\d{1,2}\s*-\s*[Ee]\d{1,3}[\s._-]+(.*)/,
27
+ /(?:^|\s)-\s*\d{1,3}\s*-\s+(.*)/,
28
+ /\d{1,2}x\d{2,3}[\s._-]+(.*)/,
29
+ /Season\s+\d+\s+Episode\s+\d+[\s._-]+(.*)/
30
+ ];
31
+ for (const p of patterns) {
32
+ const match = title.match(p);
33
+ if (match && match[1]) {
34
+ const cleaned = match[1].replace(/\.(mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm)$/i, "").trim();
35
+ result.episodeTitle = cleaned;
36
+ break;
37
+ }
38
+ }
39
+ }
40
+ });
41
+ }
42
+
43
+ // src/index.ts
44
+ var Parser2 = ptt.Parser;
45
+ var defaultParser = null;
46
+ function getDefaultParser() {
47
+ if (!defaultParser) {
48
+ defaultParser = new ptt.Parser();
49
+ ptt.addDefaults(defaultParser);
50
+ addExtendedHandlers(defaultParser);
51
+ }
52
+ return defaultParser;
53
+ }
54
+ function parsePath(filePath, options = {}) {
55
+ const parser = options.includeDefaults !== false ? getDefaultParser() : new ptt.Parser();
56
+ const basename = path.basename(filePath);
57
+ const folderName = path.dirname(filePath) !== "." ? path.basename(path.dirname(filePath)) : "";
58
+ const result = parser.parse(basename);
59
+ if (result.season === void 0 && folderName) {
60
+ const seasonMatch = folderName.match(/^(?:Season\s+|S)([0-9]{1,2})$/i);
61
+ if (seasonMatch) {
62
+ result.season = parseInt(seasonMatch[1], 10);
63
+ }
64
+ }
65
+ return result;
66
+ }
67
+ export {
68
+ Parser2 as Parser,
69
+ getDefaultParser,
70
+ parsePath
71
+ };
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/defaults.ts"],"sourcesContent":["import path from 'path';\r\nimport * as ptt from 'parse-torrent-title';\r\nimport { addExtendedHandlers } from './defaults.js';\r\nimport { ParsedInfo, ParserOptions } from './types.js';\r\n\r\nexport const Parser = ptt.Parser;\r\nexport * from './types.js';\r\n\r\nlet defaultParser: ptt.Parser<ParsedInfo> | null = null;\r\n\r\n/**\r\n * Get the default parser with standard and extended handlers.\r\n */\r\nexport function getDefaultParser(): ptt.Parser<ParsedInfo> {\r\n if (!defaultParser) {\r\n defaultParser = new ptt.Parser<ParsedInfo>();\r\n ptt.addDefaults(defaultParser);\r\n addExtendedHandlers(defaultParser);\r\n }\r\n return defaultParser;\r\n}\r\n\r\n/**\r\n * High-level function to parse a filename or path into metadata.\r\n * @param filePath The filename or full path to parse.\r\n * @param options Options for the parser.\r\n */\r\nexport function parsePath(filePath: string, options: ParserOptions = {}): ParsedInfo {\r\n const parser = options.includeDefaults !== false ? getDefaultParser() : new ptt.Parser<ParsedInfo>();\r\n\r\n const basename = path.basename(filePath);\r\n const folderName = path.dirname(filePath) !== '.' ? path.basename(path.dirname(filePath)) : '';\r\n\r\n // Parse the basename first\r\n const result = parser.parse(basename) as ParsedInfo;\r\n\r\n // If it's a \"Season\" folder or we need more context from the path, we can add it here.\r\n // For example, if season is missing but folder is \"Season 01\"\r\n if (result.season === undefined && folderName) {\r\n const seasonMatch = folderName.match(/^(?:Season\\s+|S)([0-9]{1,2})$/i);\r\n if (seasonMatch) {\r\n result.season = parseInt(seasonMatch[1], 10);\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n\r\n","import { Parser as PTTParser } from 'parse-torrent-title';\r\nimport { ParsedInfo, HandlerContext } from './types.js';\r\n\r\nexport function addExtendedHandlers(parser: PTTParser<ParsedInfo>): void {\r\n // Embedded year fallback: \"ShowName2006\"\r\n parser.addHandler('year', /^(.*\\D)(\\d{4})(?=\\s|[-_]|$)/, { type: 'integer', skipIfAlreadyFound: true });\r\n\r\n // Alternate Style: S1 - E01\r\n parser.addHandler('season', /S([0-9]{1,2})\\s*-\\s*E[0-9]{1,2}/i, { type: 'integer' });\r\n parser.addHandler('episode', /S[0-9]{1,2}\\s*-\\s*E([0-9]{1,5})/i, { type: 'integer' });\r\n\r\n // Alternate Style: - N - (standalone episode)\r\n parser.addHandler('episode', /(?:^|\\s)-\\s*(\\d{1,3})\\s*-\\s+/i, { type: 'integer' });\r\n\r\n // NxNN pattern (e.g. 1x01)\r\n parser.addHandler('season', /(\\d{1,2})x(\\d{2,3})/i, { type: 'integer' });\r\n parser.addHandler('episode', /\\d{1,2}x(\\d{2,3})/i, { type: 'integer' });\r\n\r\n // Spelled out: Season N Episode N\r\n parser.addHandler('season', /Season\\s+(\\d+)\\s+Episode\\s+\\d+/i, { type: 'integer' });\r\n parser.addHandler('episode', /Season\\s+\\d+\\s+Episode\\s+(\\d+)/i, { type: 'integer' });\r\n\r\n // Standalone Episode N (fallback)\r\n parser.addHandler('episode', /(?:^|[.\\s-_])(?:Episode|Ep)[.\\s-_]?(\\d{1,3})(?:[.\\s-_]|$)/i, { type: 'integer', skipIfAlreadyFound: true });\r\n\r\n // --- Extras Detection ---\r\n const EXTRAS_PATTERN = /\\b(?:extras?|bts|featurettes?|behind[\\s_-]the[\\s_-]scenes?|deleted[\\s_-]scenes?|interviews?|trailers?|bloopers?|outtakes?|shorts?|making[\\s_-]of|bonus)\\b/i;\r\n parser.addHandler('isExtras', ({ title, result }: HandlerContext) => {\r\n if (title && result && EXTRAS_PATTERN.test(title)) {\r\n result.isExtras = true;\r\n }\r\n });\r\n\r\n // --- Episode Title Cleanup ---\r\n // If we have an episode, try to find the remainder as the episode title\r\n parser.addHandler('episodeTitle', ({ title, result }: HandlerContext) => {\r\n if (title && result && result.episode !== undefined) {\r\n // Look for common separators after the episode number\r\n const patterns = [\r\n /[Ss]\\d{1,2}[Ee]\\d{1,3}[\\s._-]+(.*)/,\r\n /[Ss]\\d{1,2}\\s*-\\s*[Ee]\\d{1,3}[\\s._-]+(.*)/,\r\n /(?:^|\\s)-\\s*\\d{1,3}\\s*-\\s+(.*)/,\r\n /\\d{1,2}x\\d{2,3}[\\s._-]+(.*)/,\r\n /Season\\s+\\d+\\s+Episode\\s+\\d+[\\s._-]+(.*)/\r\n ];\r\n\r\n for (const p of patterns) {\r\n const match = title.match(p);\r\n if (match && match[1]) {\r\n const cleaned = match[1]\r\n .replace(/\\.(mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm)$/i, '')\r\n .trim();\r\n result.episodeTitle = cleaned;\r\n break;\r\n }\r\n }\r\n }\r\n });\r\n}\r\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,YAAY,SAAS;;;ACEd,SAAS,oBAAoB,QAAqC;AAErE,SAAO,WAAW,QAAQ,+BAA+B,EAAE,MAAM,WAAW,oBAAoB,KAAK,CAAC;AAGtG,SAAO,WAAW,UAAU,oCAAoC,EAAE,MAAM,UAAU,CAAC;AACnF,SAAO,WAAW,WAAW,oCAAoC,EAAE,MAAM,UAAU,CAAC;AAGpF,SAAO,WAAW,WAAW,iCAAiC,EAAE,MAAM,UAAU,CAAC;AAGjF,SAAO,WAAW,UAAU,wBAAwB,EAAE,MAAM,UAAU,CAAC;AACvE,SAAO,WAAW,WAAW,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAGtE,SAAO,WAAW,UAAU,mCAAmC,EAAE,MAAM,UAAU,CAAC;AAClF,S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O,WAAW,UAAa,YAAY;AAC3C,UAAM,cAAc,WAAW,MAAM,gCAAgC;AACrE,QAAI,aAAa;AACb,aAAO,SAAS,SAAS,YAAY,CAAC,GAAG,EAAE;AAAA,IAC/C;AAAA,EACJ;AAEA,SAAO;AACX;","names":["Parser"]}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "parse-torrent-path",
3
+ "version": "1.0.0",
4
+ "description": "Modular, handler-based media metadata parser for filenames and paths.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "tsup",
27
+ "test": "vitest run --coverage",
28
+ "test:watch": "vitest",
29
+ "lint": "eslint src test",
30
+ "format": "prettier --write src test",
31
+ "typecheck": "tsc --noEmit",
32
+ "prepublishOnly": "npm run typecheck && npm run lint && npm run test && npm run build"
33
+ },
34
+ "author": "Charles Aldarondo <charles.aldarondo@gmail.com>",
35
+ "license": "MIT",
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^22.13.5",
41
+ "@vitest/coverage-v8": "^3.0.7",
42
+ "eslint": "^9.21.0",
43
+ "prettier": "^3.5.2",
44
+ "tsup": "^8.4.0",
45
+ "typescript": "^5.7.3",
46
+ "typescript-eslint": "^8.56.1",
47
+ "vitest": "^3.0.7"
48
+ },
49
+ "dependencies": {
50
+ "parse-torrent-title": "^2.1.0"
51
+ }
52
+ }