Sha256: a2563ba2b1f8788ba826ea5630bda11e986c55cc985c85d72079d3388ccbdc4f

Contents?: true

Size: 1.98 KB

Versions: 124

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

RSpec.describe :truncate_bytes do
  link :truncate_bytes, from: :ree_string

  it {
    expect(truncate_bytes("👍👍👍👍", 16)).to eq("👍👍👍👍")
    expect(truncate_bytes("👍👍👍👍", 16, omission: '')).to eq("👍👍👍👍")
    expect(truncate_bytes("👍👍👍👍", 16, omission: " ")).to eq("👍👍👍👍")
    expect(truncate_bytes("👍👍👍👍", 16, omission: "🖖")).to eq("👍👍👍👍")

    expect(truncate_bytes("👍👍👍👍", 15)).to eq("👍👍👍…")
    expect(truncate_bytes("👍👍👍👍", 15, omission: '')).to eq("👍👍👍")
    expect(truncate_bytes("👍👍👍👍", 15, omission: " ")).to eq("👍👍👍 ")
    expect(truncate_bytes("👍👍👍👍", 15, omission: "🖖")).to eq("👍👍🖖")

    expect(truncate_bytes("👍👍👍👍", 5)).to eq("…")
    expect(truncate_bytes("👍👍👍👍", 5, omission: '')).to eq("👍")
    expect(truncate_bytes("👍👍👍👍", 5, omission: " ")).to eq("👍 ")
    expect(truncate_bytes("👍👍👍👍", 5, omission: "🖖")).to eq("🖖")

    expect(truncate_bytes("👍👍👍👍", 4)).to eq("…")
    expect(truncate_bytes("👍👍👍👍", 4, omission: '')).to eq("👍")
    expect(truncate_bytes("👍👍👍👍", 4, omission: " ")).to eq(" ")
    expect(truncate_bytes("👍👍👍👍", 4, omission: "🖖")).to eq("🖖")

    expect {
      truncate_bytes("👍👍👍👍", 3, omission: "🖖")
    }.to raise_error(ArgumentError)
  }

  it 'preserves grapheme clusters' do
    expect(truncate_bytes("a ❤️ b", 2, omission: '')).to eq("a ")
    expect(truncate_bytes("a ❤️ b", 3, omission: '')).to eq("a ")
    expect(truncate_bytes("a ❤️ b", 7, omission: '')).to eq("a ")
    expect(truncate_bytes("a ❤️ b", 8, omission: '')).to eq("a ❤️")
    expect(truncate_bytes("a 👩‍❤️‍👩", 13, omission: '')).to eq("a ")
    expect(truncate_bytes("👩‍❤️‍👩", 13, omission: '')).to eq("")
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.64 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.50 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.49 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.48 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.47 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.46 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.45 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb