casual_support 1.0.0 → 2.0.0
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.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +52 -17
- data/Rakefile +13 -0
- data/benchmarks/comparable/at_least.rb +9 -0
- data/benchmarks/comparable/at_most.rb +9 -0
- data/benchmarks/hash/putbang.rb +13 -0
- data/benchmarks/integer/to_hex.rb +34 -0
- data/benchmarks/string/after.rb +22 -0
- data/benchmarks/string/after_last.rb +22 -0
- data/benchmarks/string/before.rb +23 -0
- data/benchmarks/string/before_last.rb +22 -0
- data/benchmarks/string/between.rb +21 -0
- data/benchmarks/string/drop.rb +18 -0
- data/benchmarks/string/first.rb +23 -0
- data/benchmarks/string/from.rb +15 -0
- data/benchmarks/string/last.rb +15 -0
- data/benchmarks/string/prefix.rb +12 -0
- data/benchmarks/string/suffix.rb +12 -0
- data/benchmarks/string/to.rb +15 -0
- data/benchmarks/time/to_hms.rb +12 -0
- data/benchmarks/time/to_ymd.rb +12 -0
- data/casual_support.gemspec +4 -3
- data/lib/casual_support.rb +8 -8
- data/lib/casual_support/comparable.rb +3 -0
- data/lib/casual_support/comparable/at_least.rb +11 -0
- data/lib/casual_support/comparable/at_most.rb +13 -0
- data/lib/casual_support/comparable/clamp.rb +16 -0
- data/lib/casual_support/date.rb +1 -1
- data/lib/casual_support/date/to_ymd.rb +1 -1
- data/lib/casual_support/enumerable.rb +2 -2
- data/lib/casual_support/enumerable/index_to.rb +2 -2
- data/lib/casual_support/hash.rb +1 -1
- data/lib/casual_support/hash/putbang.rb +15 -0
- data/lib/casual_support/integer.rb +1 -1
- data/lib/casual_support/integer/to_hex.rb +2 -8
- data/lib/casual_support/string.rb +13 -4
- data/lib/casual_support/string/after.rb +15 -0
- data/lib/casual_support/string/after_last.rb +14 -0
- data/lib/casual_support/string/before.rb +14 -0
- data/lib/casual_support/string/before_last.rb +15 -0
- data/lib/casual_support/string/between.rb +16 -0
- data/lib/casual_support/string/drop.rb +2 -1
- data/lib/casual_support/string/first.rb +17 -0
- data/lib/casual_support/string/from.rb +8 -0
- data/lib/casual_support/string/last.rb +18 -0
- data/lib/casual_support/string/lchomp.rb +16 -0
- data/lib/casual_support/string/prefix.rb +13 -0
- data/lib/casual_support/string/suffix.rb +13 -0
- data/lib/casual_support/string/to.rb +9 -0
- data/lib/casual_support/time.rb +2 -1
- data/lib/casual_support/time/to_hms.rb +14 -0
- data/lib/casual_support/time/to_ymd.rb +14 -0
- data/lib/casual_support/version.rb +1 -1
- metadata +71 -18
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/lib/casual_support/hash/setbang.rb +0 -16
- data/lib/casual_support/numeric.rb +0 -2
- data/lib/casual_support/numeric/at_least_most.rb +0 -21
- data/lib/casual_support/numeric/constrain.rb +0 -16
- data/lib/casual_support/string/before_after.rb +0 -59
- data/lib/casual_support/string/first_last.rb +0 -15
- data/lib/casual_support/string/from_to.rb +0 -15
- data/lib/casual_support/time/ymd_hms.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a1a0d4b85acc5ebea33e00189e06584cdba7c1d
|
4
|
+
data.tar.gz: f7622e76c1255b6058499d9fa96c5cfa23ba101e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4938a0152fa5a60b49be69f3e76ac26a96e988478a789933de3d1ca999e05d38220d0b71f46e8f5909c230c264837b5053e84f405ad7a7bb3c8d11e13c7a87cd
|
7
|
+
data.tar.gz: ef2a424c9e9ef9bfb2039d7c3e02f2c736fffc9dd5fa274d8c231d6173ee29765e351d5f3e7df36852c843855893fcf3e47ec3d56475f4d0698504744db00529
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,32 +1,67 @@
|
|
1
1
|
# casual_support
|
2
2
|
|
3
|
-
Utility methods as extensions to Ruby core objects
|
3
|
+
Utility methods as extensions to Ruby core objects, a la Active Support.
|
4
|
+
|
5
|
+
Also a nod to the epic [Facets] library, which has a vast quantity and
|
6
|
+
wide variety of utility functions. casual_support distinguishes itself
|
7
|
+
in a few ways:
|
8
|
+
|
9
|
+
- Focus on fast implementations. See the benchmarks directory for
|
10
|
+
performance comparisons with typical and alternative implementations.
|
11
|
+
- Low memory footprint without requiring cherry-picking. Cherry-picking
|
12
|
+
is still possible though!
|
13
|
+
- Intended to be used alongside Active Support. Does not clash with
|
14
|
+
Active Support methods, and overrides only a few to provide more
|
15
|
+
performant implementations.
|
16
|
+
|
17
|
+
[Facets]: https://github.com/rubyworks/facets
|
18
|
+
|
19
|
+
|
20
|
+
## Utility Methods
|
21
|
+
|
22
|
+
- Comparable
|
23
|
+
- [at_least](http://www.rubydoc.info/gems/casual_support/Comparable%3Aat_least)
|
24
|
+
- [at_most](http://www.rubydoc.info/gems/casual_support/Comparable%3Aat_most)
|
25
|
+
- [clamp](http://www.rubydoc.info/gems/casual_support/Comparable%3Aclamp)
|
26
|
+
- Date
|
27
|
+
- [to_ymd](http://www.rubydoc.info/gems/casual_support/Date%3Ato_ymd)
|
28
|
+
- Enumerable
|
29
|
+
- [duplicates](http://www.rubydoc.info/gems/casual_support/Enumerable%3Aduplicates)
|
30
|
+
- [index_to](http://www.rubydoc.info/gems/casual_support/Enumerable%3Aindex_to)
|
31
|
+
- Hash
|
32
|
+
- [put!](http://www.rubydoc.info/gems/casual_support/Hash%3Aput%21)
|
33
|
+
- Integer
|
34
|
+
- [to_hex](http://www.rubydoc.info/gems/casual_support/Integer%3Ato_hex)
|
35
|
+
- String
|
36
|
+
- [after](http://www.rubydoc.info/gems/casual_support/String%3Aafter)
|
37
|
+
- [after_last](http://www.rubydoc.info/gems/casual_support/String%3Aafter_last)
|
38
|
+
- [before](http://www.rubydoc.info/gems/casual_support/String%3Abefore)
|
39
|
+
- [before_last](http://www.rubydoc.info/gems/casual_support/String%3Abefore_last)
|
40
|
+
- [between](http://www.rubydoc.info/gems/casual_support/String%3Abetween)
|
41
|
+
- [drop](http://www.rubydoc.info/gems/casual_support/String%3Adrop)
|
42
|
+
- [first](http://www.rubydoc.info/gems/casual_support/String%3Afirst)
|
43
|
+
- [from](http://www.rubydoc.info/gems/casual_support/String%3Afrom)
|
44
|
+
- [last](http://www.rubydoc.info/gems/casual_support/String%3Alast)
|
45
|
+
- [lchomp](http://www.rubydoc.info/gems/casual_support/String%3Alchomp)
|
46
|
+
- [prefix](http://www.rubydoc.info/gems/casual_support/String%3Aprefix)
|
47
|
+
- [suffix](http://www.rubydoc.info/gems/casual_support/String%3Asuffix)
|
48
|
+
- [to](http://www.rubydoc.info/gems/casual_support/String%3Ato)
|
49
|
+
- Time
|
50
|
+
- [to_hms](http://www.rubydoc.info/gems/casual_support/Time%3Ato_hms)
|
51
|
+
- [to_ymd](http://www.rubydoc.info/gems/casual_support/Time%3Ato_ymd)
|
4
52
|
|
5
53
|
|
6
54
|
## Installation
|
7
55
|
|
8
|
-
Add this line to your application's Gemfile:
|
9
|
-
|
10
|
-
```ruby
|
11
|
-
gem 'casual_support'
|
12
|
-
```
|
13
|
-
|
14
|
-
And then execute:
|
15
|
-
|
16
|
-
$ bundle
|
17
|
-
|
18
|
-
Or install it yourself as:
|
19
|
-
|
20
56
|
$ gem install casual_support
|
21
57
|
|
22
58
|
|
23
59
|
## Development
|
24
60
|
|
25
|
-
|
26
|
-
|
27
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
61
|
+
Run `rake test` to run the tests. You can also run `rake irb` for an
|
62
|
+
interactive prompt that pre-loads the project code.
|
28
63
|
|
29
64
|
|
30
65
|
## License
|
31
66
|
|
32
|
-
|
67
|
+
[MIT License](http://opensource.org/licenses/MIT)
|
data/Rakefile
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rake/testtask"
|
3
|
+
require "yard"
|
4
|
+
|
5
|
+
|
6
|
+
desc "Launch IRB with this gem pre-loaded"
|
7
|
+
task :irb do
|
8
|
+
require "casual_support"
|
9
|
+
require "irb"
|
10
|
+
ARGV.clear
|
11
|
+
IRB.start
|
12
|
+
end
|
13
|
+
|
14
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
15
|
+
end
|
3
16
|
|
4
17
|
Rake::TestTask.new(:test) do |t|
|
5
18
|
t.libs << "test"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
Benchmark.inputs([[1], [*1..10], [*1..100]]) do |job|
|
6
|
+
job.report('reduce{ put! }') do |ary|
|
7
|
+
ary.reduce({}){|h, i| h.put!(i, i) }
|
8
|
+
end
|
9
|
+
job.report('reduce{ merge! }') do |ary|
|
10
|
+
ary.reduce({}){|h, i| h.merge!(i => i) }
|
11
|
+
end
|
12
|
+
job.compare!
|
13
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class Integer
|
6
|
+
|
7
|
+
def to_hex_alt1(width = nil)
|
8
|
+
h = self.to_s(16)
|
9
|
+
width ? h.rjust(width, '0'.freeze) : h
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hex_alt2(width = 0)
|
13
|
+
h = self.to_s(16)
|
14
|
+
width > h.length ? h.rjust(width, '0'.freeze) : h
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
Benchmark.inputs([*0..8]) do |job|
|
21
|
+
job.report('Integer#to_hex'){|width| (210).to_hex(width) }
|
22
|
+
job.report('Integer#to_hex_alt1'){|width| (210).to_hex_alt1(width) }
|
23
|
+
job.report('Integer#to_hex_alt2'){|width| (210).to_hex_alt2(width) }
|
24
|
+
job.report('sprintf'){|width| sprintf("%0#{width}x", 210) }
|
25
|
+
job.compare!
|
26
|
+
end
|
27
|
+
|
28
|
+
Benchmark.inputs([nil]) do |job|
|
29
|
+
job.report('Integer#to_hex'){|width| (210).to_hex }
|
30
|
+
job.report('Integer#to_hex_alt1'){|width| (210).to_hex_alt1 }
|
31
|
+
job.report('Integer#to_hex_alt2'){|width| (210).to_hex_alt2 }
|
32
|
+
job.report('Integer#to_s'){|width| (210).to_s(16) }
|
33
|
+
job.compare!
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
|
7
|
+
def after_alt1(delim)
|
8
|
+
i = self.index(delim)
|
9
|
+
i && self.drop(i + delim.length)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
STRING = 'abcnoonxyz'
|
16
|
+
|
17
|
+
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
+
job.report('String#after'){|delim| STRING.after(delim) }
|
19
|
+
job.report('String#after_alt1'){|delim| STRING.after_alt1(delim) }
|
20
|
+
job.report('String#split'){|delim| STRING.split(delim, 2)[1] }
|
21
|
+
job.compare!
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
|
7
|
+
def after_last_alt1(delim)
|
8
|
+
i = self.rindex(delim)
|
9
|
+
i && self.drop(i + delim.length)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
STRING = 'abcnoonxyz'
|
16
|
+
|
17
|
+
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
+
job.report('String#after_last'){|delim| STRING.after_last(delim) }
|
19
|
+
job.report('String#after_last_alt1'){|delim| STRING.after_last_alt1(delim) }
|
20
|
+
job.report('String#split'){|delim| STRING.split(delim, -1).drop(1).last }
|
21
|
+
job.compare!
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
|
7
|
+
def before_alt1(delim)
|
8
|
+
i = self.index(delim)
|
9
|
+
i ? self.first(i) : self.dup
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
STRING = 'abcnoonxyz'
|
17
|
+
|
18
|
+
Benchmark.inputs(STRING.chars << '_') do |job|
|
19
|
+
job.report('String#before'){|delim| STRING.before(delim) }
|
20
|
+
job.report('String#before_alt1'){|delim| STRING.before_alt1(delim) }
|
21
|
+
job.report('String#split'){|delim| STRING.split(delim, 2).first }
|
22
|
+
job.compare!
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
|
7
|
+
def before_last_alt1(delim)
|
8
|
+
i = self.rindex(delim)
|
9
|
+
i ? self.first(i) : self.dup
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
STRING = 'abcnoonxyz'
|
16
|
+
|
17
|
+
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
+
job.report('String#before_last'){|delim| STRING.before_last(delim) }
|
19
|
+
job.report('String#before_last_alt1'){|delim| STRING.before_last_alt1(delim) }
|
20
|
+
job.report('String#split'){|delim| STRING.split(delim, -1)[0..-2].join(delim) }
|
21
|
+
job.compare!
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
|
7
|
+
def between_alt1(open, close)
|
8
|
+
s = self.after(open)
|
9
|
+
s && s.before(close) # fails to return nil if `close` is missing
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
STRING = 'key="111-delim-222-delim-333"'
|
16
|
+
|
17
|
+
Benchmark.inputs(['"', '-delim-', '!']) do |job|
|
18
|
+
job.report('String#between'){|delim| STRING.between(delim, delim) }
|
19
|
+
job.report('String#between_alt1'){|delim| STRING.between_alt1(delim, delim) }
|
20
|
+
job.compare!
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
class String
|
6
|
+
def drop_alt1(n)
|
7
|
+
self[n.constrain(0, self.length), self.length]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
STRING = 'abcdef'
|
13
|
+
|
14
|
+
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
15
|
+
job.report('String#drop'){|n| STRING.drop(n) }
|
16
|
+
job.report('String#drop_alt1'){|n| STRING.drop_alt1(n) }
|
17
|
+
job.compare!
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require 'active_support/all'
|
3
|
+
class String
|
4
|
+
alias_method :old_first, :first
|
5
|
+
end
|
6
|
+
require_relative '../../lib/casual_support'
|
7
|
+
|
8
|
+
|
9
|
+
class String
|
10
|
+
def first_alt1(limit = 1)
|
11
|
+
limit <= 0 ? '' : self[0, limit]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
STRING = 'abcdef'
|
17
|
+
|
18
|
+
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
19
|
+
job.report('casual_support String#first'){|n| STRING.first(n) }
|
20
|
+
job.report('casual_support String#first_alt1'){|n| STRING.first_alt1(n) }
|
21
|
+
job.report('active_support String#first'){|n| STRING.old_first(n) }
|
22
|
+
job.compare!
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require 'active_support/all'
|
3
|
+
class String
|
4
|
+
alias_method :old_from, :from
|
5
|
+
end
|
6
|
+
require_relative '../../lib/casual_support'
|
7
|
+
|
8
|
+
|
9
|
+
STRING = 'abcdef'
|
10
|
+
|
11
|
+
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
+
job.report('casual_support String#from'){|n| STRING.from(n) }
|
13
|
+
job.report('active_support String#from'){|n| STRING.old_from(n) }
|
14
|
+
job.compare!
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require 'active_support/all'
|
3
|
+
class String
|
4
|
+
alias_method :old_last, :last
|
5
|
+
end
|
6
|
+
require_relative '../../lib/casual_support'
|
7
|
+
|
8
|
+
|
9
|
+
STRING = 'abcdef'
|
10
|
+
|
11
|
+
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
+
job.report('casual_support String#last'){|n| STRING.last(n) }
|
13
|
+
job.report('active_support String#last'){|n| STRING.old_last(n) }
|
14
|
+
job.compare!
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
PREFIX = 'abc'
|
6
|
+
PREFIX_REGEX = /^(?!abc)/
|
7
|
+
|
8
|
+
Benchmark.inputs(['xyz', 'abxyz', 'abcxyz', '']) do |job|
|
9
|
+
job.report('String#prefix'){|s| s.prefix(PREFIX) }
|
10
|
+
job.report('String#gsub'){|s| s.gsub(PREFIX_REGEX, PREFIX) }
|
11
|
+
job.compare!
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
SUFFIX = 'xyz'
|
6
|
+
SUFFIX_REGEX = /^(?!xyz)/
|
7
|
+
|
8
|
+
Benchmark.inputs(['abc', 'abcyz', 'abcxyz', '']) do |job|
|
9
|
+
job.report('String#suffix'){|s| s.suffix(SUFFIX) }
|
10
|
+
job.report('String#gsub'){|s| s.gsub(SUFFIX_REGEX, SUFFIX) }
|
11
|
+
job.compare!
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require 'active_support/all'
|
3
|
+
class String
|
4
|
+
alias_method :old_to, :to
|
5
|
+
end
|
6
|
+
require_relative '../../lib/casual_support'
|
7
|
+
|
8
|
+
|
9
|
+
STRING = 'abcdef'
|
10
|
+
|
11
|
+
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
+
job.report('casual_support String#to'){|n| STRING.to(n) }
|
13
|
+
job.report('active_support String#to'){|n| STRING.old_to(n) }
|
14
|
+
job.compare!
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'benchmark/inputs'
|
2
|
+
require_relative '../../lib/casual_support'
|
3
|
+
|
4
|
+
|
5
|
+
FORMAT = '%H:%M:%S'
|
6
|
+
|
7
|
+
Benchmark.inputs([Time.new(1999, 12, 31, 23, 59, 59)]) do |job|
|
8
|
+
job.report('Time#to_hms'){|t| t.to_hms }
|
9
|
+
job.report('Time#strftime'){|t| t.strftime('%H:%M:%S') }
|
10
|
+
job.report('Time#strftime(CONST)'){|t| t.strftime(FORMAT) }
|
11
|
+
job.compare!
|
12
|
+
end
|