From 283d7f2159db862f0b17d2840251775f6c762c6b Mon Sep 17 00:00:00 2001 From: j433866 Date: Tue, 18 Dec 2018 10:40:18 +0000 Subject: [PATCH] Add Output Filter option to Magic operation --- src/core/lib/Magic.mjs | 9 ++++++--- src/core/operations/Magic.mjs | 9 +++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index b4d5a7b..52908eb 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -267,7 +267,7 @@ class Magic { * @param {boolean} [useful=false] - Whether the current recipe should be scored highly * @returns {Object[]} - A sorted list of the recipes most likely to result in correct decoding */ - async speculativeExecution(depth=0, extLang=false, intensive=false, recipeConfig=[], useful=false) { + async speculativeExecution(depth=0, extLang=false, intensive=false, recipeConfig=[], useful=false, filter=null) { if (depth < 0) return []; // Find any operations that can be run on this data @@ -305,7 +305,7 @@ class Magic { const magic = new Magic(output, this.opPatterns), speculativeResults = await magic.speculativeExecution( - depth-1, extLang, intensive, [...recipeConfig, opConfig], op.useful); + depth-1, extLang, intensive, [...recipeConfig, opConfig], op.useful, filter); results = results.concat(speculativeResults); })); @@ -331,7 +331,10 @@ class Magic { r.fileType || // A file was found r.isUTF8 || // UTF-8 was found r.matchingOps.length // A matching op was found - ) + ) && + ( + filter == null || // Either no filter was passed, or + new RegExp(filter).test(r.data)) // the filter matches the result data ); // Return a sorted list of possible recipes along with their properties diff --git a/src/core/operations/Magic.mjs b/src/core/operations/Magic.mjs index b3e15e6..bb419ba 100644 --- a/src/core/operations/Magic.mjs +++ b/src/core/operations/Magic.mjs @@ -43,6 +43,11 @@ class Magic extends Operation { "name": "Extensive language support", "type": "boolean", "value": false + }, + { + "name": "Output Filter (Regex)", + "type": "string", + "value": "" } ]; } @@ -56,10 +61,10 @@ class Magic extends Operation { */ async run(state) { const ings = state.opList[state.progress].ingValues, - [depth, intensive, extLang] = ings, + [depth, intensive, extLang, filter] = ings, dish = state.dish, magic = new MagicLib(await dish.get(Dish.ARRAY_BUFFER)), - options = await magic.speculativeExecution(depth, extLang, intensive); + options = await magic.speculativeExecution(depth, extLang, intensive, [], false, filter); // Record the current state for use when presenting this.state = state;