Sha256: ed5b597a22a421dd545a70ba7add448b2fed63e543b714a33b6809cfaf0bdd60

Contents?: true

Size: 819 Bytes

Versions: 1

Compression:

Stored size: 819 Bytes

Contents

require 'md5'

module ToPass::Converters
  class ExpandBelow
    class << self
      # Strings below the threshold are expanded with a md5-hash of them.
      #
      # The md5 is used to generate extra characters which are then added
      # before and after the string.
      def expand_below(string, rules, threshold)
        if string.length < threshold.to_i
          digest = "#{MD5.hexdigest(string)}#{MD5.hexdigest(string).reverse}"
          extension = 1.upto(digest.length / 2).map do |nr|
            char = digest[(nr*2-2),2].to_i(16).chr
            char if char =~ /\w/i
          end.compact.join

          part1 = extension[0,(extension.length/2).floor]
          part2 = extension.gsub(part1,'')

          part1 + string + part2
        else
          string
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
to_pass-0.6.0 data/to_pass/converters/expand_below.rb