Module: Random::Formatter
Instance Method Summary collapse
- #alphanumeric(count = nil) ⇒ Object
- #base64(count = nil) ⇒ Object
- #hex(count = nil) ⇒ Object
- #random_bytes(count = nil) ⇒ Object
-
#random_float ⇒ Object
Implemented in terms of
#bytes
for SecureRandom, but Random overrides this method to implement#bytes
in terms of#random_float
. - #random_number(limit = undefined) ⇒ Object
- #urlsafe_base64(count = nil, padding = false) ⇒ Object
- #uuid ⇒ Object
Instance Method Details
#alphanumeric(count = nil) ⇒ Object
[View source]
111 112 113 114 115 116 117 |
# File 'opal/opal/corelib/random/formatter.rb', line 111 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
[View source]
19 20 21 |
# File 'opal/opal/corelib/random/formatter.rb', line 19 def base64(count = nil) Base64.strict_encode64(random_bytes(count)).encode('US-ASCII') end |
#hex(count = nil) ⇒ Object
[View source]
3 4 5 6 7 8 9 10 11 12 13 |
# File 'opal/opal/corelib/random/formatter.rb', line 3 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
[View source]
15 16 17 |
# File 'opal/opal/corelib/random/formatter.rb', line 15 def random_bytes(count = nil) bytes(count) end |
#random_float ⇒ Object
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.
39 40 41 42 43 44 45 46 47 |
# File 'opal/opal/corelib/random/formatter.rb', line 39 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
[View source]
49 50 51 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 |
# File 'opal/opal/corelib/random/formatter.rb', line 49 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) { #{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) { #{raise ArgumentError, "invalid argument - #{limit}"} } return randomInt(limit); } } end |
#urlsafe_base64(count = nil, padding = false) ⇒ Object
[View source]
23 24 25 |
# File 'opal/opal/corelib/random/formatter.rb', line 23 def urlsafe_base64(count = nil, padding = false) Base64.urlsafe_encode64(random_bytes(count), padding).encode('US-ASCII') end |
#uuid ⇒ Object
[View source]
27 28 29 30 31 32 33 34 |
# File 'opal/opal/corelib/random/formatter.rb', line 27 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 |