diff --git a/extension.js b/extension.js index 6b84bd4..18cfbe8 100644 --- a/extension.js +++ b/extension.js @@ -5,12 +5,11 @@ const chicagoStyleTitleCase = require("chicago-capitalize"); const slugify = require("@sindresorhus/slugify"); const defaultFunction = (commandName, option) => (str) => _string[commandName](str, option); -const sequence = (str) => { - let initial; +const sequence = (str, multiselectData = {}) => { return str.replace(/-?\d+/g, (n) => { - const isFirst = typeof initial !== "number"; - initial = isFirst ? Number(n) : initial + 1; - return initial; + const isFirst = typeof multiselectData.offset !== "number"; + multiselectData.offset = isFirst ? Number(n) : multiselectData.offset + 1; + return multiselectData.offset; }); }; const increment = (str) => str.replace(/-?\d+/g, (n) => Number(n) + 1); @@ -75,6 +74,7 @@ const stringFunction = async (commandName, context) => { const selectionMap = {}; if (!editor) return; + let multiselectData = {}; editor.selections.forEach(async (selection, index) => { const text = editor.document.getText(selection); const textParts = text.split("\n"); @@ -87,7 +87,7 @@ const stringFunction = async (commandName, context) => { .reduce((prev, curr) => prev.push(stringFunc(curr)) && prev, []) .join("\n"); } else if (numberFunctionNames.includes(commandName)) { - replaced = commandNameFunctionMap[commandName](text); + replaced = commandNameFunctionMap[commandName](text, multiselectData); } else { stringFunc = commandNameFunctionMap[commandName]; replaced = textParts diff --git a/test/suite/extension.test.js b/test/suite/extension.test.js index f5fb7da..2c2c46e 100644 --- a/test/suite/extension.test.js +++ b/test/suite/extension.test.js @@ -95,17 +95,24 @@ suite("Extension Test Suite", () => { "a14 b15 c16\n17d 18e 19f 20x y21 22z23", ], ["sequence", "-3 4 5 6 7", "-3 -2 -1 0 1"], + [ + "sequence", + "1 2 3 7 8 9", + "4 5 6 7 8 9", + { multiselectData: { offset: 3 } }, + ], ["utf8ToChar", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96", "abc中文💖"], ["charToUtf8", "abc中文💖", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96"], ]; suite("commandNameFunctionMap outputs correctly for all methods", () => { tests.forEach( - ([funcName, originalString, expectedString, { functionArg } = {}]) => { - test(`${funcName} returns ${expectedString} when called with ${originalString}`, () => { + ([funcName, originalString, expectedString, { multiselectData, functionArg } = {}]) => { + const arguments = `${originalString}${multiselectData ? `, ${JSON.stringify(multiselectData)}` : ''}`; + test(`${funcName} returns ${expectedString} when called with ${arguments}`, () => { const func = functionArg ? myExtension.commandNameFunctionMap[funcName](functionArg) : myExtension.commandNameFunctionMap[funcName]; - assert.equal(func(originalString), expectedString); + assert.equal(func(originalString, multiselectData), expectedString); }); } );