Created
June 11, 2017 01:29
-
-
Save maugravena/0340828e9587352deb93ca4d004d7747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rot13(str) { // LBH QVQ VG! | |
var valoresUnicode = [] | |
for (let i in str) { | |
valoresUnicode.push(str.charCodeAt(i)) | |
} | |
var str13 = valoresUnicode.map((x) => { | |
if (x === 32) return String.fromCharCode(x) //preserva o espaço | |
if (x >= 65 && x <=90) { // range A-Z | |
if (x >= 78) return String.fromCharCode(x - 13) //Maior que 'N' | |
if (x <= 78) return String.fromCharCode(x + 13) //Menor que 'N' | |
} else { | |
return String.fromCharCode(x) //Demais caracteres ñ aplica mudança | |
} | |
}).join('') | |
return str13 | |
} |
Mais uma refatoração da refatoração ehhehehhe
const CYPHER_LIMIT = 78
const A = 65
const Z = 90
const add = ( c ) => ( s ) => s.concat( c )
const getCharCode = String.fromCharCode
const isSpace = ( x ) => ( x === 32 )
const isInRange = ( min, max ) => ( x ) =>
( ( x >= min ) && ( x <= max ) )
const toCharCode = ( letter, i, str ) =>
str.join( '' ).charCodeAt( i )
const getPosition = ( CYPHER_LENGTH ) => ( x ) =>
( x >= CYPHER_LIMIT ) ? x - CYPHER_LENGTH : x + CYPHER_LENGTH
const getCharCodeFromCypher = ( CYPHER_LENGTH ) => ( x ) =>
( isInRange( A, Z )( x ) ) ? getPosition( CYPHER_LENGTH )( x ) : x
const cypherThis = ( CYPHER_LENGTH ) => ( x, isSpace ) =>
getCharCode( isSpace ? x : getCharCodeFromCypher( CYPHER_LENGTH )( x ) )
const toCypher = ( CYPHER_LENGTH ) => ( result, x, i ) =>
add ( cypherThis( CYPHER_LENGTH )( x ), isSpace( x ) )( result )
const rot = ( CYPHER_LENGTH ) => ( str ) =>
str.toUpperCase()
.split( '' )
.map( toCharCode )
.reduce( toCypher( CYPHER_LENGTH ), '' )
const rot13 = rot( 13 )
console.log('rot13 LBH QVQ VG!', rot13( 'LBH QVQ VG!' ) )
console.log('rot13 suissa', rot13( 'suissa' ) )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minha refatoração