aboutsummaryrefslogtreecommitdiff
path: root/src/tools/string-obfuscator/string-obfuscator.model.ts
diff options
context:
space:
mode:
authorGravatar Corentin THOMASSET <corentin.thomasset74@gmail.com> 2023-08-16 23:43:45 +0200
committerGravatar GitHub <noreply@github.com> 2023-08-16 21:43:45 +0000
commitc58d6e34232e199406b39cb258e8106dc6b2f9c1 (patch)
tree3a21bcc5de88be2e260a852bf7d99acdd7b42da1 /src/tools/string-obfuscator/string-obfuscator.model.ts
parentf235dcd6c1aeaf92ad2e1e7125aac76367e85345 (diff)
downloadit-tools-c58d6e34232e199406b39cb258e8106dc6b2f9c1.tar.gz
it-tools-c58d6e34232e199406b39cb258e8106dc6b2f9c1.tar.zst
it-tools-c58d6e34232e199406b39cb258e8106dc6b2f9c1.zip
feat(new tool): string obfuscator (#575)
Diffstat (limited to 'src/tools/string-obfuscator/string-obfuscator.model.ts')
-rw-r--r--src/tools/string-obfuscator/string-obfuscator.model.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/tools/string-obfuscator/string-obfuscator.model.ts b/src/tools/string-obfuscator/string-obfuscator.model.ts
new file mode 100644
index 0000000..7f56dd1
--- /dev/null
+++ b/src/tools/string-obfuscator/string-obfuscator.model.ts
@@ -0,0 +1,35 @@
+import { get } from '@vueuse/core';
+import { type MaybeRef, computed } from 'vue';
+
+export { obfuscateString, useObfuscateString };
+
+function obfuscateString(
+ str: string,
+ { replacementChar = '*', keepFirst = 4, keepLast = 0, keepSpace = true }: { replacementChar?: string; keepFirst?: number; keepLast?: number; keepSpace?: boolean } = {}): string {
+ return str
+ .split('')
+ .map((char, index, array) => {
+ if (keepSpace && char === ' ') {
+ return char;
+ }
+
+ return (index < keepFirst || index >= array.length - keepLast) ? char : replacementChar;
+ })
+ .join('');
+}
+
+function useObfuscateString(
+ str: MaybeRef<string>,
+ config: { replacementChar?: MaybeRef<string>; keepFirst?: MaybeRef<number>; keepLast?: MaybeRef<number>; keepSpace?: MaybeRef<boolean> } = {},
+
+) {
+ return computed(() => obfuscateString(
+ get(str),
+ {
+ replacementChar: get(config.replacementChar),
+ keepFirst: get(config.keepFirst),
+ keepLast: get(config.keepLast),
+ keepSpace: get(config.keepSpace),
+ },
+ ));
+}