Module: CSSModules::Transform

Defined in:
lib/css_modules/transform.rb

Defined Under Namespace

Modules: DevelopmentTransform, ProductionTransform

Constant Summary collapse

HASH_LIMIT =

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

10009
SUM_SIZE =

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

4

Class Method Summary collapse

Class Method Details

.compute_hash(input_string) ⇒ String

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!

Parameters:

  • input_string (String)

    A string to hash

Returns:

  • (String)

    a deterministic output for ‘input_string`



13
14
15
16
17
18
19
20
21
22
23
24
# 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