Sha256: 6952de84592e758cd691a8fa8a77b9192f3a6ec98528dc362899dfbe3ac202e1

Contents?: true

Size: 1.19 KB

Versions: 27

Compression:

Stored size: 1.19 KB

Contents

require 'volt/extra_core/inflector'

class String
  # TODO: replace with better implementations
  # NOTE: strings are currently immutable in Opal, so no ! methods

  # Turns a string into the camel case version.  If it is already camel case, it should
  # return the same string.
  def camelize(first_letter = :upper)
    new_str = gsub(/[_\-][a-z]/) { |a| a[1].upcase }
    new_str = new_str[0].capitalize + new_str[1..-1] if first_letter == :upper

    new_str
  end

  # Returns the underscore version of a string.  If it is already underscore, it should
  # return the same string.
  def underscore
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
  end

  def dasherize
    gsub('_', '-')
  end

  def pluralize
    Volt::Inflector.pluralize(self)
  end

  def singularize
    Volt::Inflector.singularize(self)
  end

  def titleize
    gsub('_', ' ').split(' ').map(&:capitalize).join(' ')
  end

  def headerize
    split(/[_-]/)
      .map { |new_str| new_str[0].capitalize + new_str[1..-1] }
      .join('-')
  end

  def plural?
    # TODO: Temp implementation
    pluralize == self
  end

  def singular?
    # TODO: Temp implementation
    singularize == self
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
volt-0.9.6 lib/volt/extra_core/string.rb
volt-0.9.6.pre3 lib/volt/extra_core/string.rb
volt-0.9.6.pre2 lib/volt/extra_core/string.rb
volt-0.9.6.pre1 lib/volt/extra_core/string.rb
volt-0.9.5 lib/volt/extra_core/string.rb
volt-0.9.5.pre12 lib/volt/extra_core/string.rb
volt-0.9.5.pre11 lib/volt/extra_core/string.rb
volt-0.9.5.pre9 lib/volt/extra_core/string.rb
volt-0.9.5.pre8 lib/volt/extra_core/string.rb
volt-0.9.5.pre7 lib/volt/extra_core/string.rb
volt-0.9.5.pre6 lib/volt/extra_core/string.rb
volt-0.9.5.pre5 lib/volt/extra_core/string.rb
volt-0.9.5.pre4 lib/volt/extra_core/string.rb
volt-0.9.5.pre3 lib/volt/extra_core/string.rb
volt-0.9.5.pre2 lib/volt/extra_core/string.rb
volt-0.9.5.pre1 lib/volt/extra_core/string.rb
volt-0.9.4 lib/volt/extra_core/string.rb
volt-0.9.4.pre5 lib/volt/extra_core/string.rb
volt-0.9.4.pre3 lib/volt/extra_core/string.rb
volt-0.9.4.pre2 lib/volt/extra_core/string.rb