diff --git a/lib/faker/default/string.rb b/lib/faker/default/string.rb index c3ccb69327..bfcece20a3 100644 --- a/lib/faker/default/string.rb +++ b/lib/faker/default/string.rb @@ -3,6 +3,22 @@ module Faker class String < Base class << self + ## + # Produces a random UTF-8 string with optional nested length selectors. + # + # @param length [Integer, Range, Array] The length of produced string and/or specific UTF-8 characters to use. + # @return [String] + # + # @example + # rubocop:disable Style/AsciiComments + # Faker::String.random #=> "3 뇦\u0017&y\u{3A109}$8^4* 녹豿4좘툢ꔾ쉙6ɉ\uA6 8TN畀챵|\"3쇤Ŵ" + # Faker::String.random(length: 4) #=> "⼨%0*" + # Faker::String.random(length: 3..12) #=> "\u{69FDC};秨툫" + # Faker::String.random(length: [0, 6]) #=> "I轤𣴒P溟L" + # Faker::String.random(length: [1, (2..5), [3, 6], nil]) #=> "葓L#ћ" + # rubocop:enable Style/AsciiComments + # + # @faker.version 1.9.0 def random(legacy_length = NOT_GIVEN, length: 32) warn_for_deprecated_arguments do |keywords| keywords << :length if legacy_length != NOT_GIVEN @@ -38,9 +54,9 @@ def char_space_ratio def utf8character sample([ - rand(32..90), # latin alphabet - rand(128), # 7-bit ASCII - rand(0xd800), # utf-8 codepoints below utf-16 surrogate halves + rand(32..90), # latin alphabet + rand(128), # 7-bit ASCII + rand(0xd800), # utf-8 codepoints below utf-16 surrogate halves rand(57_344..1_114_111) # utf-8 codepoints above utf-16 surrogate halves ]).chr(Encoding::UTF_8) end