Sha256: a2563ba2b1f8788ba826ea5630bda11e986c55cc985c85d72079d3388ccbdc4f

Contents?: true

Size: 1.98 KB

Versions: 125

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

125 entries across 125 versions & 1 rubygems

Version Path
ree_lib-1.0.45 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.44 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.43 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.42 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.41 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.40 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.39 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.38 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.37 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.36 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.35 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.34 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.33 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.32 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.31 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.30 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.29 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.28 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.27 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.26 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb