Sha256: f1ed3704fb966c44fb344433ad49f782207625c7e2fa6398380459021d4dea08

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module ToPass
  # The StringConversions-Module is a collection of available
  # Transformations. Every method is given a string as single argument
  # and is expected to return a transformed string. The only exception
  # from this is general rule is the replace method which also takes a
  # replacement table.
  #
  # This enables chaining and eases extending the capabilities.
  module StringConversions
    # all the blanks are removed.
    #
    # this is useful if you convert a sentence into a password.
    def collapse_chars(string)
      string = string.split(' ').join('')
    end

    # reduces every word to its first character, preserving case
    def first_chars(string)
      string.split(' ').map do |word|
        word[0].chr
      end.join(' ')
    end

    # alternate case of letter (not numbers)
    def swapcase(string)
      pwd = ""
      last_upcase = true

      string.each_char do |char|
        char = if char.between?("0", "9")
                 char
               elsif last_upcase
                 last_upcase = false
                 char.downcase
               else
                 last_upcase = true
                 char.upcase
               end
        pwd << char
      end
      pwd
    end

    # perform replacements on a string, based on a replacment table
    def replace(string, table)
      table.inject(string) do |pwd, map|
        pwd = pwd.gsub(/#{map[0].to_s}/, map[1].to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
to_pass-0.2.3 lib/to_pass/string_conversions.rb
to_pass-0.2.2 lib/to_pass/string_conversions.rb