lx 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +34 -0
  3. data/lib/lx.rb +197 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d4c0434d776d126749bc25f0261862ef735b77ac115e65fa53b81c4d37910e0b
4
+ data.tar.gz: 327b023d673ed23f7281d61d85001b9a2c1e577ed2b0c5ba571f245c4b8d1883
5
+ SHA512:
6
+ metadata.gz: d46c1fd25072f59cb72eff63d3b323c86749cdb522ac17c225509397227be60f2fc9e92ac695467866170244f6318128c2b744394b1e755f0f25ffb7ce440838
7
+ data.tar.gz: 35bbb8fb3bd0a20c9eaae80685bf255ebb3374950230c28771e5a7a1bdff0b9cccffc46938af062990d2d52359a3d813281e5051947eda274fcc7bcac136e424
@@ -0,0 +1,34 @@
1
+ # LX
2
+
3
+ OK, let's just be honest here. These are some cool little utilities I cooked up
4
+ for myself. I think they're the coolest thing since sliced bread. You probably
5
+ won't agree. That's how we programmers are, always besmirching each other's work
6
+ because it doesn't meet our individual ideas of what constitutes making the
7
+ world a better place.
8
+
9
+ Mainly, I'm posting this library on RubyGems because I use it in some other
10
+ libraries that I'm going to upload.
11
+
12
+ ## Install
13
+
14
+ The usual:
15
+
16
+ ``
17
+ gem install lx
18
+ ``
19
+
20
+ ## Author
21
+
22
+ Mike O'Sullivan
23
+ mike@idocs.com
24
+
25
+ ## Name
26
+
27
+ "LX" doesn't mean anything in particular. It's concise and it was available on
28
+ rubygems.rb.
29
+
30
+ ## History
31
+
32
+ | version | date | notes |
33
+ |---------|--------------|-------------------------------|
34
+ | 1.0 | May 29, 2020 | Initial upload. |
@@ -0,0 +1,197 @@
1
+ #===============================================================================
2
+ # LX
3
+ #
4
+
5
+ # The LX module is the home for utilities that don't particularly fit in one
6
+ # class. For now, it only contains the randstr method.
7
+ module LX
8
+ # version '1.0'
9
+ VERSION = '1.0'
10
+
11
+ #---------------------------------------------------------------------------
12
+ # verbose
13
+ # @verbose = true
14
+ #
15
+ # def self.verbose
16
+ # return @verbose
17
+ # end
18
+ #
19
+ # def self.verbose=(bool)
20
+ # return @verbose = bool ? true : false
21
+ # end
22
+ #
23
+ # verbose
24
+ #---------------------------------------------------------------------------
25
+
26
+
27
+ #---------------------------------------------------------------------------
28
+ # randstr
29
+ #
30
+
31
+ # characters for random string
32
+ @@RAND_CHARS = %w(
33
+ b c d f h m p q r s t v w x z
34
+ B C D F H M P Q R S T V W X Z
35
+ 2 3 4 5 7 8 9
36
+ )
37
+
38
+ # length of random character string
39
+ @@RND_CHARS_MAX = @@RAND_CHARS.length - 1
40
+
41
+ # target length
42
+ @@RND_PK_LENGTH = 20
43
+
44
+ # Returns a random string of characters. The optional single param is the
45
+ # length of the string to return. By default, the string is 20 characters
46
+ # long.
47
+ def self.randstr(len=@@RND_PK_LENGTH)
48
+ # intialize rv
49
+ rv = ''
50
+
51
+ # build return string
52
+ len.times do
53
+ rv += @@RAND_CHARS[rand 0 .. @@RND_CHARS_MAX]
54
+ end
55
+
56
+ # freeze return value
57
+ rv.freeze
58
+
59
+ # return
60
+ return rv
61
+ end
62
+ #
63
+ # randstr
64
+ #---------------------------------------------------------------------------
65
+ end
66
+ #
67
+ # LX
68
+ #===============================================================================
69
+
70
+
71
+ #===============================================================================
72
+ # Array
73
+ #
74
+
75
+ # Adds a single method to the Array class. This is the only method that is
76
+ # added to the Array class itself. Access LX utilities through the array's new
77
+ # +lx+ method.
78
+ #
79
+ # For example, the use the move method, call the method like this:
80
+ # arr = ['a', 'b', 'c']
81
+ # arr.lx.move 1, 2
82
+ class Array
83
+ # Creates a LX::Array object and returns it.
84
+ def lx
85
+ return LX::Array.new(self)
86
+ end
87
+ end
88
+
89
+ # A helper class to add methods to the Array class. When you use the Array::lx
90
+ # you're creating an instance of LX::Array.
91
+ class LX::Array
92
+ # Initialize a LX::Array object. The only param is the array itself.
93
+ def initialize(p_arr)
94
+ @arr = p_arr
95
+ end
96
+
97
+ # Moves an array element from the old index to the new index.
98
+ # arr = ['a', 'b', 'c']
99
+ # arr.lx.move 1, 2 # => ["a", "c", "b"]
100
+ def move(old_idx, new_idx)
101
+ @arr.insert new_idx, @arr.slice!(1)
102
+ end
103
+
104
+ # Splits an array into an array of arrays. The param is the delimiter.
105
+ # arr = ['Fred', 'George', ':', 'Mary', 'Frank']
106
+ # arr.lx.split ':' # => [["Fred", "George"], ["Mary", "Frank"]]
107
+ def split(exp)
108
+ # $tm.hrm
109
+ rv = []
110
+ current = nil
111
+
112
+ # loop through array looking for delimiter
113
+ @arr.each do |el|
114
+ if el == exp
115
+ if current
116
+ rv.push current
117
+ current = nil
118
+ end
119
+ else
120
+ current ||= []
121
+ current.push el
122
+ end
123
+ end
124
+
125
+ # add last current
126
+ if current
127
+ rv.push current
128
+ end
129
+
130
+ # return
131
+ return rv
132
+ end
133
+ end
134
+ #
135
+ # Array
136
+ #===============================================================================
137
+
138
+
139
+ #===============================================================================
140
+ # String
141
+ #
142
+
143
+ # Adds the +lx+ method to the String class. To call LX utilities on the string,
144
+ # call its lx method. For example, call +collapse+ like this:
145
+ # str = ' Whatever Dude '
146
+ # str.lx.collapse # => "Whatever Dude"
147
+ class String
148
+ # Creates and returns an LX::String object.
149
+ def lx
150
+ return LX::String.new(self)
151
+ end
152
+ end
153
+
154
+ # Utilities for strings.
155
+ class LX::String
156
+ #---------------------------------------------------------------------------
157
+ # initialize
158
+ #
159
+
160
+ # Initializes a LX::String object, The single param is the string with which
161
+ # the object will be associated.
162
+ def initialize(p_str)
163
+ @str = p_str
164
+ end
165
+ #
166
+ # initialize
167
+ #---------------------------------------------------------------------------
168
+
169
+
170
+ #---------------------------------------------------------------------------
171
+ # collapse
172
+ #
173
+
174
+ # Returns a new string in which leading and trailing whitespace have been
175
+ # removed, and multiple internal whitespaces are collapsed into into single
176
+ # spaces.
177
+ # str = ' Whatever Dude '
178
+ # str.lx.collapse # => "Whatever Dude"
179
+ def collapse()
180
+ rv = @str.dup
181
+ rv.lx.collapse!
182
+ return rv
183
+ end
184
+
185
+ # Performs a collapse on the string itself.
186
+ def collapse!()
187
+ @str.sub!(/\A[^[:graph:]]+/mu, '')
188
+ @str.sub!(/[^[:graph:]]+\z/mu, '')
189
+ @str.gsub!(/[^[:graph:]]+/mu, ' ')
190
+ end
191
+ #
192
+ # collapse
193
+ #---------------------------------------------------------------------------
194
+ end
195
+ #
196
+ # String
197
+ #===============================================================================
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lx
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Mike O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Misc utilities for strings and arrays
14
+ email: mike@idocs.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/lx.rb
21
+ homepage: https://rubygems.org/gems/lx
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.6
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Misc utilities
45
+ test_files: []