natural-date 1.1 → 1.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.
- checksums.yaml +4 -4
- data/README.md +72 -12
- data/lib/natural-date/natural_date_expression.rb +29 -12
- data/lib/natural-date/natural_date_expression_factory.rb +1 -1
- data/lib/natural-date/translator/en.yml +7 -7
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 542d489e665790093de2be56251385422e79d4e2
|
4
|
+
data.tar.gz: 7ee809b6b7bfad9535bdca731effad0bef68108e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55711080ed2742f5ed38ecacc94db1a9ef9a45035d0714151146f46dd6e88ca2214d3f77fd40025e647713641e95ced01939ee487223071bb72608f48ca4d06c
|
7
|
+
data.tar.gz: f83fbd5aae5cd47e64a3b50ecdaf599de0dc6ee7143b10e33a894d51784351739203579f9a97cfe72f3357cd21aa299358999dc32c6ca486d3546543e29847f4
|
data/README.md
CHANGED
@@ -24,33 +24,93 @@ Or install it yourself as:
|
|
24
24
|
```ruby
|
25
25
|
require 'natural-date'
|
26
26
|
|
27
|
-
# First you need to create a NaturalDateExpressionFactory with a specific locale.
|
27
|
+
# First you need to create a NaturalDateExpressionFactory with a specific locale.
|
28
|
+
# Only pt-BR and en is supported by now.
|
28
29
|
factory = NaturalDateExpressionFactory.new(:en)
|
29
30
|
|
30
|
-
#
|
31
|
+
# Then create a natural date expression passing a string expression and a reference date.
|
31
32
|
# In the example below as we set the reference date to 9th of Dezember, 2016 the natural_date is
|
32
|
-
# going to match only '2017-8-1'
|
33
|
+
# going to match only '2017-8-1'.
|
34
|
+
# When no reference date is provided the Date.today is going to be used.
|
33
35
|
natural_date = factory.create('1th of august', Date.parse('2016-12-9'))
|
34
36
|
|
35
|
-
#
|
36
|
-
|
37
|
-
|
37
|
+
# NaturalDateExpression does not generate Dates (due it can represent a
|
38
|
+
# recurrent date expression like 'every monday') instead you need to
|
39
|
+
# match a particular date to the expression using the *=~* or *match?*.
|
40
|
+
puts natural_date =~ Date.new(2016, 8, 1) # => false
|
41
|
+
puts natural_date =~ Date.new(2017, 8, 1) # => true
|
42
|
+
|
43
|
+
# or using match?
|
44
|
+
natural_date.match?(Date.new(2016, 8, 1))
|
45
|
+
# => false
|
46
|
+
natural_date.match?(Date.new(2017, 8, 1))
|
47
|
+
# => true
|
48
|
+
|
49
|
+
# You can also get some matched dates in a given interval
|
50
|
+
natural_date.fetch_dates(Date.today..(Date.today + 5))
|
51
|
+
# => array with dates matched inside the interval
|
52
|
+
|
53
|
+
# If you don't pass any interval a default interval of Date.today..Date.today + 365 will be given
|
54
|
+
natural_date.fetch_dates
|
55
|
+
# => array with dates matched inside the interval
|
38
56
|
```
|
39
57
|
|
40
|
-
|
58
|
+
## Types of expressions
|
41
59
|
|
42
|
-
|
60
|
+
You can create lots of different types of expressions, using days months, years and week days, the expression can
|
61
|
+
refer both single and recurring dates.
|
43
62
|
|
44
|
-
|
63
|
+
- **Days**: Can be written as numbers `1..31` or ordinals like `['1st'..'31st']`.
|
64
|
+
- **Months**: Can be written using the whole name `['January'..'December']` or using the first 3 letters `['Jan'..'Dec']`, also they are case insensitive `'JAN' == 'Jan' == 'jAn'` also you can use numbers if you use the 'of' world before `'1 of 12' == '1st December'`.
|
65
|
+
- **Years**: You can write the year as usual 4 digits number like `2016` or you can ignore the year, if you ignore it the year is going to be guessed based on the expression and the reference date
|
66
|
+
- **Week Days**: As months you can write both whole name and abbreviation (first 3 letters) 'monday' == 'mon'.
|
45
67
|
|
46
|
-
|
68
|
+
```ruby
|
69
|
+
factory = NaturalDateExpressionFactory.new(:en)
|
70
|
+
|
71
|
+
# Day and month
|
72
|
+
factory.create('1st of November, 2016')
|
73
|
+
# => '1st November 2016'
|
74
|
+
factory.create('1 sep 2016')
|
75
|
+
# => '1st September 2016'
|
76
|
+
factory.create('1 of 1 2016')
|
77
|
+
# => '1st September 2016'
|
78
|
+
factory.create('1 of July', Date.new(2016, 1, 1)
|
79
|
+
# => '1st July 2016' due the reference date is before '1st July, 2016'
|
80
|
+
factory.create('1st of Apr', Date.new(2016, 9, 9)
|
81
|
+
# => '1st April 2017' due the reference date is after '1st April, 2016'
|
82
|
+
|
83
|
+
# Multiple days in a month
|
84
|
+
factory.create('1 8 and 15 of July, 2016')
|
85
|
+
# => ['1st July 2016', '8th July 2016', '15th July 2016']
|
86
|
+
factory.create('1st 8 and 15th of September, 2016')
|
87
|
+
# => ['1st July 2016', '8th July 2016', '15th July 2016'], not the best consistency but still ok
|
88
|
+
|
89
|
+
# Single day in multiple months
|
90
|
+
factory.create('1 of July and August 2016')
|
91
|
+
# => ['1st July 2016', '1st August 2016']
|
92
|
+
|
93
|
+
# Multiple days and multiple months
|
94
|
+
factory.create('1, 7 of May and March 2016')
|
95
|
+
# => ['1st May 2016', '7st May 2016', '1st March, 2016', '7st March 2016']
|
96
|
+
|
97
|
+
# You can also set ranges in both days and months using *till* or *until*
|
98
|
+
factory.create('1 till 7 of Jan 2016')
|
99
|
+
# => ['1st January 2016'..'7st January 2016']
|
100
|
+
factory.create('1 of Jan until April 2016')
|
101
|
+
# => ['1st January 2016', '1st January 2016', '1st January 2016', '1st January 2016']
|
102
|
+
|
103
|
+
# Also you can set week days like 'monday', 'sunday', 'sat'
|
104
|
+
factory.create('every monday of January')
|
105
|
+
# => every monday of January any year
|
106
|
+
factory.create('every monday and friday')
|
107
|
+
# => every monday and friday of any month and any year.
|
108
|
+
```
|
47
109
|
|
48
110
|
## Contributing
|
49
111
|
|
50
112
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/natural-date. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
51
113
|
|
52
|
-
|
53
114
|
## License
|
54
115
|
|
55
116
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
56
|
-
|
@@ -1,5 +1,20 @@
|
|
1
1
|
class NaturalDateExpression
|
2
|
-
VERSION = "1.
|
2
|
+
VERSION = "1.2"
|
3
|
+
|
4
|
+
class DateMatch
|
5
|
+
attr_reader :first_matched_expression, :tested_date, :reference_date
|
6
|
+
|
7
|
+
def initialize match, first_matched_expression, tested_date, reference_date
|
8
|
+
@match = match
|
9
|
+
@first_matched_expression = first_matched_expression
|
10
|
+
@tested_date = tested_date
|
11
|
+
@reference_date = reference_date
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches?
|
15
|
+
@match
|
16
|
+
end
|
17
|
+
end
|
3
18
|
|
4
19
|
def initialize data, reference_date, expression_string
|
5
20
|
@data = data
|
@@ -7,6 +22,14 @@ class NaturalDateExpression
|
|
7
22
|
@expression_string = expression_string
|
8
23
|
end
|
9
24
|
|
25
|
+
def match?
|
26
|
+
match(date).matches?
|
27
|
+
end
|
28
|
+
|
29
|
+
def =~ date
|
30
|
+
match(date).matches?
|
31
|
+
end
|
32
|
+
|
10
33
|
def match date
|
11
34
|
matches = @data.map do |expression_map|
|
12
35
|
match_week?(date, expression_map) &&
|
@@ -16,14 +39,10 @@ class NaturalDateExpression
|
|
16
39
|
match_year?(date, expression_map)
|
17
40
|
end
|
18
41
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
{ match: matches.any?,
|
26
|
-
first_matched_expression: first_matched_expression }
|
42
|
+
DateMatch.new(matches.any?,
|
43
|
+
(matches.any?? @data[matches.each_with_index.find { |exp, index| exp }.last] : nil),
|
44
|
+
@date,
|
45
|
+
@reference_date)
|
27
46
|
end
|
28
47
|
|
29
48
|
def recurrent?
|
@@ -35,9 +54,7 @@ class NaturalDateExpression
|
|
35
54
|
end
|
36
55
|
|
37
56
|
def fetch_dates dates_range = nil
|
38
|
-
(dates_range || (Date.today..(Date.today + 365))).to_a.select
|
39
|
-
match(date).fetch(:match)
|
40
|
-
end
|
57
|
+
(dates_range || (Date.today..(Date.today + 365))).to_a.select { |date| self =~ date }
|
41
58
|
end
|
42
59
|
|
43
60
|
private
|
@@ -6,7 +6,7 @@ class NaturalDateExpressionFactory
|
|
6
6
|
STEPS = {}
|
7
7
|
EXPRESSION_SPLITTER_STEP = {}
|
8
8
|
|
9
|
-
def create expression_string, reference_date
|
9
|
+
def create expression_string, reference_date = Date.today
|
10
10
|
NaturalDateExpression.new(create_data_expression(expression_string, reference_date), reference_date, expression_string)
|
11
11
|
end
|
12
12
|
|
@@ -17,9 +17,9 @@ keys:
|
|
17
17
|
fri_5: 'friday fri'
|
18
18
|
sat_6: 'saturday sat'
|
19
19
|
day:
|
20
|
-
day_1: '1 1th'
|
21
|
-
day_2: '2 2th'
|
22
|
-
day_3: '3 3th'
|
20
|
+
day_1: '1 1th 1st'
|
21
|
+
day_2: '2 2th 2nd'
|
22
|
+
day_3: '3 3th 3rd'
|
23
23
|
day_4: '4 4th'
|
24
24
|
day_5: '5 5th'
|
25
25
|
day_6: '6 6th'
|
@@ -37,9 +37,9 @@ keys:
|
|
37
37
|
day_18: '18 18th'
|
38
38
|
day_19: '19 19th'
|
39
39
|
day_20: '20 20th'
|
40
|
-
day_21: '21 21th'
|
41
|
-
day_22: '22 22th'
|
42
|
-
day_23: '23 23th'
|
40
|
+
day_21: '21 21th 21st'
|
41
|
+
day_22: '22 22th 22nd'
|
42
|
+
day_23: '23 23th 23rd'
|
43
43
|
day_24: '24 24th'
|
44
44
|
day_25: '25 25th'
|
45
45
|
day_26: '26 26th'
|
@@ -47,7 +47,7 @@ keys:
|
|
47
47
|
day_28: '28 28th'
|
48
48
|
day_29: '29 29th'
|
49
49
|
day_30: '30 30th'
|
50
|
-
day_31: '31 31th'
|
50
|
+
day_31: '31 31th 31st'
|
51
51
|
month:
|
52
52
|
jan_1: 'jan janeiro'
|
53
53
|
feb_2: 'feb february'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: natural-date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew S Aguiar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -110,8 +110,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.4.8
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Converts natural language in date expressions
|
117
117
|
test_files: []
|
118
|
+
has_rdoc:
|