Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/ulid/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def encode32
b32 = value.to_s(32)
b32.tr!(B32_RCF4648_FRAGMENT, B32_CROCKFORD_FRAGMENT)
b32.upcase!

return "0#{b32}" if b32.length == 25

b32
b32.rjust(26, "0")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , per discussion on slack, this should be fine as I believe the prior version was just a micro-optimization assuming a single leading zero for 25-char ULIDs generated using the shortest possible representation, but there should be no issue to replace with rjust(26, "0") to handle an arbitrary number of leading zeroes for alternative underlying representations

end

def encode16
Expand Down
8 changes: 8 additions & 0 deletions spec/ulid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
expect(ulid_string).to be_instance_of(String)
end

it 'generates the minimum ULID at the Unix epoch' do
expect(ULID.min_ulid_at(Time.at(0))).to eq("00000000000000000000000000")
end

it 'generates the lowest lexicographical ULID' do
expect(ulid_string).to match(/0000000000000000$/)
end
Expand Down Expand Up @@ -265,6 +269,10 @@
expect(other.bytes).to eq(first.bytes)
expect(other.time).to eq(first.time)
end

it 'pads the lowest possible encoded value to 26 characters' do
expect(ULID.new(0).ulid).to eq("00000000000000000000000000")
end
end

describe 'compared to other ULIDs' do
Expand Down