Class: Bizside::StringUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/string_utils.rb

Constant Summary collapse

Characters =
('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a

Class Method Summary collapse

Class Method Details

.compress_doble_byte_space(string) ⇒ Object

複数個の全角スペースを1個に圧縮する



129
130
131
132
# File 'lib/bizside/string_utils.rb', line 129

def self.compress_doble_byte_space(string)
  return nil unless string
  string.gsub(/ +/, ' ')
end

.create_random_alpha_string(length, case_sensitive = false) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/bizside/string_utils.rb', line 7

def self.create_random_alpha_string length, case_sensitive = false
  chars = ('a'..'z').to_a
  chars += ('A'..'Z').to_a if case_sensitive
  chars_length = chars.length

  Array.new(length) { chars[rand(chars_length)] }.join
end

.create_random_string(number) ⇒ Object



15
16
17
18
19
20
# File 'lib/bizside/string_utils.rb', line 15

def self.create_random_string(number)
  (Array.new(number.to_i) do
    Characters[rand(Characters.size)]
  end
  ).join
end

.current_time_string(now = nil) ⇒ Object



62
63
64
65
# File 'lib/bizside/string_utils.rb', line 62

def self.current_time_string(now = nil)
  now = Time.now unless now
  now.instance_eval { '%s%03d' % [strftime('%Y%m%d%H%M%S'), (usec / 1000.0).round] }
end

.equal(s1, s2) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/bizside/string_utils.rb', line 54

def self.equal(s1, s2)
  if is_empty(s1) and is_empty(s2)
    true
  else
    s1 == s2
  end
end

.exclude_query_string(string) ⇒ Object

URLからクエリストリングを除外して文字列を返す。 www.example.com?foo=bar #=> www.example.com



123
124
125
126
# File 'lib/bizside/string_utils.rb', line 123

def self.exclude_query_string(string)
  return string unless string.include?("?")
  string[/^.*(?=\?)/]
end

.is_empty(s) ⇒ Object



50
51
52
# File 'lib/bizside/string_utils.rb', line 50

def self.is_empty(s)
  s.nil? or s.empty?
end

.is_login_id(value) ⇒ Object



75
76
77
78
# File 'lib/bizside/string_utils.rb', line 75

def self.(value)
  return nil if value =~ /\.{2,}/
  value =~ /^[0-9a-z_\.\-]{1,50}$/u
end

.is_not_login_id(value) ⇒ Object



80
81
82
# File 'lib/bizside/string_utils.rb', line 80

def self.(value)
  ! (value)
end

.is_not_user_password(value, minimum, maximum) ⇒ Object



71
72
73
# File 'lib/bizside/string_utils.rb', line 71

def self.is_not_user_password(value,minimum,maximum)
   ! is_user_password(value,minimum,maximum)
end

.is_user_password(value, minimum, maximum) ⇒ Object



67
68
69
# File 'lib/bizside/string_utils.rb', line 67

def self.is_user_password(value,minimum,maximum)
  value =~ /^[!-~]{#{minimum},#{maximum}}$/u
end

.rand_string(length = 8) ⇒ Object

指定した長さのランダム英数字文字列を返す。a-zA-Z0-9。62種類。



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bizside/string_utils.rb', line 102

def self.rand_string(length = 8)
  ret = []
  length.times do |i|
    ret[i] = Characters[rand(Characters.size)]
  end
  ret = ret.join

  return ret if ret =~ /^(?=.*?[a-z])(?=.*?\d).*+$/iu

  # 英字と数字が最低1文字を含むランダム英数字文字列を返すようにする
  alphabets = ('a'..'z').to_a + ('A'..'Z').to_a
  numerics = (0..9).to_a
  ranges = (0...length).to_a
  ret[ranges.slice!(rand(ranges.size))] = alphabets[rand(alphabets.size)]
  ret[ranges.slice!(rand(ranges.size))] = numerics[rand(numerics.size)].to_s

  ret
end

.remove_html_tag(str) ⇒ Object

HTMLタグを除去した文書を返す



85
86
87
88
89
90
# File 'lib/bizside/string_utils.rb', line 85

def self.remove_html_tag(str)
  return nil unless str
  str=str.dup
  str.sub!(/<[^<>]*>/,"") while /<[^<>]*>/ =~ str
  str
end

.remove_rich_html_tag(str) ⇒ Object

リッチテキストの改行を保ったまま、HTMLタグを除去した文書を返す



93
94
95
96
97
98
99
# File 'lib/bizside/string_utils.rb', line 93

def self.remove_rich_html_tag(str)
  return nil unless str

  ret = str.gsub(/<div[^>]*>/, "\n")
  ret = remove_html_tag(ret)
  ret
end

.replace_char_like_hyphen_to(string, to: "\u30FC") ⇒ Object

文字列中に含まれる全角ハイフンっぽい文字を、指定した文字に一括置換する



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bizside/string_utils.rb', line 135

def self.replace_char_like_hyphen_to(string, to: "\u30FC")
  # U+002D Hyphen-Minus
  # U+00AD Soft Hyphen
  # U+02D7 Modifier Letter Minus Sign
  # U+2010 Hyphen
  # U+2011 Non-Breaking Hyphen
  # U+2012 Figure Dash
  # U+2013 En Dash
  # U+2014 Em Dash
  # U+2015 Horizontal Bar
  # U+2043 Hyphen Bullet
  # U+2212 Minus Sign
  # U+2796 Heavy Minus Sign
  # U+30FC Katakana-Hiragana Prolonged Sound Mark
  # U+FE58 Small Em Dash
  # U+FE63 Small Hyphen-Minus
  # U+FF0D Fullwidth Hyphen-Minus
  # U+FF70 Halfwidth Katakana-Hiragana Prolonged Sound Mark
  string.gsub(
    /[\u002D\u00AD\u02D7\u2010\u2011\u2012\u2013\u2014\u2015\u2043\u2212\u2796\u30FC\uFE58\uFE63\uFF0D\uFF70]/,
    to
  )
end

.sjis_to_utf8(s) ⇒ Object



42
43
44
# File 'lib/bizside/string_utils.rb', line 42

def self.sjis_to_utf8(s)
  NKF.nkf('-Swx', s)
end

.to_han(s) ⇒ Object



37
38
39
40
# File 'lib/bizside/string_utils.rb', line 37

def self.to_han(s)
  return s if is_empty(s)
  NKF::nkf('-Wwxm0Z0', s)
end

.to_hiragana(s) ⇒ Object



22
23
24
25
# File 'lib/bizside/string_utils.rb', line 22

def self.to_hiragana(s)
  return s if is_empty(s)
  NKF::nkf('-Ww --hiragana', s)
end

.to_katakana(s) ⇒ Object



27
28
29
30
# File 'lib/bizside/string_utils.rb', line 27

def self.to_katakana(s)
  return s if is_empty(s)
  NKF::nkf('-Ww --katakana', s)
end

.to_zen(s) ⇒ Object



32
33
34
35
# File 'lib/bizside/string_utils.rb', line 32

def self.to_zen(s)
  return s if is_empty(s)
  NKF::nkf('-WwXm0', s)
end

.utf8_to_sjis(s) ⇒ Object



46
47
48
# File 'lib/bizside/string_utils.rb', line 46

def self.utf8_to_sjis(s)
  NKF.nkf('-Wsx', s)
end