Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions content.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions src/commands/increment-decrement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => "";
Expand Down
4 changes: 4 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
duplicateAndDecrement,
handleDuplicateAndIncrementDecrement,
updateSelectionsAfterDuplicate,
incrementFloat,
decrementFloat,
} from "./increment-decrement";
import { sequence } from "./sequence";
import { randomCase } from "./random-case";
Expand Down Expand Up @@ -49,6 +51,8 @@ export const commandNameFunctionMap: CommandRegistry = {
decrement,
duplicateAndIncrement,
duplicateAndDecrement,
incrementFloat,
decrementFloat,
sequence,
utf8ToChar,
charToUtf8,
Expand Down
2 changes: 2 additions & 0 deletions src/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const numberFunctionNames = [
"sequence",
"duplicateAndIncrement",
"duplicateAndDecrement",
"incrementFloat",
"decrementFloat",
];

export const functionNamesWithArgument = [
Expand Down
34 changes: 34 additions & 0 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down