module CSSModules::Transform

Constants

HASH_LIMIT

Hash outputs will be within the range `0..HASH_LIMIT-1`

SUM_SIZE

How big of a chunk should we use for folding over the string?

Public Class Methods

compute_hash(input_string) click to toggle source

Generate a short, random-ish token for `input_string`. This has to be replicable in JS. Ruby's `#hash` is randomly seeded, so we can't reuse that! @param input_string [String] A string to hash @return [String] a deterministic output for `input_string`

# File lib/css_modules/transform.rb, line 13
def self.compute_hash(input_string)
  bytes_count = 0
  string_sum = 0

  input_string.each_byte do |byte|
    string_sum += byte * (256 ** bytes_count)
    bytes_count += 1
    bytes_count %= SUM_SIZE
  end

  string_sum % HASH_LIMIT
end