From 3986aedc586854cafc7bc8072fa7d71344a1efb1 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Mon, 15 Jun 2020 21:23:15 -0700 Subject: [PATCH] feat(common): add utility class Strings with replaceAll This is a method that is used pretty often but not included in the Javascript language itself. What the method does is what you would expect, it finds all occurrences of a substring and replaces all of them with another one. The deafult Javascript String.replace function only does this with the first occurrence by default so it is not a good solution. Signed-off-by: Peter Somogyvari --- packages/cactus-common/src/main/typescript/public-api.ts | 1 + packages/cactus-common/src/main/typescript/strings.ts | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 packages/cactus-common/src/main/typescript/strings.ts diff --git a/packages/cactus-common/src/main/typescript/public-api.ts b/packages/cactus-common/src/main/typescript/public-api.ts index 20a2a56ffe..33356a798f 100755 --- a/packages/cactus-common/src/main/typescript/public-api.ts +++ b/packages/cactus-common/src/main/typescript/public-api.ts @@ -2,3 +2,4 @@ export { LoggerProvider } from "./logging/logger-provider"; export { Logger, ILoggerOptions } from "./logging/logger"; export { LogLevelDesc } from "loglevel"; export { Objects } from "./objects"; +export { Strings } from "./strings"; diff --git a/packages/cactus-common/src/main/typescript/strings.ts b/packages/cactus-common/src/main/typescript/strings.ts new file mode 100644 index 0000000000..54cd57b5a3 --- /dev/null +++ b/packages/cactus-common/src/main/typescript/strings.ts @@ -0,0 +1,9 @@ +export class Strings { + public static replaceAll( + source: string, + searchValue: string, + replaceValue: string + ): string { + return source.replace(new RegExp(searchValue, "gm"), replaceValue); + } +}