|
| 1 | +function extendOptions(options) { |
| 2 | + options = options || {}; |
| 3 | + |
| 4 | + const defaultOptions = { |
| 5 | + skipIfAlreadyFound: true, |
| 6 | + type: "string", |
| 7 | + }; |
| 8 | + |
| 9 | + options.skipIfAlreadyFound = options.skipIfAlreadyFound || defaultOptions.skipIfAlreadyFound; |
| 10 | + options.multipleParts = options.multipleParts || defaultOptions.multipleParts; |
| 11 | + |
| 12 | + return options; |
| 13 | +} |
| 14 | + |
| 15 | +function createHandlerFromRegExp(name, regExp, options) { |
| 16 | + let transformer; |
| 17 | + |
| 18 | + if (!options.type) { |
| 19 | + transformer = input => input; |
| 20 | + } else if (options.type.toLowerCase() === "lowercase") { |
| 21 | + transformer = input => input.toLowerCase(); |
| 22 | + } else if (options.type.toLowerCase().substr(0, 4) === "bool") { |
| 23 | + transformer = input => true; |
| 24 | + } else if (options.type.toLowerCase().substr(0, 3) === "int") { |
| 25 | + transformer = input => parseInt(input, 10); |
| 26 | + } else { |
| 27 | + transformer = input => input; |
| 28 | + } |
| 29 | + |
| 30 | + return ({ title, result }) => { |
| 31 | + if (result[name] && options.skipIfAlreadyFound) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const match = title.match(regExp); |
| 36 | + const [rawMatch, cleanMatch] = match || []; |
| 37 | + |
| 38 | + if (rawMatch) { |
| 39 | + result[name] = transformer(cleanMatch || rawMatch); |
| 40 | + return match.index; |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function cleanTitle(rawTitle) { |
| 46 | + if (rawTitle.indexOf(" ") === -1 && rawTitle.indexOf(".") !== -1) { |
| 47 | + rawTitle = rawTitle.replace(/\./g, " "); |
| 48 | + } |
| 49 | + |
| 50 | + rawTitle = rawTitle.replace(/_/g, " "); |
| 51 | + rawTitle = rawTitle.replace(/([(_]|- )$/, "").trim(); |
| 52 | + |
| 53 | + return rawTitle; |
| 54 | +} |
| 55 | + |
| 56 | +class Parser { |
| 57 | + |
| 58 | + constructor() { |
| 59 | + this.handlers = []; |
| 60 | + } |
| 61 | + |
| 62 | + addHandler(handlerName, handler, options) { |
| 63 | + if (typeof handler === "undefined" && typeof handlerName === "function") { |
| 64 | + // If no name is provided and a function handler is directly given |
| 65 | + handler = handlerName; |
| 66 | + handlerName = "Unknown handler"; |
| 67 | + } else if (typeof handlerName === "string" && handler instanceof RegExp) { |
| 68 | + options = extendOptions(options); |
| 69 | + // If the handler provided is a regular expression |
| 70 | + handler = createHandlerFromRegExp(handlerName, handler, options); |
| 71 | + } else if (typeof handler !== "function") { |
| 72 | + // If the handler is neither a function or a regular expression, throw an error |
| 73 | + throw new Error(`Handler for ${handlerName} should be a RegExp or a function. Got: ${typeof handler}`); |
| 74 | + } |
| 75 | + |
| 76 | + this.handlers.push(handler); |
| 77 | + } |
| 78 | + |
| 79 | + parse(title) { |
| 80 | + const result = {}; |
| 81 | + let endOfTitle = title.length; |
| 82 | + |
| 83 | + for (const handler of this.handlers) { |
| 84 | + const matchIndex = handler({ title: title, result }); |
| 85 | + if (matchIndex && matchIndex < endOfTitle) { |
| 86 | + endOfTitle = matchIndex; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + result.title = cleanTitle(title.substr(0, endOfTitle)); |
| 91 | + |
| 92 | + return result; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +exports.Parser = Parser; |
0 commit comments