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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ dist
# Vite logs files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

# MacOS
.DS_Store
src/.DS_Store
tests/.DS_Store
2 changes: 2 additions & 0 deletions docs/STR.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ brutils str extract "\[(.*?)\]" --text "[one] [two]" --regex
brutils str base64 --text "hello" --mode encode
brutils str urlencode --text "hello world" --mode encode
brutils str html --text "<strong>ok</strong>" --mode encode
brutils str leven "kitten" "sitting"
```

## Actions
Expand All @@ -39,6 +40,7 @@ brutils str html --text "<strong>ok</strong>" --mode encode
| `base64` | `brutils str base64 --text <value> [--mode <mode>]` | Encode or decode Base64. |
| `urlencode` | `brutils str urlencode --text <value> [--mode <mode>]` | Encode or decode URL content. |
| `html` | `brutils str html --text <value> [--mode <mode>]` | Encode or decode HTML entities. |
| `leven` | `brutils str leven <value> <value>` | Calculates the Levenshtein distance between two strings. |

## Flags

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@danielarndt0/brutils-cli",
"version": "1.0.1",
"version": "1.1.0",
"description": "Production-ready CLI for Brazilian and general developer utilities.",
"type": "module",
"main": "./dist/index.js",
Expand Down
13 changes: 12 additions & 1 deletion src/cli/commands/register-text-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
convertStringCase,
extractText,
getLevenshteinDistance,
normalizeText,
padText,
removeAccents,
Expand Down Expand Up @@ -58,7 +59,8 @@ export function registerTextDataCommands(program: Command): void {
'brutils str truncate --text "hello world" --max 8 --suffix "..."',
'brutils str replace --text "hello 123" --from "\\\\d+" --with "X" --regex',
'brutils str extract "\\\\[(.*?)\\\\]" --text "[one] [two]" --regex',
'brutils str base64 --text "hello" --mode encode'
'brutils str base64 --text "hello" --mode encode',
'brutils str leven "kitten" "sitting"'
])
);

Expand Down Expand Up @@ -268,6 +270,15 @@ export function registerTextDataCommands(program: Command): void {
}
);

str
.description("Calculates the Levenshtein distance between two strings")
.command("leven")
.argument("<a>")
.argument("<b>")
.action((a: string, b: string) => {
printValue(getLevenshteinDistance(a, b));
});

const jsonCommand = program
.command("json")
.description("Local JSON formatting, editing and diff helpers.")
Expand Down
2 changes: 1 addition & 1 deletion src/cli/create-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { registerTextDataCommands } from "./commands/register-text-data.js";
import { rootFooter } from "./shared/help.js";
import { configureProgramUi } from "./ui/output.js";

const CLI_VERSION = "1.0.1";
const CLI_VERSION = "1.1.0";

export function buildProgram(): Command {
const program = new Command();
Expand Down
30 changes: 30 additions & 0 deletions src/services/str/str.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,34 @@ export function transformHtmlEntities(
}

return encodeHtmlEntities(value);

}

export function getLevenshteinDistance(a: string, b: string): number {
a = a.trim()
b = b.trim()

const n = a.length;
const m = b.length;

const dp: number[][] = Array.from({ length: n + 1 }, () =>
new Array(m + 1).fill(0)
);

for (let i = 0; i <= n; i++) dp[i]![0] = i;
for (let j = 0; j <= m; j++) dp[0]![j] = j;

for (let i = 1; i <= n; i++) {
for (let j = 1; j <= m; j++) {
const cost = a[i - 1] === b[j - 1] ? 0 : 1;

dp[i]![j] = Math.min(
dp[i - 1]![j]! + 1,
dp[i]![j - 1]! + 1,
dp[i - 1]![j - 1]! + cost
);
}
}

return dp[n]![m]!;
}
27 changes: 26 additions & 1 deletion tests/str/str.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,35 @@ import {
transformHtmlEntities,
transformUrlEncoding,
trimText,
truncateText
truncateText,
getLevenshteinDistance
} from "../../src/services/str/index.js";

describe("str service", () => {
it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("kitten", "sitting")).toBe(3);
});

it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("book", "back")).toBe(2);
});

it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("cat", "cut")).toBe(1);
});

it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("", "")).toBe(0);
});

it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("Hello".toLowerCase(), "hello")).toBe(0);
});

it("should give me the distance between two strings", () => {
expect(getLevenshteinDistance("João", "Joao")).toBe(1);
});

it("should slugify text", () => {
expect(slugifyText("Hello Cool World")).toBe("hello-cool-world");
});
Expand Down
Loading