Module: Random.self::Formatter

Defined in:
opal/opal/corelib/random/formatter.rb

Instance Method Summary collapse

Instance Method Details

#alphanumeric(count = nil) ⇒ Object



114
115
116
117
118
119
120
# File 'opal/opal/corelib/random/formatter.rb', line 114

def alphanumeric(count = nil)
  count = Random._verify_count(count)
  map = ['0'..'9', 'a'..'z', 'A'..'Z'].map(&:to_a).flatten
  ::Array.new(count) do |i|
    map[random_number(map.length)]
  end.join
end

#base64(count = nil) ⇒ Object



22
23
24
# File 'opal/opal/corelib/random/formatter.rb', line 22

def base64(count = nil)
  ::Base64.strict_encode64(random_bytes(count)).encode('US-ASCII')
end

#hex(count = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'opal/opal/corelib/random/formatter.rb', line 6

def hex(count = nil)
  count = ::Random._verify_count(count)
  %x{
    var bytes = #{bytes(count)};
    var out = "";
    for (var i = 0; i < #{count}; i++) {
      out += bytes.charCodeAt(i).toString(16).padStart(2, '0');
    }
    return #{`out`.encode('US-ASCII')};
  }
end

#random_bytes(count = nil) ⇒ Object



18
19
20
# File 'opal/opal/corelib/random/formatter.rb', line 18

def random_bytes(count = nil)
  bytes(count)
end

#random_floatObject

Implemented in terms of #bytes for SecureRandom, but Random overrides this method to implement #bytes in terms of #random_float. Not part of standard Ruby interface - use random_number for portability.



42
43
44
45
46
47
48
49
50
# File 'opal/opal/corelib/random/formatter.rb', line 42

def random_float
  bs = bytes(4)
  num = 0
  4.times do |i|
    num <<= 8
    num |= bs[i].ord
  end
  num.abs / 0x7fffffff
end

#random_number(limit = undefined) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'opal/opal/corelib/random/formatter.rb', line 52

def random_number(limit = undefined)
  %x{
    function randomFloat() {
      return #{random_float};
    }

    function randomInt(max) {
      return Math.floor(randomFloat() * max);
    }

    function randomRange() {
      var min = limit.begin,
          max = limit.end;

      if (min === nil || max === nil) {
        return nil;
      }

      var length = max - min;

      if (length < 0) {
        return nil;
      }

      if (length === 0) {
        return min;
      }

      if (max % 1 === 0 && min % 1 === 0 && !limit.excl) {
        length++;
      }

      return randomInt(length) + min;
    }

    if (limit == null) {
      return randomFloat();
    } else if (limit.$$is_range) {
      return randomRange();
    } else if (limit.$$is_number) {
      if (limit <= 0) {
        #{::Kernel.raise ::ArgumentError, "invalid argument - #{limit}"}
      }

      if (limit % 1 === 0) {
        // integer
        return randomInt(limit);
      } else {
        return randomFloat() * limit;
      }
    } else {
      limit = #{::Opal.coerce_to!(limit, ::Integer, :to_int)};

      if (limit <= 0) {
        #{::Kernel.raise ::ArgumentError, "invalid argument - #{limit}"}
      }

      return randomInt(limit);
    }
  }
end

#urlsafe_base64(count = nil, padding = false) ⇒ Object



26
27
28
# File 'opal/opal/corelib/random/formatter.rb', line 26

def urlsafe_base64(count = nil, padding = false)
  ::Base64.urlsafe_encode64(random_bytes(count), padding).encode('US-ASCII')
end

#uuidObject



30
31
32
33
34
35
36
37
# File 'opal/opal/corelib/random/formatter.rb', line 30

def uuid
  str = hex(16).split('')
  str[12] = '4'
  str[16] = `(parseInt(#{str[16]}, 16) & 3 | 8).toString(16)`
  str = [str[0...8], str[8...12], str[12...16], str[16...20], str[20...32]]
  str = str.map(&:join)
  str.join('-')
end