Skip to content

Commit

Permalink
version 2.1.0
Browse files Browse the repository at this point in the history
- much faster processing of strings containing multiple words
- move utility functions to a separate file
- cleaned up testing
  • Loading branch information
dangowans committed May 14, 2021
1 parent d78551f commit aa7b97d
Show file tree
Hide file tree
Showing 14 changed files with 966 additions and 528 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ npm install @cityssm/unleet

```javascript
unleet("b@d w0rd");
= [ "bad word" ]
// => [ "bad word" ]

unleet("1337 $P33K");
= [ 'ieet zpeek', 'ieet speek', 'leet zpeek', 'leet speek' ]
// => [ 'ieet zpeek', 'ieet speek', 'leet zpeek', 'leet speek' ]

unleet("0rg@ni℠");
= [ 'organizm', 'organism' ]
// => [ 'organizm', 'organism' ]
```

## Contributing
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare const unleet: (leetString: string) => string[];
export declare const unleet: (leetString: string | number) => string[];
export default unleet;
46 changes: 15 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
import diacritic from "diacritic";
import { leetSymbolTranslationKeys, simpleTranslations, complexTranslations } from "./translations/translations.js";
const indiciesOf = (sourceString, searchString) => {
const indicies = [];
for (let index = 0; index < sourceString.length - searchString.length; index += 1) {
if (sourceString.substring(index, searchString.length + index) === searchString) {
indicies.push(index);
}
}
return indicies;
};
const isLetter = (potentialLetter) => {
if ("abcdefghijklmnopqrstuvwxyz".includes(potentialLetter)) {
return true;
}
return false;
};
const isPotentialLeet = (potentialLeetString) => {
for (const leetSymbol of leetSymbolTranslationKeys) {
if (isLetter(leetSymbol)) {
continue;
}
if (potentialLeetString.includes(leetSymbol)) {
return true;
}
}
return false;
};
import * as utils from "./utils.js";
import { simpleTranslations, complexTranslations } from "./translations/translations.js";
const unleetRecurse = (lowerCaseLeetString, unleetStrings, previousStrings, complexTranslationKeys) => {
for (const leetSymbol of complexTranslationKeys) {
if (lowerCaseLeetString.includes(leetSymbol)) {
let matchingIndicies = indiciesOf(lowerCaseLeetString, leetSymbol);
let matchingIndicies = utils.indiciesOf(lowerCaseLeetString, leetSymbol);
if (matchingIndicies.length === 0) {
matchingIndicies = [lowerCaseLeetString.indexOf(leetSymbol)];
}
Expand All @@ -46,7 +21,7 @@ const unleetRecurse = (lowerCaseLeetString, unleetStrings, previousStrings, comp
}
}
}
if (!isPotentialLeet(lowerCaseLeetString)) {
if (!utils.isPotentialLeet(lowerCaseLeetString)) {
unleetStrings.add(lowerCaseLeetString);
return unleetStrings;
}
Expand All @@ -56,7 +31,7 @@ export const unleet = (leetString) => {
if (leetString === null || leetString === undefined || leetString === "") {
return [""];
}
let cleanLeetString = (leetString + "").toLowerCase();
let cleanLeetString = leetString.toString().toLowerCase();
cleanLeetString = cleanLeetString.replace(/\./g, " ");
cleanLeetString = cleanLeetString.replace(/ +/g, " ");
cleanLeetString = diacritic.clean(cleanLeetString);
Expand All @@ -68,5 +43,14 @@ export const unleet = (leetString) => {
const complexTranslationKeys = Object.keys(complexTranslations).filter(function (leetSymbol) {
return cleanLeetString.includes(leetSymbol);
});
return Array.from(unleetRecurse(cleanLeetString.trim(), new Set(), new Set(), complexTranslationKeys));
const cleanLeetStringSplit = cleanLeetString.split(" ");
const unleetResults = [];
for (const cleanLeetStringPiece of cleanLeetStringSplit) {
unleetResults.push(Array.from(unleetRecurse(cleanLeetStringPiece.trim(), new Set(), new Set(), complexTranslationKeys)));
}
if (unleetResults.length === 1) {
return unleetResults[0];
}
return utils.combineStringArrays(unleetResults);
};
export default unleet;
69 changes: 22 additions & 47 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
import diacritic from "diacritic";

import { leetSymbolTranslationKeys, simpleTranslations, complexTranslations } from "./translations/translations.js";


const indiciesOf = (sourceString: string, searchString: string) => {

const indicies: number[] = [];

for (let index = 0; index < sourceString.length - searchString.length; index += 1) {

if (sourceString.substring(index, searchString.length + index) === searchString) {
indicies.push(index);
}
}

return indicies;
};


const isLetter = (potentialLetter: string) => {
if ("abcdefghijklmnopqrstuvwxyz".includes(potentialLetter)) {
return true;
}
return false;
};


const isPotentialLeet = (potentialLeetString: string) => {

for (const leetSymbol of leetSymbolTranslationKeys) {

if (isLetter(leetSymbol)) {
continue;
}

if (potentialLeetString.includes(leetSymbol)) {
return true;
}
}

return false;
};
import * as utils from "./utils.js";
import { simpleTranslations, complexTranslations } from "./translations/translations.js";


const unleetRecurse = (lowerCaseLeetString: string,
Expand All @@ -54,7 +14,7 @@ const unleetRecurse = (lowerCaseLeetString: string,
// If the current leet symbol is found in the string
if (lowerCaseLeetString.includes(leetSymbol)) {

let matchingIndicies = indiciesOf(lowerCaseLeetString, leetSymbol);
let matchingIndicies = utils.indiciesOf(lowerCaseLeetString, leetSymbol);

if (matchingIndicies.length === 0) {
matchingIndicies = [lowerCaseLeetString.indexOf(leetSymbol)];
Expand All @@ -81,7 +41,7 @@ const unleetRecurse = (lowerCaseLeetString: string,
}

// If no leet symbols exist in the string that are able to be translated
if (!isPotentialLeet(lowerCaseLeetString)) {
if (!utils.isPotentialLeet(lowerCaseLeetString)) {
unleetStrings.add(lowerCaseLeetString);
return unleetStrings;
}
Expand All @@ -90,14 +50,14 @@ const unleetRecurse = (lowerCaseLeetString: string,
};


export const unleet = (leetString: string): string[] => {
export const unleet = (leetString: string | number): string[] => {

if (leetString === null || leetString === undefined || leetString === "") {
return [""];
}

// Convert to lower case
let cleanLeetString = (leetString + "").toLowerCase();
let cleanLeetString = leetString.toString().toLowerCase();

// Remove periods
cleanLeetString = cleanLeetString.replace(/\./g, " ");
Expand All @@ -120,5 +80,20 @@ export const unleet = (leetString: string): string[] => {
return cleanLeetString.includes(leetSymbol);
});

return Array.from(unleetRecurse(cleanLeetString.trim(), new Set(), new Set(), complexTranslationKeys));
const cleanLeetStringSplit = cleanLeetString.split(" ");

const unleetResults: string[][] = [];

for (const cleanLeetStringPiece of cleanLeetStringSplit) {
unleetResults.push(Array.from(unleetRecurse(cleanLeetStringPiece.trim(), new Set(), new Set(), complexTranslationKeys)));
}

if (unleetResults.length === 1) {
return unleetResults[0];
}

return utils.combineStringArrays(unleetResults);
};


export default unleet;
Loading

0 comments on commit aa7b97d

Please sign in to comment.