Skip to content

Commit 77b6a7e

Browse files
author
Marc Lipovsky
authored
Merge pull request #64 from marclipovsky/feature/float
Fix #29: Add incrementFloat and decrementFloat commands for floating …
2 parents a8c725c + 19122e9 commit 77b6a7e

File tree

7 files changed

+78
-0
lines changed

7 files changed

+78
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Number related functions:
4040
1. duplicate and increment - duplicates selection and increments all numbers
4141
1. duplicate and decrement - duplicates selection and decrements all numbers
4242
1. sequence - replaces numbers with a sequence starting from the first number
43+
1. incrementFloat - increases all floating point numbers in the selection by 1
44+
1. decrementFloat - decreases all floating point numbers in the selection by 1
4345

4446
Additional utility commands:
4547

content.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ Test123!
5151
No quotes at all
5252
'It's'
5353
"My name's %20 Minalike!"
54+
Lorem -1.234 ipsum 5.678 dolor sit amet, consectetur adipiscing.
55+
Sed do 9.876 eiusmod -4.321 tempor incididunt labore.

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@
180180
"category": "String Manipulation",
181181
"command": "string-manipulation.duplicateAndDecrement"
182182
},
183+
{
184+
"title": "Increment all floats",
185+
"category": "String Manipulation",
186+
"command": "string-manipulation.incrementFloat"
187+
},
188+
{
189+
"title": "Decrement all floats",
190+
"category": "String Manipulation",
191+
"command": "string-manipulation.decrementFloat"
192+
},
183193
{
184194
"title": "Sequence all numbers from first number",
185195
"category": "String Manipulation",
@@ -329,6 +339,14 @@
329339
"command": "string-manipulation.duplicateAndDecrement",
330340
"group": "7_modification"
331341
},
342+
{
343+
"command": "string-manipulation.incrementFloat",
344+
"group": "7_modification"
345+
},
346+
{
347+
"command": "string-manipulation.decrementFloat",
348+
"group": "7_modification"
349+
},
332350
{
333351
"command": "string-manipulation.sequence",
334352
"group": "7_modification"

src/commands/increment-decrement.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ export const increment: CommandFunction = (str: string) =>
77
export const decrement: CommandFunction = (str: string) =>
88
str.replace(/-?\d+/g, (n) => String(Number(n) - 1));
99

10+
export const incrementFloat: CommandFunction = (str: string) => {
11+
return str.replace(/-?\d+\.\d+/g, (n) => {
12+
let decimalPlaces = (n.split(".")[1] || "").length;
13+
let factor = Math.pow(10, decimalPlaces);
14+
return String((Number(n) * factor + 1) / factor);
15+
});
16+
};
17+
18+
export const decrementFloat: CommandFunction = (str: string) => {
19+
return str.replace(/-?\d+\.\d+/g, (n) => {
20+
let decimalPlaces = (n.split(".")[1] || "").length;
21+
let factor = Math.pow(10, decimalPlaces);
22+
return String((Number(n) * factor - 1) / factor);
23+
});
24+
};
25+
1026
// These functions are placeholders as the actual implementation is in the stringFunction
1127
// They're kept here for type consistency in the command registry
1228
export const duplicateAndIncrement: CommandFunction = () => "";

src/commands/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
duplicateAndDecrement,
1414
handleDuplicateAndIncrementDecrement,
1515
updateSelectionsAfterDuplicate,
16+
incrementFloat,
17+
decrementFloat,
1618
} from "./increment-decrement";
1719
import { sequence } from "./sequence";
1820
import { randomCase } from "./random-case";
@@ -49,6 +51,8 @@ export const commandNameFunctionMap: CommandRegistry = {
4951
decrement,
5052
duplicateAndIncrement,
5153
duplicateAndDecrement,
54+
incrementFloat,
55+
decrementFloat,
5256
sequence,
5357
utf8ToChar,
5458
charToUtf8,

src/commands/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const numberFunctionNames = [
2323
"sequence",
2424
"duplicateAndIncrement",
2525
"duplicateAndDecrement",
26+
"incrementFloat",
27+
"decrementFloat",
2628
];
2729

2830
export const functionNamesWithArgument = [

src/test/extension.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,40 @@ suite("Extension Test Suite", () => {
455455
);
456456
});
457457

458+
test("incrementFloat increases all floats in the selection by 1", async () => {
459+
const [output] = await getTextForSelectionsByCommand(
460+
"string-manipulation.incrementFloat",
461+
[
462+
{
463+
start: { line: 53, character: 0 },
464+
end: { line: 53, character: 64 },
465+
},
466+
]
467+
);
468+
469+
assert.strictEqual(
470+
output /* Lorem -1.234 ipsum 5.678 dolor sit amet, consectetur adipiscing. */,
471+
"Lorem -1.233 ipsum 5.679 dolor sit amet, consectetur adipiscing."
472+
);
473+
});
474+
475+
test("decrementFloat decreases all floats in the selection by 1", async () => {
476+
const [output] = await getTextForSelectionsByCommand(
477+
"string-manipulation.decrementFloat",
478+
[
479+
{
480+
start: { line: 54, character: 0 },
481+
end: { line: 54, character: 53 },
482+
},
483+
]
484+
);
485+
486+
assert.strictEqual(
487+
output /* Sed do 9.876 eiusmod -4.321 tempor incididunt labore. */,
488+
"Sed do 9.875 eiusmod -4.322 tempor incididunt labore."
489+
);
490+
});
491+
458492
test("duplicateAndIncrement duplicates selection and increments numbers in duplicate", async () => {
459493
const [output] = await getTextForSelectionsByCommand(
460494
"string-manipulation.duplicateAndIncrement",

0 commit comments

Comments
 (0)