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.124 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_string/spec/ree_string/functions/truncate_bytes_spec.rb