fat_core 4.8.2 → 4.9.3
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/.gitignore +1 -0
- data/.rubocop.yml +19 -3
- data/.travis.yml +4 -4
- data/Gemfile +3 -0
- data/fat_core.gemspec +2 -3
- data/lib/fat_core/all.rb +2 -0
- data/lib/fat_core/array.rb +3 -1
- data/lib/fat_core/bigdecimal.rb +2 -0
- data/lib/fat_core/date.rb +92 -46
- data/lib/fat_core/enumerable.rb +6 -1
- data/lib/fat_core/hash.rb +3 -0
- data/lib/fat_core/kernel.rb +2 -0
- data/lib/fat_core/nil.rb +3 -1
- data/lib/fat_core/numeric.rb +13 -10
- data/lib/fat_core/patches.rb +2 -0
- data/lib/fat_core/range.rb +35 -23
- data/lib/fat_core/string.rb +9 -5
- data/lib/fat_core/symbol.rb +3 -1
- data/lib/fat_core/version.rb +4 -2
- data/lib/fat_core.rb +1 -0
- data/spec/lib/bigdecimal_spec.rb +1 -0
- data/spec/lib/date_spec.rb +17 -1
- data/spec/lib/enumerable_spec.rb +0 -1
- data/spec/lib/hash_spec.rb +0 -1
- data/spec/lib/kernel_spec.rb +0 -1
- data/spec/lib/nil_spec.rb +0 -1
- data/spec/lib/numeric_spec.rb +1 -0
- data/spec/lib/range_spec.rb +4 -2
- data/spec/lib/symbol_spec.rb +1 -0
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c7c63377c037bd7a5f79fa829b942f3e1d87985de7f5fe778a4e3211a31cde9
|
4
|
+
data.tar.gz: 612094bfa4667b800a0d677fee774d2d2a63e54bf2acee1db59c64d906f11ca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86f750f6fc63cacfdf659444a00db1ae07070a2133b347de5c4f943744678969f6c76a1c2ae3d92ce1dfb688ccffd922bd1f1fc3c72da70c18d3b19fa71f5408
|
7
|
+
data.tar.gz: 8d9aaad679c1d72869f89ea659cff2ddc321df7f2b9b6f1d167916fdc77fe0024ae6bae86d46dc336bcfafb426f3145dda6b24aab2fe95ed62e3d4bae6783235
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-shopify: rubocop.yml
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
TargetRubyVersion: 2.7
|
6
|
+
Exclude:
|
7
|
+
- 'test/tmp/**/*'
|
8
|
+
- 'vendor/bundle/**/*'
|
9
|
+
|
10
|
+
Style/MethodCallWithArgsParentheses:
|
11
|
+
Exclude:
|
12
|
+
- '**/Gemfile'
|
13
|
+
|
14
|
+
Style/ClassAndModuleChildren:
|
15
|
+
Exclude:
|
16
|
+
- 'test/**/*'
|
17
|
+
|
18
|
+
Style/StringLiterals:
|
19
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/fat_core.gemspec
CHANGED
@@ -4,8 +4,6 @@ lib = File.expand_path('../lib', __FILE__)
|
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
5
|
require 'fat_core/version'
|
6
6
|
|
7
|
-
gem 'damerau-levenshtein', git: 'https://github.com/ddoherty03/damerau-levenshtein'
|
8
|
-
|
9
7
|
Gem::Specification.new do |spec|
|
10
8
|
spec.name = 'fat_core'
|
11
9
|
spec.version = FatCore::VERSION
|
@@ -33,7 +31,8 @@ Gem::Specification.new do |spec|
|
|
33
31
|
spec.add_development_dependency 'pry-doc'
|
34
32
|
spec.add_development_dependency 'pry-byebug'
|
35
33
|
spec.add_development_dependency 'redcarpet'
|
34
|
+
spec.add_development_dependency 'solargraph'
|
36
35
|
|
37
|
-
spec.add_runtime_dependency 'activesupport'
|
36
|
+
spec.add_runtime_dependency 'activesupport', '~>6.0'
|
38
37
|
spec.add_runtime_dependency 'damerau-levenshtein'
|
39
38
|
end
|
data/lib/fat_core/all.rb
CHANGED
data/lib/fat_core/array.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module FatCore
|
2
4
|
module Array
|
3
5
|
# Return the index of the last element of this Array. This is just a
|
4
6
|
# convenience for an oft-needed Array attribute.
|
5
7
|
def last_i
|
6
|
-
|
8
|
+
size - 1
|
7
9
|
end
|
8
10
|
|
9
11
|
# Return a new Array that is the intersection of this Array with +other+,
|
data/lib/fat_core/bigdecimal.rb
CHANGED
data/lib/fat_core/date.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'date'
|
2
4
|
require 'active_support/core_ext/date'
|
3
5
|
require 'active_support/core_ext/time'
|
@@ -106,7 +108,7 @@ module FatCore
|
|
106
108
|
# form.
|
107
109
|
# @return [String]
|
108
110
|
def american
|
109
|
-
strftime
|
111
|
+
strftime('%-m/%-d/%Y')
|
110
112
|
end
|
111
113
|
|
112
114
|
# :category: Queries
|
@@ -117,7 +119,7 @@ module FatCore
|
|
117
119
|
# Self's calendar "half" by analogy to calendar quarters: 1 or 2, depending
|
118
120
|
# on whether the date falls in the first or second half of the calendar
|
119
121
|
# year.
|
120
|
-
# @return [
|
122
|
+
# @return [Integer]
|
121
123
|
def half
|
122
124
|
case month
|
123
125
|
when (1..6)
|
@@ -131,7 +133,7 @@ module FatCore
|
|
131
133
|
|
132
134
|
# Self's calendar quarter: 1, 2, 3, or 4, depending on which calendar quarter
|
133
135
|
# the date falls in.
|
134
|
-
# @return [
|
136
|
+
# @return [Integer]
|
135
137
|
def quarter
|
136
138
|
case month
|
137
139
|
when (1..3)
|
@@ -147,7 +149,7 @@ module FatCore
|
|
147
149
|
|
148
150
|
# Self's calendar bimonth: 1, 2, 3, 4, 5, or 6 depending on which calendar
|
149
151
|
# bimonth the date falls in.
|
150
|
-
# @return [
|
152
|
+
# @return [Integer]
|
151
153
|
def bimonth
|
152
154
|
case month
|
153
155
|
when (1..2)
|
@@ -857,7 +859,8 @@ module FatCore
|
|
857
859
|
::Date.parse('2012-12-24'),
|
858
860
|
# And Trump
|
859
861
|
::Date.parse('2018-12-24'),
|
860
|
-
::Date.parse('2019-12-24')
|
862
|
+
::Date.parse('2019-12-24'),
|
863
|
+
::Date.parse('2020-12-24'),
|
861
864
|
].freeze
|
862
865
|
|
863
866
|
# Presidential funeral since JFK
|
@@ -877,7 +880,7 @@ module FatCore
|
|
877
880
|
# GTF Funeral
|
878
881
|
::Date.parse('2007-01-02'),
|
879
882
|
# GHWBFuneral
|
880
|
-
::Date.parse('2018-12-05')
|
883
|
+
::Date.parse('2018-12-05'),
|
881
884
|
].freeze
|
882
885
|
|
883
886
|
# Return whether this date is a United States federal holiday.
|
@@ -1115,7 +1118,7 @@ module FatCore
|
|
1115
1118
|
def nyse_workday?
|
1116
1119
|
!nyse_holiday?
|
1117
1120
|
end
|
1118
|
-
|
1121
|
+
alias_method :trading_day?, :nyse_workday?
|
1119
1122
|
|
1120
1123
|
# Return the date that is n NYSE trading days after or before (if n < 0) this
|
1121
1124
|
# date.
|
@@ -1134,7 +1137,7 @@ module FatCore
|
|
1134
1137
|
end
|
1135
1138
|
d
|
1136
1139
|
end
|
1137
|
-
|
1140
|
+
alias_method :add_trading_days, :add_nyse_workdays
|
1138
1141
|
|
1139
1142
|
# Return the next NYSE trading day after this date. The date returned is always
|
1140
1143
|
# a date at least one day after this date, never this date.
|
@@ -1143,7 +1146,7 @@ module FatCore
|
|
1143
1146
|
def next_nyse_workday
|
1144
1147
|
add_nyse_workdays(1)
|
1145
1148
|
end
|
1146
|
-
|
1149
|
+
alias_method :next_trading_day, :next_nyse_workday
|
1147
1150
|
|
1148
1151
|
# Return the last NYSE trading day before this date. The date returned is always
|
1149
1152
|
# a date at least one day before this date, never this date.
|
@@ -1152,7 +1155,7 @@ module FatCore
|
|
1152
1155
|
def prior_nyse_workday
|
1153
1156
|
add_nyse_workdays(-1)
|
1154
1157
|
end
|
1155
|
-
|
1158
|
+
alias_method :prior_trading_day, :prior_nyse_workday
|
1156
1159
|
|
1157
1160
|
# Return this date if its a trading day, otherwise skip forward to the first
|
1158
1161
|
# later trading day.
|
@@ -1212,11 +1215,15 @@ module FatCore
|
|
1212
1215
|
# MLK's Birthday (Third Monday in Jan) since 1998
|
1213
1216
|
year >= 1998 && nth_wday_in_month?(3, 1, 1)
|
1214
1217
|
when 2
|
1215
|
-
#
|
1216
|
-
#
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1218
|
+
# Washington's Birthday was celebrated on February 22 until 1970. In
|
1219
|
+
# 1971 and later, it was moved to the third Monday in February. Note:
|
1220
|
+
# Lincoln's birthday is not an official holiday, but is sometimes
|
1221
|
+
# included with Washington's and called "Presidents' Day."
|
1222
|
+
if year <= 1970
|
1223
|
+
month == 2 && day == 22
|
1224
|
+
else
|
1225
|
+
nth_wday_in_month?(3, 1, 2)
|
1226
|
+
end
|
1220
1227
|
when 3, 4
|
1221
1228
|
# Good Friday
|
1222
1229
|
if !friday?
|
@@ -1419,7 +1426,7 @@ module FatCore
|
|
1419
1426
|
#
|
1420
1427
|
# @param spec [String, #to_s] the spec to be interpreted as a calendar period
|
1421
1428
|
#
|
1422
|
-
# @param spec_type [:from, :to] return the first (:from) or last (:to)
|
1429
|
+
# @param spec_type [Symbol, :from, :to] return the first (:from) or last (:to)
|
1423
1430
|
# date in the spec's period respectively
|
1424
1431
|
#
|
1425
1432
|
# @return [::Date] date that is the first (:from) or last (:to) in the period
|
@@ -1432,11 +1439,12 @@ module FatCore
|
|
1432
1439
|
|
1433
1440
|
today = ::Date.current
|
1434
1441
|
case spec.clean
|
1435
|
-
when %r{\A(
|
1442
|
+
when %r{\A(?<yr>\d\d\d\d)[-/](?<mo>\d\d?)[-/](?<dy>\d\d?)\z}
|
1436
1443
|
# A specified date
|
1437
|
-
::Date.new(
|
1438
|
-
|
1439
|
-
|
1444
|
+
::Date.new(Regexp.last_match[:yr].to_i, Regexp.last_match[:mo].to_i,
|
1445
|
+
Regexp.last_match[:dy].to_i)
|
1446
|
+
when /\AW(?<wk>\d\d?)\z/, /\A(?<wk>\d\d?)W\z/
|
1447
|
+
week_num = Regexp.last_match[:wk].to_i
|
1440
1448
|
if week_num < 1 || week_num > 53
|
1441
1449
|
raise ArgumentError, "invalid week number (1-53): '#{spec}'"
|
1442
1450
|
end
|
@@ -1446,9 +1454,9 @@ module FatCore
|
|
1446
1454
|
else
|
1447
1455
|
::Date.commercial(today.year, week_num).end_of_week
|
1448
1456
|
end
|
1449
|
-
when %r{\A(
|
1450
|
-
year =
|
1451
|
-
week_num =
|
1457
|
+
when %r{\A(?<yr>\d\d\d\d)[-/]W(?<wk>\d\d?)\z}, %r{\A(?<yr>\d\d\d\d)[-/](?<wk>\d\d?)W\z}
|
1458
|
+
year = Regexp.last_match[:yr].to_i
|
1459
|
+
week_num = Regexp.last_match[:wk].to_i
|
1452
1460
|
if week_num < 1 || week_num > 53
|
1453
1461
|
raise ArgumentError, "invalid week number (1-53): '#{spec}'"
|
1454
1462
|
end
|
@@ -1458,10 +1466,10 @@ module FatCore
|
|
1458
1466
|
else
|
1459
1467
|
::Date.commercial(year, week_num).end_of_week
|
1460
1468
|
end
|
1461
|
-
when %r{^(
|
1469
|
+
when %r{^(?<yr>\d\d\d\d)[-/](?<qt>\d)[Qq]$}, %r{^(?<yr>\d\d\d\d)[-/][Qq](?<qt>\d)$}
|
1462
1470
|
# Year-Quarter
|
1463
|
-
year =
|
1464
|
-
quarter =
|
1471
|
+
year = Regexp.last_match[:yr].to_i
|
1472
|
+
quarter = Regexp.last_match[:qt].to_i
|
1465
1473
|
unless [1, 2, 3, 4].include?(quarter)
|
1466
1474
|
raise ArgumentError, "invalid quarter number (1-4): '#{spec}'"
|
1467
1475
|
end
|
@@ -1472,10 +1480,10 @@ module FatCore
|
|
1472
1480
|
else
|
1473
1481
|
::Date.new(year, month, 1).end_of_quarter
|
1474
1482
|
end
|
1475
|
-
when /^([1234])[qQ]$/, /^[qQ]([1234])$/
|
1483
|
+
when /^(?<qt>[1234])[qQ]$/, /^[qQ](?<qt>[1234])$/
|
1476
1484
|
# Quarter only
|
1477
1485
|
this_year = today.year
|
1478
|
-
quarter =
|
1486
|
+
quarter = Regexp.last_match[:qt].to_i
|
1479
1487
|
unless [1, 2, 3, 4].include?(quarter)
|
1480
1488
|
raise ArgumentError, "invalid quarter number (1-4): '#{spec}'"
|
1481
1489
|
end
|
@@ -1486,10 +1494,10 @@ module FatCore
|
|
1486
1494
|
else
|
1487
1495
|
date.end_of_quarter
|
1488
1496
|
end
|
1489
|
-
when %r{^(
|
1497
|
+
when %r{^(?<yr>\d\d\d\d)[-/](?<hf>\d)[Hh]$}, %r{^(?<yr>\d\d\d\d)[-/][Hh](?<hf>\d)$}
|
1490
1498
|
# Year-Half
|
1491
|
-
year =
|
1492
|
-
half =
|
1499
|
+
year = Regexp.last_match[:yr].to_i
|
1500
|
+
half = Regexp.last_match[:hf].to_i
|
1493
1501
|
msg = "invalid half number: '#{spec}'"
|
1494
1502
|
raise ArgumentError, msg unless [1, 2].include?(half)
|
1495
1503
|
|
@@ -1499,10 +1507,10 @@ module FatCore
|
|
1499
1507
|
else
|
1500
1508
|
::Date.new(year, month, 1).end_of_half
|
1501
1509
|
end
|
1502
|
-
when /^([12])[hH]$/, /^[hH]([12])$/
|
1510
|
+
when /^(?<hf>[12])[hH]$/, /^[hH](?<hf>[12])$/
|
1503
1511
|
# Half only
|
1504
1512
|
this_year = today.year
|
1505
|
-
half =
|
1513
|
+
half = Regexp.last_match[:hf].to_i
|
1506
1514
|
msg = "invalid half number: '#{spec}'"
|
1507
1515
|
raise ArgumentError, msg unless [1, 2].include?(half)
|
1508
1516
|
|
@@ -1512,10 +1520,10 @@ module FatCore
|
|
1512
1520
|
else
|
1513
1521
|
date.end_of_half
|
1514
1522
|
end
|
1515
|
-
when %r{^(
|
1523
|
+
when %r{^(?<yr>\d\d\d\d)[-/](?<mo>\d\d?)*$}
|
1516
1524
|
# Year-Month only
|
1517
|
-
year =
|
1518
|
-
month =
|
1525
|
+
year = Regexp.last_match[:yr].to_i
|
1526
|
+
month = Regexp.last_match[:mo].to_i
|
1519
1527
|
unless [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].include?(month)
|
1520
1528
|
raise ArgumentError, "invalid month number (1-12): '#{spec}'"
|
1521
1529
|
end
|
@@ -1525,10 +1533,10 @@ module FatCore
|
|
1525
1533
|
else
|
1526
1534
|
::Date.new(year, month, 1).end_of_month
|
1527
1535
|
end
|
1528
|
-
when %r{^(
|
1536
|
+
when %r{^(?<mo>\d\d?)[-/](?<dy>\d\d?)*$}
|
1529
1537
|
# Month-Day only
|
1530
|
-
month =
|
1531
|
-
day =
|
1538
|
+
month = Regexp.last_match[:mo].to_i
|
1539
|
+
day = Regexp.last_match[:dy].to_i
|
1532
1540
|
unless [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].include?(month)
|
1533
1541
|
raise ArgumentError, "invalid month number (1-12): '#{spec}'"
|
1534
1542
|
end
|
@@ -1538,9 +1546,9 @@ module FatCore
|
|
1538
1546
|
else
|
1539
1547
|
::Date.new(today.year, month, day).end_of_month
|
1540
1548
|
end
|
1541
|
-
when /\A(
|
1549
|
+
when /\A(?<mo>\d\d?)\z/
|
1542
1550
|
# Month only
|
1543
|
-
month =
|
1551
|
+
month = Regexp.last_match[:mo].to_i
|
1544
1552
|
unless [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].include?(month)
|
1545
1553
|
raise ArgumentError, "invalid month number (1-12): '#{spec}'"
|
1546
1554
|
end
|
@@ -1550,12 +1558,13 @@ module FatCore
|
|
1550
1558
|
else
|
1551
1559
|
::Date.new(today.year, month, 1).end_of_month
|
1552
1560
|
end
|
1553
|
-
when /^(
|
1561
|
+
when /^(?<yr>\d\d\d\d)$/
|
1554
1562
|
# Year only
|
1563
|
+
year = Regexp.last_match[:yr].to_i
|
1555
1564
|
if spec_type == :from
|
1556
|
-
::Date.new(
|
1565
|
+
::Date.new(year, 1, 1)
|
1557
1566
|
else
|
1558
|
-
::Date.new(
|
1567
|
+
::Date.new(year, 12, 31)
|
1559
1568
|
end
|
1560
1569
|
when /^(to|this_?)?day/
|
1561
1570
|
today
|
@@ -1679,6 +1688,43 @@ module FatCore
|
|
1679
1688
|
end
|
1680
1689
|
end
|
1681
1690
|
|
1691
|
+
# Return the 1-indexed integer that corresponds to a month name.
|
1692
|
+
#
|
1693
|
+
# @param name [String] a name of a month
|
1694
|
+
#
|
1695
|
+
# @return [Integer] the integer integer that corresponds to a month
|
1696
|
+
# name, or nil of no month recognized.
|
1697
|
+
def mo_name_to_num(name)
|
1698
|
+
case name.clean
|
1699
|
+
when /\Ajan/i
|
1700
|
+
1
|
1701
|
+
when /\Afeb/i
|
1702
|
+
2
|
1703
|
+
when /\Amar/i
|
1704
|
+
3
|
1705
|
+
when /\Aapr/i
|
1706
|
+
4
|
1707
|
+
when /\Amay/i
|
1708
|
+
5
|
1709
|
+
when /\Ajun/i
|
1710
|
+
6
|
1711
|
+
when /\Ajul/i
|
1712
|
+
7
|
1713
|
+
when /\Aaug/i
|
1714
|
+
8
|
1715
|
+
when /\Asep/i
|
1716
|
+
9
|
1717
|
+
when /\Aoct/i
|
1718
|
+
10
|
1719
|
+
when /\Anov/i
|
1720
|
+
11
|
1721
|
+
when /\Adec/i
|
1722
|
+
12
|
1723
|
+
else
|
1724
|
+
nil
|
1725
|
+
end
|
1726
|
+
end
|
1727
|
+
|
1682
1728
|
# Return the nth weekday in the given month. If n is negative, count from
|
1683
1729
|
# last day of month.
|
1684
1730
|
#
|
@@ -1744,8 +1790,8 @@ module FatCore
|
|
1744
1790
|
# Ensure that date is of class Date based either on a string or Date
|
1745
1791
|
# object.
|
1746
1792
|
#
|
1747
|
-
# @param dat [String
|
1748
|
-
# @return [Date]
|
1793
|
+
# @param dat [String, Date, Time] the object to be converted to Date
|
1794
|
+
# @return [Date, DateTime]
|
1749
1795
|
def ensure_date(dat)
|
1750
1796
|
case dat
|
1751
1797
|
when String
|
data/lib/fat_core/enumerable.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerable
|
2
4
|
# Yield items in groups of n, for each group yield the group number, starting
|
3
5
|
# with zero and an Array of n items, or all remaining items if less than n.
|
@@ -8,7 +10,10 @@ module Enumerable
|
|
8
10
|
# end
|
9
11
|
def groups_of(num)
|
10
12
|
k = -1
|
11
|
-
group_by
|
13
|
+
group_by do
|
14
|
+
k += 1
|
15
|
+
k.div(num)
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
19
|
# Yield each item together with two booleans that indicate whether the item is
|
data/lib/fat_core/hash.rb
CHANGED
data/lib/fat_core/kernel.rb
CHANGED
data/lib/fat_core/nil.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module FatCore
|
2
4
|
module NilClass
|
3
5
|
# Allow nils to respond to #as_string like String and Symbol
|
@@ -6,7 +8,7 @@ module FatCore
|
|
6
8
|
def as_string
|
7
9
|
''
|
8
10
|
end
|
9
|
-
|
11
|
+
alias_method :entitle, :as_string
|
10
12
|
|
11
13
|
# Allow nils to respond to #tex_quote for use in TeX documents
|
12
14
|
#
|
data/lib/fat_core/numeric.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'fat_core/patches'
|
2
4
|
require 'active_support/core_ext/object/blank'
|
3
5
|
|
@@ -74,11 +76,11 @@ module FatCore
|
|
74
76
|
end
|
75
77
|
|
76
78
|
# Break the number into parts; underscores are possible in all components.
|
77
|
-
str =~ /\A([-+])?([\d_]*)((\.)?([\d_]*))?([eE][+-]?[\d_]+)?\z/
|
78
|
-
sig =
|
79
|
-
whole =
|
80
|
-
frac =
|
81
|
-
exp =
|
79
|
+
str =~ /\A(?<sg>[-+])?(?<wh>[\d_]*)((\.)?(?<fr>[\d_]*))?(?<ex>x[eE][+-]?[\d_]+)?\z/
|
80
|
+
sig = Regexp.last_match[:sg] || ''
|
81
|
+
whole = Regexp.last_match[:wh] ? Regexp.last_match[:wh].delete('_') : ''
|
82
|
+
frac = Regexp.last_match[:fr] || ''
|
83
|
+
exp = Regexp.last_match[:ex] || ''
|
82
84
|
|
83
85
|
# Pad out the fractional part with zeroes to the right
|
84
86
|
unless places.nil?
|
@@ -133,12 +135,13 @@ module FatCore
|
|
133
135
|
mins, secs = divmod(60)
|
134
136
|
hrs, mins = mins.divmod(60)
|
135
137
|
if frac.round(5) > 0.0
|
136
|
-
'%02<hrs>d:%02<mins>d:%02<secs>d.%<frac>d'
|
137
|
-
|
138
|
-
|
138
|
+
format('%02<hrs>d:%02<mins>d:%02<secs>d.%<frac>d',
|
139
|
+
{ hrs: hrs,
|
140
|
+
mins: mins, secs: secs,
|
141
|
+
frac: frac.round(5) * 100 })
|
139
142
|
else
|
140
|
-
'%02<hrs>d:%02<mins>d:%02<secs>d'
|
141
|
-
|
143
|
+
format('%02<hrs>d:%02<mins>d:%02<secs>d',
|
144
|
+
{ hrs: hrs, mins: mins, secs: secs })
|
142
145
|
end
|
143
146
|
end
|
144
147
|
|
data/lib/fat_core/patches.rb
CHANGED
data/lib/fat_core/range.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# FatCore extends the Range class with methods that
|
2
4
|
#
|
3
5
|
# 1. provide some set operations operations on Ranges, union, intersection, and
|
@@ -130,7 +132,7 @@ module FatCore
|
|
130
132
|
|
131
133
|
([min, other.min].max..[max, other.max].min)
|
132
134
|
end
|
133
|
-
|
135
|
+
alias_method :&, :intersection
|
134
136
|
|
135
137
|
# Return a Range that represents the union between this range and the
|
136
138
|
# `other` range. If there is no overlap and self is not contiguous with
|
@@ -148,36 +150,44 @@ module FatCore
|
|
148
150
|
|
149
151
|
([min, other.min].min..[max, other.max].max)
|
150
152
|
end
|
151
|
-
|
153
|
+
alias_method :+, :union
|
152
154
|
|
153
155
|
# The difference method, -, removes the overlapping part of the other
|
154
156
|
# argument from self. Because in the case where self is a superset of the
|
155
157
|
# other range, this will result in the difference being two non-contiguous
|
156
158
|
# ranges, this returns an array of ranges. If there is no overlap or if
|
157
159
|
# self is a subset of the other range, return an array of self
|
160
|
+
#
|
161
|
+
# @param other [Range] the Range whose overlap is removed from self
|
162
|
+
# @return [Array<Range>] the Ranges representing self with other removed
|
158
163
|
def difference(other)
|
159
164
|
unless max.respond_to?(:succ) && min.respond_to?(:pred) &&
|
160
165
|
other.max.respond_to?(:succ) && other.min.respond_to?(:pred)
|
161
166
|
raise 'Range difference requires objects have pred and succ methods'
|
162
167
|
end
|
163
168
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
169
|
+
# Remove the intersection of self and other from self. The intersection
|
170
|
+
# is either (a) empty, so return self, (b) coincides with self, so
|
171
|
+
# return nothing,
|
172
|
+
isec = self & other
|
173
|
+
return [self] if isec.nil?
|
174
|
+
return [] if isec == self
|
175
|
+
|
176
|
+
# (c) touches self on the right, (d) touches self on the left, or (e)
|
177
|
+
# touches on neither the left or right, in which case the difference is
|
178
|
+
# two ranges.
|
179
|
+
if isec.max == max && isec.min > min
|
180
|
+
# Return the part to the left of isec
|
181
|
+
[(min..isec.min.pred)]
|
182
|
+
elsif isec.min == min && isec.max < max
|
183
|
+
# Return the part to the right of isec
|
184
|
+
[(isec.max.succ..max)]
|
176
185
|
else
|
177
|
-
|
186
|
+
# Return the parts to the left and right of isec
|
187
|
+
[(min..isec.min.pred), (isec.max.succ..max)]
|
178
188
|
end
|
179
189
|
end
|
180
|
-
|
190
|
+
alias_method :-, :difference
|
181
191
|
|
182
192
|
# Allow erb or erubis documents to directly interpolate a Range.
|
183
193
|
#
|
@@ -280,13 +290,14 @@ module FatCore
|
|
280
290
|
min >= other.min && max <= other.max
|
281
291
|
end
|
282
292
|
|
283
|
-
# Return whether self is contained within `other` range,
|
284
|
-
#
|
293
|
+
# Return whether self is contained within `other` range, with at most one
|
294
|
+
# boundary touching.
|
285
295
|
#
|
286
296
|
# @param other [Range] the containing range
|
287
297
|
# @return [Boolean] is self wholly within other
|
288
298
|
def proper_subset_of?(other)
|
289
|
-
|
299
|
+
subset_of?(other) &&
|
300
|
+
(min > other.min || max < other.max)
|
290
301
|
end
|
291
302
|
|
292
303
|
# Return whether self contains `other` range, even if their
|
@@ -298,13 +309,14 @@ module FatCore
|
|
298
309
|
min <= other.min && max >= other.max
|
299
310
|
end
|
300
311
|
|
301
|
-
# Return whether self contains `other` range,
|
302
|
-
#
|
312
|
+
# Return whether self contains `other` range, with at most one
|
313
|
+
# boundary touching.
|
303
314
|
#
|
304
315
|
# @param other [Range] the contained range
|
305
316
|
# @return [Boolean] does self wholly contain other
|
306
317
|
def proper_superset_of?(other)
|
307
|
-
|
318
|
+
superset_of?(other) &&
|
319
|
+
(min < other.min || max > other.max)
|
308
320
|
end
|
309
321
|
|
310
322
|
# Return whether self overlaps with other Range.
|
@@ -368,7 +380,7 @@ module FatCore
|
|
368
380
|
# (4..8) <=> (4..8) #=> 0
|
369
381
|
#
|
370
382
|
# @param other [Range] range to compare self with
|
371
|
-
# @return [-1, 0, 1] if self is less, equal, or greater than other
|
383
|
+
# @return [Integer, -1, 0, 1] if self is less, equal, or greater than other
|
372
384
|
def <=>(other)
|
373
385
|
[min, max] <=> other.minmax
|
374
386
|
end
|
data/lib/fat_core/string.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'bigdecimal'
|
2
4
|
require 'fat_core/patches'
|
3
5
|
require 'damerau-levenshtein'
|
@@ -49,14 +51,14 @@ module FatCore
|
|
49
51
|
#
|
50
52
|
# @return [String] self wrapped
|
51
53
|
def wrap(width = 70, hang = 0)
|
52
|
-
result =
|
54
|
+
result = ::String.new
|
53
55
|
first_line = true
|
54
56
|
first_word_on_line = true
|
55
57
|
line_width_so_far = 0
|
56
58
|
words = split(' ')
|
57
59
|
words.each do |w|
|
58
|
-
w = ' ' * hang + w if !first_line && first_word_on_line
|
59
|
-
w = ' ' + w unless first_word_on_line
|
60
|
+
w = ::String.new(' ') * hang + w if !first_line && first_word_on_line
|
61
|
+
w = ::String.new(' ') + w unless first_word_on_line
|
60
62
|
result << w
|
61
63
|
first_word_on_line = false
|
62
64
|
line_width_so_far += 1 + w.length
|
@@ -104,8 +106,10 @@ module FatCore
|
|
104
106
|
#
|
105
107
|
# @return [Date] the translated Date
|
106
108
|
def as_date
|
107
|
-
if self =~ %r{(
|
108
|
-
::Date.new(
|
109
|
+
if self =~ %r{(?<yr>\d\d\d\d)[-/]?(?<mo>\d\d?)[-/]?(?<dy>\d\d?)}
|
110
|
+
::Date.new(Regexp.last_match[:yr].to_i,
|
111
|
+
Regexp.last_match[:mo].to_i,
|
112
|
+
Regexp.last_match[:dy].to_i)
|
109
113
|
end
|
110
114
|
end
|
111
115
|
|
data/lib/fat_core/symbol.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'fat_core/string'
|
2
4
|
|
3
5
|
module FatCore
|
@@ -14,7 +16,7 @@ module FatCore
|
|
14
16
|
def as_string
|
15
17
|
to_s.tr('_', ' ').split(' ').join(' ').entitle
|
16
18
|
end
|
17
|
-
|
19
|
+
alias_method :entitle, :as_string
|
18
20
|
|
19
21
|
# Return self. This (together with String#as_sym) allows `#as_sym` to be
|
20
22
|
# applied to a string or Symbol and get back a Symbol with out testing for
|
data/lib/fat_core/version.rb
CHANGED
data/lib/fat_core.rb
CHANGED
data/spec/lib/bigdecimal_spec.rb
CHANGED
data/spec/lib/date_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
#require 'spec_helper'
|
3
2
|
|
3
|
+
require 'spec_helper'
|
4
4
|
require 'fat_core/date'
|
5
5
|
|
6
6
|
describe Date do
|
@@ -425,6 +425,22 @@ describe Date do
|
|
425
425
|
expect(Date.parse_spec('forever', :to)).to eq Date::EOT
|
426
426
|
expect(Date.parse_spec('never')).to be_nil
|
427
427
|
end
|
428
|
+
|
429
|
+
it 'should be able to convert a month name into its sequential number' do
|
430
|
+
expect(Date.mo_name_to_num(' January')).to eq 1
|
431
|
+
expect(Date.mo_name_to_num(' feb ')).to eq 2
|
432
|
+
expect(Date.mo_name_to_num(' mAr ')).to eq 3
|
433
|
+
expect(Date.mo_name_to_num(' Aprol ')).to eq 4
|
434
|
+
expect(Date.mo_name_to_num("\t \tmaybe")).to eq 5
|
435
|
+
expect(Date.mo_name_to_num("\t \tjunta\t \t")).to eq 6
|
436
|
+
expect(Date.mo_name_to_num("\t \tjulia\t \t")).to eq 7
|
437
|
+
expect(Date.mo_name_to_num("\t \tAugustus\t \t")).to eq 8
|
438
|
+
expect(Date.mo_name_to_num("September")).to eq 9
|
439
|
+
expect(Date.mo_name_to_num("octagon")).to eq 10
|
440
|
+
expect(Date.mo_name_to_num(" novena this month")).to eq 11
|
441
|
+
expect(Date.mo_name_to_num("decimal")).to eq 12
|
442
|
+
expect(Date.mo_name_to_num(" dewey decimal")).to be nil
|
443
|
+
end
|
428
444
|
end
|
429
445
|
end
|
430
446
|
|
data/spec/lib/enumerable_spec.rb
CHANGED
data/spec/lib/hash_spec.rb
CHANGED
data/spec/lib/kernel_spec.rb
CHANGED
data/spec/lib/nil_spec.rb
CHANGED
data/spec/lib/numeric_spec.rb
CHANGED
data/spec/lib/range_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'spec_helper'
|
1
2
|
require 'fat_core/range'
|
2
3
|
|
3
4
|
describe Range do
|
@@ -12,6 +13,7 @@ describe Range do
|
|
12
13
|
|
13
14
|
it 'should know if it is a proper subset of another range' do
|
14
15
|
expect((4..8)).to be_proper_subset_of((2..9))
|
16
|
+
expect((4..8)).to be_proper_subset_of((4..9))
|
15
17
|
expect((4..8)).not_to be_proper_subset_of((4..8))
|
16
18
|
expect((4..8)).not_to be_proper_subset_of((2..7))
|
17
19
|
expect((4..8)).not_to be_proper_subset_of((5..8))
|
@@ -34,8 +36,8 @@ describe Range do
|
|
34
36
|
|
35
37
|
it 'should know if it is a proper superset of another range' do
|
36
38
|
expect((4..8)).to be_proper_superset_of((5..7))
|
37
|
-
expect((4..8)).
|
38
|
-
expect((4..8)).
|
39
|
+
expect((4..8)).to be_proper_superset_of((6..8))
|
40
|
+
expect((4..8)).to be_proper_superset_of((4..7))
|
39
41
|
expect((4..8)).not_to be_proper_superset_of((4..8))
|
40
42
|
expect((4..8)).not_to be_proper_superset_of((2..9))
|
41
43
|
expect((4..8)).not_to be_proper_superset_of((2..8))
|
data/spec/lib/symbol_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel E. Doherty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -137,19 +137,33 @@ dependencies:
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
140
|
+
name: solargraph
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: '0'
|
146
|
-
type: :
|
146
|
+
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - ">="
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: activesupport
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '6.0'
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '6.0'
|
153
167
|
- !ruby/object:Gem::Dependency
|
154
168
|
name: damerau-levenshtein
|
155
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -233,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
233
247
|
- !ruby/object:Gem::Version
|
234
248
|
version: '0'
|
235
249
|
requirements: []
|
236
|
-
rubygems_version: 3.
|
250
|
+
rubygems_version: 3.3.3
|
237
251
|
signing_key:
|
238
252
|
specification_version: 4
|
239
253
|
summary: fat_core provides some useful core extensions
|