diff --git a/README.md b/README.md index b2f7615..efc2625 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ Number related functions: 1. duplicate and increment - duplicates selection and increments all numbers 1. duplicate and decrement - duplicates selection and decrements all numbers 1. sequence - replaces numbers with a sequence starting from the first number +1. incrementFloat - increases all floating point numbers in the selection by 1 +1. decrementFloat - decreases all floating point numbers in the selection by 1 Additional utility commands: diff --git a/content.txt b/content.txt index 6853409..d861fbb 100644 --- a/content.txt +++ b/content.txt @@ -51,3 +51,5 @@ Test123! No quotes at all 'It's' "My name's %20 Minalike!" +Lorem -1.234 ipsum 5.678 dolor sit amet, consectetur adipiscing. +Sed do 9.876 eiusmod -4.321 tempor incididunt labore. diff --git a/package.json b/package.json index 5100283..d7cd937 100644 --- a/package.json +++ b/package.json @@ -180,6 +180,16 @@ "category": "String Manipulation", "command": "string-manipulation.duplicateAndDecrement" }, + { + "title": "Increment all floats", + "category": "String Manipulation", + "command": "string-manipulation.incrementFloat" + }, + { + "title": "Decrement all floats", + "category": "String Manipulation", + "command": "string-manipulation.decrementFloat" + }, { "title": "Sequence all numbers from first number", "category": "String Manipulation", @@ -329,6 +339,14 @@ "command": "string-manipulation.duplicateAndDecrement", "group": "7_modification" }, + { + "command": "string-manipulation.incrementFloat", + "group": "7_modification" + }, + { + "command": "string-manipulation.decrementFloat", + "group": "7_modification" + }, { "command": "string-manipulation.sequence", "group": "7_modification" diff --git a/src/commands/increment-decrement.ts b/src/commands/increment-decrement.ts index 0bfe76c..bd4218b 100644 --- a/src/commands/increment-decrement.ts +++ b/src/commands/increment-decrement.ts @@ -7,6 +7,22 @@ export const increment: CommandFunction = (str: string) => export const decrement: CommandFunction = (str: string) => str.replace(/-?\d+/g, (n) => String(Number(n) - 1)); +export const incrementFloat: CommandFunction = (str: string) => { + return str.replace(/-?\d+\.\d+/g, (n) => { + let decimalPlaces = (n.split(".")[1] || "").length; + let factor = Math.pow(10, decimalPlaces); + return String((Number(n) * factor + 1) / factor); + }); +}; + +export const decrementFloat: CommandFunction = (str: string) => { + return str.replace(/-?\d+\.\d+/g, (n) => { + let decimalPlaces = (n.split(".")[1] || "").length; + let factor = Math.pow(10, decimalPlaces); + return String((Number(n) * factor - 1) / factor); + }); +}; + // These functions are placeholders as the actual implementation is in the stringFunction // They're kept here for type consistency in the command registry export const duplicateAndIncrement: CommandFunction = () => ""; diff --git a/src/commands/index.ts b/src/commands/index.ts index afe8968..be6a970 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -13,6 +13,8 @@ import { duplicateAndDecrement, handleDuplicateAndIncrementDecrement, updateSelectionsAfterDuplicate, + incrementFloat, + decrementFloat, } from "./increment-decrement"; import { sequence } from "./sequence"; import { randomCase } from "./random-case"; @@ -49,6 +51,8 @@ export const commandNameFunctionMap: CommandRegistry = { decrement, duplicateAndIncrement, duplicateAndDecrement, + incrementFloat, + decrementFloat, sequence, utf8ToChar, charToUtf8, diff --git a/src/commands/types.ts b/src/commands/types.ts index d84cab1..2931b5e 100644 --- a/src/commands/types.ts +++ b/src/commands/types.ts @@ -23,6 +23,8 @@ export const numberFunctionNames = [ "sequence", "duplicateAndIncrement", "duplicateAndDecrement", + "incrementFloat", + "decrementFloat", ]; export const functionNamesWithArgument = [ diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index e8fa5b1..735d471 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -455,6 +455,40 @@ suite("Extension Test Suite", () => { ); }); + test("incrementFloat increases all floats in the selection by 1", async () => { + const [output] = await getTextForSelectionsByCommand( + "string-manipulation.incrementFloat", + [ + { + start: { line: 53, character: 0 }, + end: { line: 53, character: 64 }, + }, + ] + ); + + assert.strictEqual( + output /* Lorem -1.234 ipsum 5.678 dolor sit amet, consectetur adipiscing. */, + "Lorem -1.233 ipsum 5.679 dolor sit amet, consectetur adipiscing." + ); + }); + + test("decrementFloat decreases all floats in the selection by 1", async () => { + const [output] = await getTextForSelectionsByCommand( + "string-manipulation.decrementFloat", + [ + { + start: { line: 54, character: 0 }, + end: { line: 54, character: 53 }, + }, + ] + ); + + assert.strictEqual( + output /* Sed do 9.876 eiusmod -4.321 tempor incididunt labore. */, + "Sed do 9.875 eiusmod -4.322 tempor incididunt labore." + ); + }); + test("duplicateAndIncrement duplicates selection and increments numbers in duplicate", async () => { const [output] = await getTextForSelectionsByCommand( "string-manipulation.duplicateAndIncrement",