Skip to content

Instantly share code, notes, and snippets.

@maugravena
Created June 11, 2017 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maugravena/0340828e9587352deb93ca4d004d7747 to your computer and use it in GitHub Desktop.
Save maugravena/0340828e9587352deb93ca4d004d7747 to your computer and use it in GitHub Desktop.
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
}
@suissa
Copy link

suissa commented Jun 14, 2017

Minha refatoração

const NOT = x => !x
const getCharCode = String.fromCharCode
const isSpace = ( x ) => ( x === 32 )
const isInRangeA_Z = ( x ) => ( ( x >= 65 ) &&  (x <= 90 ) )
const toCharCode = ( letter, i, str ) => 
  str.join( '' ).charCodeAt( i )

const cryptOutRangeA_Z = ( x ) =>
  ( x >= 78 ) ? getCharCode( x - 13 )  : getCharCode( x + 13 ) 

const cryptThis = ( x ) => 
  NOT ( isInRangeA_Z( x ) ) ? getCharCode( x ) : cryptOutRangeA_Z( x )

const toCypher = ( result, x, i ) => 
  ( isSpace( x ) )
    ? result.concat( getCharCode( x ) )
    : result.concat( cryptThis( x ) )

const rot13 = ( str ) => 
  str.toUpperCase()
      .split( '' )
      .map( toCharCode )
      .reduce( toCypher, '' )


console.log('rot13 LBH QVQ VG!', rot13( 'LBH QVQ VG!' ) )
console.log('rot13 suissa', rot13( 'suissa' ) )

@suissa
Copy link

suissa commented Jun 14, 2017

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