lite-ruby 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,14 @@ Returns the closest number outside of the lower and upper bound.
18
18
  8.clamp(3, 6) # => 6
19
19
  ```
20
20
 
21
+ `close?`
22
+ ------
23
+ Returns if a number is within the degree of another.
24
+
25
+ ```ruby
26
+ 10.006.close?(10, 0.1) #=> true
27
+ ```
28
+
21
29
  `decrement`
22
30
  ------
23
31
  Returns the n decremented number.
@@ -27,13 +35,31 @@ Returns the n decremented number.
27
35
  1.decrement(0.5) #=> 0.5
28
36
  ```
29
37
 
30
- `distance`
38
+ `delta`
31
39
  ------
32
40
  Returns the absolute difference between numbers.
33
41
 
42
+ ```ruby
43
+ 5.delta(3) #=> 2
44
+ 3.delta(5) #=> 2
45
+ ```
46
+
47
+ `delimit`
48
+ ------
49
+ Returns a string representation of the number.
50
+
51
+ ```ruby
52
+ 5.delimit #=> "1,000,000.1234"
53
+ 3.delimit(delimiter: '.', separator: ',') #=> "1.000.000,1234"
54
+ ```
55
+
56
+ `distance`
57
+ ------
58
+ Returns the difference between numbers.
59
+
34
60
  ```ruby
35
61
  5.distance(3) #=> 2
36
- 3.distance(5) #=> 2
62
+ 3.distance(5) #=> -2
37
63
  ```
38
64
 
39
65
  `divide`
@@ -234,6 +260,14 @@ Returns the nth power of a number.
234
260
  4.power(2) #=> 16
235
261
  ```
236
262
 
263
+ `range` aka `plus_minus`
264
+ ------
265
+ Returns a range from the number plus-or-minus a given value.
266
+
267
+ ```ruby
268
+ 4.range(2) #=> 2..6
269
+ ```
270
+
237
271
  `root`
238
272
  ------
239
273
  Returns the nth root of a number.
@@ -0,0 +1,51 @@
1
+ # OpenStruct
2
+
3
+ `Initialize`
4
+ Allows the initialization of an OpenStruct with a setter block.
5
+
6
+ ```ruby
7
+ OpenStruct.new(name: 'bob', age: 60) do |o|
8
+ o.gender = :M
9
+ end
10
+ ```
11
+
12
+ `[]`
13
+ ------
14
+ Access a value in the OpenStruct by key, like a Hash.
15
+
16
+ ```ruby
17
+ person = OpenStruct.new(name: 'bob', age: 60)
18
+
19
+ person['name'] #=> 'bob'
20
+ ```
21
+
22
+ `[]=`
23
+ ------
24
+ Store a value in the OpenStruct by key, like a Hash.
25
+
26
+ ```ruby
27
+ person = OpenStruct.new(name: 'bob', age: 60)
28
+
29
+ person['name'] = 'tim'
30
+ ```
31
+
32
+ `attributes`
33
+ ------
34
+ Returns the key values of the assigned struct.
35
+
36
+ ```ruby
37
+ person = OpenStruct.new(name: 'bob', age: 60)
38
+
39
+ person.attributes #=> { name: 'bob', age: 60 }
40
+ ```
41
+
42
+ `replace`
43
+ ------
44
+ Replaces values provided within the hash.
45
+
46
+ ```ruby
47
+ person = OpenStruct.new(name: 'bob', age: 60)
48
+
49
+ person.replace(name: 'tom', age: 28)
50
+ preson.name #=> 'tom'
51
+ ```
@@ -17,6 +17,15 @@ Returns if a string includes a set of string(s).
17
17
  'example string'.any?('foo', 'string') #=> true
18
18
  ```
19
19
 
20
+ `ascii_only(!)`
21
+ ------
22
+ Replace non-ascii characters.
23
+
24
+ ```ruby
25
+ '中文123'.ascii_only #=> '123'
26
+ '中文123'.ascii_only!('x') #=> 'xx123'
27
+ ```
28
+
20
29
  `at`
21
30
  ------
22
31
  Returns the characters at index position, matching string, or regex.
@@ -41,6 +50,17 @@ Transforms a string to camelcase.
41
50
  'example_String'.camecase(:lower) #=> 'exampleString'
42
51
  ```
43
52
 
53
+ `capitalized?`
54
+ ------
55
+ Returns true if the first character is capitalized.
56
+
57
+ ```ruby
58
+ 'Example string'.capitalized? #=> true
59
+ 'EXAMPLE STRING'.capitalized? #=> false
60
+ 'example string'.capitalized? #=> false
61
+ 'Example String'.capitalized? #=> false
62
+ ```
63
+
44
64
  `classify(!)`
45
65
  ------
46
66
  Transforms a string to a class name like Rails does for table names to models.
@@ -107,6 +127,23 @@ Returns true if all characters are lowercase.
107
127
  'EXAMPLE'.downcase? #=> false
108
128
  ```
109
129
 
130
+ `each_word`
131
+ ------
132
+ Splits a string into multiple words and yields its enumeration.
133
+
134
+ ```ruby
135
+ 'abc. 123'.each_word(&:campitalize!) #=> ['Abc.', '123']
136
+ ```
137
+
138
+ `encode_only(!)`
139
+ ------
140
+ Replace non-encode characters.
141
+
142
+ ```ruby
143
+ '中文123'.encode_only('UTF-8') #=> '123'
144
+ '中文123'.encode_only!('ASCII', 'x') #=> 'xx123'
145
+ ```
146
+
110
147
  `ellipsize`
111
148
  ------
112
149
  Truncate a string in the middle.
@@ -217,6 +254,32 @@ than or equal to the string length, Returns a copy of self.
217
254
  'example'.first(3) #=> 'ple'
218
255
  ```
219
256
 
257
+ `lchomp(!)`
258
+ ------
259
+ Left chomp.
260
+
261
+ ```ruby
262
+ 'example'.lchomp('e') #=> 'xample'
263
+ 'example'.lchomp!('z') #=> 'example'
264
+ ```
265
+
266
+ `methodize(!)`
267
+ ------
268
+ Translate a class or module name to a suitable method name.
269
+
270
+ ```ruby
271
+ 'Example::ClassString'.methodize #=> 'example__class_string'
272
+ ```
273
+
274
+ `modulize(!)`
275
+ ------
276
+ Converts a string to module name representation.
277
+
278
+ ```ruby
279
+ 'example_string'.modulize #=> 'ExampleString'
280
+ 'example/string'.modulize! #=> 'Example::String'
281
+ ```
282
+
220
283
  `mixcase?`
221
284
  ------
222
285
  Returns true if characters are mixedcase.
@@ -258,6 +321,14 @@ Makes a string suitable for a dashed url parameter string.
258
321
  'example_string'.parameterize(separator: '?') #=> 'example?string'
259
322
  ```
260
323
 
324
+ `pathize(!)`
325
+ ------
326
+ Transforms a string to a suitable file path.
327
+
328
+ ```ruby
329
+ 'ExamplePathize::Example'.pathize! #=> 'example_string/class'
330
+ ```
331
+
261
332
  `pollute(!)`
262
333
  ------
263
334
  Pollutes the space between every letter in a string, so it will be exempt from any impending string searches.
@@ -283,6 +354,16 @@ Concats string to self.
283
354
  'test'.push('er') #=> 'tester'
284
355
  ```
285
356
 
357
+ `quote(!)`
358
+ ------
359
+ Adds a given quote type to the string.
360
+
361
+ ```ruby
362
+ 'example'.quote #=> '"example"'
363
+ 'example'.quote!(1) #=> "'example'"
364
+ 'example'.quote(:backtick) #=> '`example`'
365
+ ```
366
+
286
367
  `remove(!)`
287
368
  ------
288
369
  Removes every instance of a string.
@@ -304,6 +385,15 @@ Removes HTML tags from a string.
304
385
  'this is <b>bold</b> and <em>emphatic</em>'.remove_tags #=> 'this is bold and emphatic'
305
386
  ```
306
387
 
388
+ `rotate(!)`
389
+ ------
390
+ Rotate string to the left with count.
391
+
392
+ ```ruby
393
+ 'example'.rotate #=> 'xamplee'
394
+ 'example'.rotate(2) #=> 'ampleex'
395
+ ```
396
+
307
397
  `sample(!)`
308
398
  ------
309
399
  Removes a random value and returns that value.
@@ -432,7 +522,7 @@ separator | string | ' '
432
522
  'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)') #=> 'And they found that many... (continued)'
433
523
  ```
434
524
 
435
- `underscore(!)`
525
+ `underscore(!)` aka `snakecase(!)`
436
526
  ------
437
527
  Transforms a string to snake case.
438
528
 
@@ -469,3 +559,35 @@ Prepends string(s) to self.
469
559
  'this thing that thing'.unshift('thing ') #=> 'thing this thing that thing'
470
560
  'this thing that thing'.unshift('this ', 'that ') #=> 'this that this thing that thing'
471
561
  ```
562
+
563
+ `unquote(!)`
564
+ ------
565
+ Removes any quote types from a given string.
566
+
567
+ ```ruby
568
+ '`example`'.unquote #=> 'example'
569
+ ```
570
+
571
+ `variablize(!)`
572
+ ------
573
+ Prepend an "@" to the beginning of a string and replace non-valid characters with underscores.
574
+
575
+ ```ruby
576
+ 'example String'.variablize #=> '@example_String'
577
+ ```
578
+
579
+ `words`
580
+ ------
581
+ Splits a string into multiple words split by spaces.
582
+
583
+ ```ruby
584
+ 'abc. 123'.words #=> ['abc.', '123']
585
+ ```
586
+
587
+ `words_without_punctuation`
588
+ ------
589
+ Splits a string into multiple words without punctuation split by spaces.
590
+
591
+ ```ruby
592
+ 'Slowly, grudgingly he said: "This has to stop."'.words_without_punctuation #=> ['Slowly', 'grudgingly', 'he', 'said', 'This', 'has', 'to', 'stop']
593
+ ```
@@ -1,5 +1,29 @@
1
1
  # Struct
2
2
 
3
+ `[]`
4
+ ------
5
+ Access a value in the Struct by key, like a Hash.
6
+
7
+ ```ruby
8
+ person = Struct.new(:name, :age)
9
+ person.new('bob', 60)
10
+
11
+ person['name'] #=> 'bob'
12
+ ```
13
+
14
+ `[]=`
15
+ ------
16
+ Store a value in the Struct by key, like a Hash.
17
+
18
+ ```ruby
19
+ person = Struct.new(:name, :age)
20
+ person.new('bob', 60)
21
+
22
+ person['name'] = 'tim'
23
+
24
+ person['name'] #=> 'tim'
25
+ ```
26
+
3
27
  `attributes`
4
28
  ------
5
29
  Returns the key values of the assigned struct.
@@ -2,6 +2,6 @@
2
2
 
3
3
  Lite::Ruby.configure do |config|
4
4
  config.monkey_patches = %w[
5
- array date enumerable hash integer kernel numeric object range string struct time
5
+ array date enumerable hash integer kernel numeric object open_struct range string struct time
6
6
  ]
7
7
  end
@@ -9,7 +9,8 @@ module Lite
9
9
 
10
10
  def initialize
11
11
  @monkey_patches = %w[
12
- array date enumerable hash integer kernel numeric object range string struct time
12
+ array date enumerable hash integer kernel numeric object open_struct range string struct
13
+ time
13
14
  ]
14
15
  end
15
16
 
@@ -14,19 +14,15 @@ module Enumerable
14
14
  end
15
15
  end
16
16
 
17
- def deduce(identity = 0, &block)
18
- if block_given?
19
- map(&block).deduce(identity)
20
- else
21
- inject { |key, val| key - val } || identity
22
- end
17
+ def cluster_by(&block)
18
+ group_by(&block).sort.transpose.pop || []
23
19
  end
24
20
 
25
- def divisible(identity = 0, &block)
21
+ def deduce(identity = 0, &block)
26
22
  if block_given?
27
- map(&block).divisible(identity)
23
+ map(&block).deduce(identity)
28
24
  else
29
- inject { |key, val| key / val } || identity
25
+ inject { |acc, val| acc - val } || identity
30
26
  end
31
27
  end
32
28
 
@@ -76,10 +72,16 @@ module Enumerable
76
72
  if block_given?
77
73
  map(&block).exponential(identity)
78
74
  else
79
- inject { |key, val| key**val } || identity
75
+ inject { |acc, val| acc**val } || identity
80
76
  end
81
77
  end
82
78
 
79
+ def frequency
80
+ each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
81
+ end
82
+
83
+ alias occurrences frequency
84
+
83
85
  # rubocop:disable Style/CaseEquality
84
86
  def incase?(object)
85
87
  any? { |val| object === val }
@@ -125,16 +127,57 @@ module Enumerable
125
127
  end
126
128
  end
127
129
 
128
- def multiple(identity = 0, &block)
130
+ def modulate(modulo)
131
+ if modulo == 1
132
+ to_a
133
+ elsif size % modulo != 0
134
+ raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
135
+ else
136
+ (0...size).each_with_object(Array.new(modulo, [])) do |i, array|
137
+ array[i % modulo] += [self[i]]
138
+ end
139
+ end
140
+ end
141
+
142
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
143
+ def occur(amount = nil)
144
+ result = Hash.new { |hash, key| hash[key] = [] }
145
+
146
+ each do |item|
147
+ key = item
148
+ result[key] << item
149
+ end
150
+
151
+ if block_given?
152
+ result.select! { |_key, values| yield(values.size) }
153
+ else
154
+ raise ArgumentError, 'Invalid occur amount' unless amount
155
+
156
+ if amount.is_a?(Range)
157
+ result.select! { |_key, values| amount.include?(values.size) }
158
+ else
159
+ result.select! { |_key, values| values.size == amount }
160
+ end
161
+ end
162
+
163
+ result.values.flatten.uniq
164
+ end
165
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
166
+
167
+ def produce(identity = 0, &block)
129
168
  if block_given?
130
- map(&block).multiple(identity)
169
+ map(&block).produce(identity)
131
170
  else
132
- inject { |key, val| key * val } || identity
171
+ inject { |acc, val| acc * val } || identity
133
172
  end
134
173
  end
135
174
 
136
- def occurrences
137
- each_with_object(Hash.new(0)) { |key, hsh| hsh[key] += 1 }
175
+ def quotient(identity = 0, &block)
176
+ if block_given?
177
+ map(&block).quotient(identity)
178
+ else
179
+ inject { |acc, val| acc / val } || identity
180
+ end
138
181
  end
139
182
 
140
183
  def several?
@@ -149,6 +192,23 @@ module Enumerable
149
192
  found_count > 1
150
193
  end
151
194
 
195
+ # rubocop:disable Metrics/MethodLength
196
+ def squeeze(*limited_to)
197
+ first = true
198
+ current = nil
199
+
200
+ each_with_object([]) do |val, array|
201
+ if !limited_to.empty? && !limited_to.include?(val)
202
+ array << val
203
+ elsif first || current != val
204
+ array << val
205
+ first = false
206
+ current = val
207
+ end
208
+ end
209
+ end
210
+ # rubocop:enable Metrics/MethodLength
211
+
152
212
  def take_last(num)
153
213
  collection_size = to_a.size
154
214
  return self if num > collection_size