lib/lite/ruby/string.rb in lite-ruby-1.0.31 vs lib/lite/ruby/string.rb in lite-ruby-1.1.0
- old
+ new
@@ -1,8 +1,10 @@
# frozen_string_literal: false
if Lite::Ruby.configuration.monkey_patches.include?('string')
+ require 'lite/ruby/safe/string' unless defined?(ActiveSupport)
+
class String
TRANSLITERATIONS ||= {
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'Ae',
'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I',
@@ -32,69 +34,37 @@
'ű' => 'u', 'Ų' => 'U', 'ų' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y',
'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z'
}.freeze
def acronym
- dup.acronym!
+ gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
end
def acronym!
- gsub!(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
+ replace(acronym)
end
def any?(*keys)
keys.any? { |key| include?(key) }
end
- def at(position)
- self[position]
- end
-
- def camelize(first_letter = :upper)
- case first_letter
- when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
- when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
- end || modulize
- end
-
- alias camelcase camelize
-
def camelize!(first_letter = :upper)
replace(camelize(first_letter))
end
- alias camelcase! camelize!
-
def capitalized?
capitalize == self
end
- def classify
- dup.classify!
- end
-
def classify!
- sub!(/.*\./, '')
- camelize!
+ replace(classify)
end
- def constantize
- Object.const_get(camelize)
- end
-
- def dasherize
- underscore.tr('_', '-')
- end
-
def dasherize!
replace(dasherize)
end
- def deconstantize
- [0, rindex('::') || 0]
- end
-
def deconstantize!
replace(deconstantize)
end
def dedupe(pattern)
@@ -104,14 +74,10 @@
def dedupe!(pattern)
pattern.each_char { |char| gsub!(/#{Regexp.escape(char)}{2,}/, char) }
self
end
- def demodulize
- gsub(/^.*::/, '')
- end
-
def demodulize!
replace(demodulize)
end
def domain
@@ -135,61 +101,26 @@
offset = options[:offset] || 4
"#{self[0, offset]}#{separator}#{self[-offset, offset]}"
end
- def first(limit = 1)
- if limit.zero?
- ''
- elsif limit >= length
- self
- else
- to(limit - 1)
- end
- end
-
def format(*args)
super(self, *args.flatten)
end
- def from(position)
- self[position..-1]
- end
-
def headerize
squish.each_word(&:capitalize!).join(' ')
end
def headerize!
replace(headerize)
end
- def humanize(capitalize: true)
- dup.humanize!(capitalize: capitalize)
- end
-
def humanize!(capitalize: true)
- underscore!
- gsub!(/_id\z/, '')
- tr!('_', ' ')
- squish!
- gsub!(/([a-z\d]*)/i, &:downcase)
- gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } || self
+ replace(humanize(capitalize: capitalize))
end
- def indent(amount, seperator = ' ')
- dup.indent!(amount, seperator)
- end
-
- def indent!(amount, seperator = ' ')
- if amount >= 0
- gsub!(/^/, seperator * amount)
- else
- gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
- end
- end
-
def index_all(pattern)
pattern = pattern.to_s if pattern.is_a?(Numeric)
arr_indexes = []
srch_index = rindex(pattern)
@@ -204,33 +135,19 @@
def labelize(capitalize: true)
dup.labelize!(capitalize: capitalize)
end
- alias labelcase labelize
-
def labelize!(capitalize: true)
underscore!
tr!('_', ' ')
squish!
gsub!(/([a-z\d]*)/i, &:downcase)
gsub!(/\A\w/) { |str| capitalize ? str.upcase : str }
gsub!(/ id\z/, ' ID') || self
end
- alias labelcase! labelize!
-
- def last(limit = 1)
- if limit.zero?
- ''
- elsif limit >= length
- self
- else
- from(-limit)
- end
- end
-
def lchomp(match)
dup.lchomp!(match)
end
def lchomp!(match)
@@ -265,26 +182,30 @@
def mixedcase?
!upcase? && !downcase?
end
+ def non_possessive
+ dup.non_possessive!
+ end
+
+ def non_possessive!
+ return self unless possessive?
+
+ chomp!("'s") || chomp!("'") || self
+ end
+
def ordinal
to_i.ordinal
end
def ordinalize
to_i.ordinalize
end
- def parameterize(separator: '-')
- dup.parameterize!(separator: separator)
- end
-
def parameterize!(separator: '-')
- underscore!
- gsub!(/\s+/, separator)
- downcase! || self
+ replace(parameterize(separator: separator))
end
def pathize
dup.pathize!
end
@@ -309,20 +230,10 @@
def pop
self[-1]
end
- def non_possessive
- dup.non_possessive!
- end
-
- def non_possessive!
- return self unless possessive?
-
- chomp!("'s") || chomp!("'") || self
- end
-
def possessive
return self if possessive?
possession = end_with?('s') ? "'" : "'s"
"#{self}#{possession}"
@@ -382,20 +293,10 @@
def quote!(type = :double, amount = nil)
replace(quote(type, amount))
end
- def remove(*patterns)
- dup.remove!(*patterns)
- end
-
- def remove!(*patterns)
- patterns.each_with_object(self) do |pat, str|
- pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
- end
- end
-
def remove_tags
dup.remove_tags!
end
def remove_tags!
@@ -482,120 +383,52 @@
gsub!(/[ \-]+/i, '-')
gsub!(/^-|-$/i, '')
downcase! || self
end
- def squish
- dup.squish!
- end
-
- def squish!
- strip!
- gsub!(/\s+/, ' ') || self
- end
-
def sort
chars.sort.join
end
def sort!
replace(sort)
end
- def titleize
- dup.titleize!
+ def transliterate!
+ replace(transliterate)
end
- alias titlecase titleize
-
def titleize!
- underscore!
- humanize!
- gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize } || self
+ replace(titleize)
end
- alias titlecase! titleize!
-
- def to(position)
- self[0..position]
- end
-
- def transliterize
- dup.transliterize!
- end
-
- def transliterize!
- TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
- end
-
- def truncate(truncate_at, options = {})
- return dup unless length > truncate_at
-
+ def truncate_words(word_count, options = {})
omission = options[:omission] || '...'
- seperator = options[:separator]
-
- size_with_room_for_omission = truncate_at - omission.length
-
- stop = if seperator
- rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
- else
- size_with_room_for_omission
- end
-
- "#{self[0, stop]}#{omission}"
- end
-
- # rubocop:disable Layout/LineLength
- def truncate_words(words_count, options = {})
- omission = options[:omission] || '...'
seperator = options[:separator] || /\s+/
seperator = Regexp.escape(seperator.to_s) unless seperator.is_a(Regexp)
- return self unless /\A((?:.+?#{seperator}){#{words_count - 1}}.+?)#{seperator}.*/m.match?(self)
+ return self unless /\A((?:.+?#{seperator}){#{word_count - 1}}.+?)#{seperator}.*/m.match?(self)
"#{::Regexp.last_match(1)}#{omission}"
end
- # rubocop:enable Layout/LineLength
- def underscore
- dup.underscore!
+ def upcase?
+ upcase == self
end
- alias snakecase underscore
-
def underscore!
- camelize!
- gsub!(/::/, '/')
- gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
- gsub!(/([a-z\d])([A-Z])/, '\1_\2')
- tr!('-', '_')
- downcase! || self
+ replace(underscore)
end
- alias snakecase! underscore!
-
def unpollute(delimiter = '^--^--^')
dup.unpollute!(delimiter)
end
def unpollute!(delimiter = '^--^--^')
gsub!(delimiter, '') || self
end
- def upcase?
- upcase == self
- end
-
- def unshift(*patterns)
- patterns.each_with_object('') { |pat, str| str << pat }
- .concat(self)
- end
-
- def unshift!(*patterns)
- replace(unshift(*patterns))
- end
-
def unquote
dup.unquote!
end
def unquote!
@@ -606,10 +439,19 @@
end
self
end
+ def unshift(*patterns)
+ patterns.each_with_object('') { |pat, str| str << pat }
+ .concat(self)
+ end
+
+ def unshift!(*patterns)
+ replace(unshift(*patterns))
+ end
+
def words
split(/\s+/)
end
def words_without_punctuation
@@ -627,10 +469,13 @@
def variablize!
replace(variablize)
end
- alias ends_with? end_with?
- alias starts_with? start_with?
+ alias camelcase! camelize!
+ alias labelcase labelize
+ alias labelcase! labelize!
+ alias snakecase! underscore!
+ alias titlecase! titleize!
end
end