mirror of
https://github.com/sbrow/strings.git
synced 2025-12-29 15:17:38 -05:00
feat: Added obfuscate.
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
nodejs
|
nodejs
|
||||||
yarn
|
yarn
|
||||||
|
|
||||||
|
nodePackages.typescript-language-server
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,6 @@
|
|||||||
"typedoc": "^0.24.7",
|
"typedoc": "^0.24.7",
|
||||||
"typedoc-plugin-markdown": "^3.15.3",
|
"typedoc-plugin-markdown": "^3.15.3",
|
||||||
"typescript": "^5.0.4",
|
"typescript": "^5.0.4",
|
||||||
"vitest": "^0.31.1"
|
"vitest": "^2.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/obfuscate.spec.ts
Normal file
16
src/obfuscate.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { obfuscate } from "./obfuscate";
|
||||||
|
|
||||||
|
describe('obfuscate', function() {
|
||||||
|
test.each([
|
||||||
|
['brower.spencer@gmail.com', [/^.{3}/, '@', '.com'], ['co'], 'bro•••••••••••@•••••.••m'],
|
||||||
|
['brower.spencer@gmail.com', [/^.{3}/, '@', '.com'], [], 'bro•••••••••••@•••••.com']
|
||||||
|
])('%# works', function (str, allow, deny, want) {
|
||||||
|
const got = obfuscate(str, allow, deny);
|
||||||
|
|
||||||
|
expect(want.length).toEqual(str.length)
|
||||||
|
|
||||||
|
expect(got).toBe(want);
|
||||||
|
});
|
||||||
|
});
|
||||||
56
src/obfuscate.ts
Normal file
56
src/obfuscate.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
export type Patterns = Array<string | RegExp>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces all but the first `length` characters of a string with `•`.
|
||||||
|
*
|
||||||
|
* @param {string} string The string to obfuscate.
|
||||||
|
* @param {string[]} whitelistPatterns
|
||||||
|
* @return {string} The obfuscated string.
|
||||||
|
*/
|
||||||
|
export function obfuscate(
|
||||||
|
string: string,
|
||||||
|
whitelistPatterns: Patterns = [],
|
||||||
|
blacklistPatterns: Patterns = []
|
||||||
|
): string {
|
||||||
|
return [
|
||||||
|
whitelist(whitelistPatterns),
|
||||||
|
blacklist(blacklistPatterns)
|
||||||
|
].reduce(
|
||||||
|
(str, fn) => fn(str),
|
||||||
|
string
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergePatterns(patterns: Patterns): string {
|
||||||
|
return patterns
|
||||||
|
.map((pattern) =>
|
||||||
|
typeof pattern === "string" ? escapeRegExp(pattern) : pattern.source
|
||||||
|
)
|
||||||
|
.join("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
function whitelist(patterns: Patterns) {
|
||||||
|
const whitelistPattern = RegExp(mergePatterns(patterns), "dgm");
|
||||||
|
|
||||||
|
return (string: string) =>
|
||||||
|
Array.from(string.matchAll(whitelistPattern))
|
||||||
|
.map((result) => [result[0], result.indices[0][0]])
|
||||||
|
.reduce(
|
||||||
|
(carry, [word, index]) => carry.padEnd(index, "\u2022") + word,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function blacklist(patterns: Patterns) {
|
||||||
|
const blacklistPattern = new RegExp(mergePatterns(patterns), "dgm");
|
||||||
|
//return string => Array.from(string.matchAll(blacklistPattern)).filter(x => x[0].length)
|
||||||
|
return (string: string) =>
|
||||||
|
string.replaceAll(blacklistPattern, (match: string) => {
|
||||||
|
return "".padEnd(match.length, "\u2022");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function escapeRegExp(string: string): string {
|
||||||
|
return string.replace(/[.*+?^${}()|[/\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user