Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replace common non-typeable characters with typeable versions.
/*
Name: StringCleanse
Author: SweptThrone (sweptthr.one)
Description: Replaces non-typeable characters in a provided string with typeable versions.
Returns: The cleansed string, or the original string if no valid non-typeable characters were found.
Argument: The string to cleanse.
*/
function StringCleanse( str ) {
// dirtyRegex is a huge range of letters and numbers that are not typeable, u tag helps us grab them all
let dirtyRegex = /[\u{1D400}-\u{1D6A3}\u{1D7CE}-\u{1D7FF}\u{FF21}-\u{FF5A}\u{FF10}-\u{FF19}\u{24B6}-\u{24E9}\u{2460}-\u{2468}\u{24EA}]/gmu
let res = str.match( dirtyRegex )
let finalStr = str
if ( res != null ) {
for ( i = 0; i < res.length; i++ ) {
let code = res[ i ].codePointAt( 0 )
if ( code >= 0x1D400 && code <= 0x1D6A3 ) { // letters
// sub is the number we have to subtract from our "bad" letter code
// it will be different for each "set" of invalid letters so that it always ends up in the range of typeable letters
let sub = 0
let iter = 0
for ( comp = 0x1D419; comp <= 0x1D6D7; comp += 52 ) { // comp = 119833; comp <= 120483
if ( code <= comp - 26 ) { // lowercase letters
sub = 119743 + ( 52 * iter ) - 58
break
} else if ( code <= comp ) { // capital letters
sub = 119743 + ( 52 * iter )
break
}
iter++
}
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - sub ) )
} else if ( code >= 0x1D7CE && code <= 0x1D7FF ) { // numbers
// sub is the number we have to subtract from our "bad" number code
// it will be different for each "set" of invalid numbers so that it always ends up in the range of typeable numbers
let sub = 0
let iter = 0
for ( comp = 0x1D7D7; comp <= 0x1D809; comp += 10 ) { // comp = 120791; comp <= 120841
if ( code <= comp ) { // numbers
sub = 120734 + ( 10 * iter )
break
}
iter++
}
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - sub ) )
// these are more consistent because they're only in one range
// but that one range is always out of range of the others
} else if ( code >= 0xFF21 && code <= 0xFF5A ) { // fullwidth letters
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - 0xFEE0 ) )
} else if ( code >= 0xFF10 && code <= 0xFF19 ) { // fullwidth numbers
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - 0xFEE0 ) )
} else if ( code >= 0x24B6 && code <= 0x24E9 ) { // circled letters
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - ( code > 0x24CF ? 0x246F : 0x2475 ) ) )
} else if ( ( code >= 0x2460 && code <= 0x2468 ) || code == 0x24EA ) { // circled numbers, 0 out of range for some reason
finalStr = finalStr.replace( res[ i ], String.fromCodePoint( code - ( code == 0x24EA ? 0x24BA : 0x242F ) ) )
}
}
return finalStr
} else {
return str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment