Sha256: 7ac4f14406c5649c0e43c1dcd935a56d0012ef3f1e45fc655d13d4003661ba01

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Transmutation
  module StringRefinements # rubocop:disable Style/Documentation
    DELIMITERS = %r{[a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+}

    refine String do
      def camelcase
        return capitalize unless match? DELIMITERS

        split(%r{\s*-\s*|\s*/\s*|\s*:+\s*}).then { |parts| combine parts, :capitalize, "::" }
                                           .then { |text| text.split(/\s*_\s*|\s+/) }
                                           .then { |parts| combine parts, :capitalize }
      end

      def first(maximum = 0)
        return self if empty?
        return self[0] if maximum.zero?
        return "" if maximum.negative?

        self[..(maximum - 1)]
      end

      def capitalize = empty? ? self : first.upcase + self[1, size]

      private

      def combine(parts, method, delimiter = "")
        parts.reduce "" do |result, part|
          next part.public_send method if result.empty?

          "#{result}#{delimiter}#{part.__send__ method}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transmutation-0.1.1 lib/transmutation/string_refinements.rb
transmutation-0.1.0 lib/transmutation/string_refinements.rb