stamp 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -10
- data/cucumber.yml +1 -0
- data/features/stamp.feature +26 -21
- data/lib/stamp.rb +26 -8
- data/lib/stamp/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -19,26 +19,33 @@ date = Date.new(2011, 6, 9)
|
|
19
19
|
date.stamp("March 1, 1999") # "June 9, 2011"
|
20
20
|
date.stamp("Jan 1, 1999") # "Jun 9, 2011"
|
21
21
|
date.stamp("Jan 01") # "Jun 09"
|
22
|
-
date.stamp("Sunday, May 1, 2000") # "
|
23
|
-
date.stamp("Sun Aug 5") # "
|
24
|
-
date.stamp("
|
25
|
-
date.stamp("DOB:
|
22
|
+
date.stamp("Sunday, May 1, 2000") # "Thursday, June 9, 2011"
|
23
|
+
date.stamp("Sun Aug 5") # "Thu Jun 9"
|
24
|
+
date.stamp("12/31/99") # "06/09/11"
|
25
|
+
date.stamp("DOB: 12/31/2000") # "DOB: 06/09/2011"
|
26
26
|
```
|
27
27
|
|
28
28
|
### Features
|
29
29
|
|
30
30
|
* Abbreviated and full names of months and weekdays are recognized.
|
31
31
|
* Days with or without a leading zero work instinctively.
|
32
|
-
* You can use whatever month, weekday, day, or year value makes sense in your
|
33
|
-
examples.
|
34
32
|
* Include any extraneous text you'd like, e.g. "DOB:".
|
35
33
|
|
34
|
+
### Disambiguation by value
|
35
|
+
|
36
|
+
You can use any month, weekday, day, or year value that makes sense in your
|
37
|
+
examples, and stamp can often infer your intent based on context, but there
|
38
|
+
may be times that you need to use unambiguous values to make your intent more
|
39
|
+
explicit.
|
40
|
+
|
41
|
+
For example, "01/09" could refer to January 9, September 1, or
|
42
|
+
January 2009. More explicit examples include "12/31", "31/12", and "12/99".
|
43
|
+
|
44
|
+
Using unambiguous values will also help people who read the code in the
|
45
|
+
future understand your intent.
|
46
|
+
|
36
47
|
### Limitations
|
37
48
|
|
38
|
-
* "01/09/99" is assumed to be January 9, not September 1. Patches to make this
|
39
|
-
configurable are welcome. Even better, make it smart enough to disambiguate
|
40
|
-
given an example like 31/01/99, where 31 is obviously not a month.
|
41
|
-
* "01-Jan-1999" doesn't work yet (see @wip cucumber scenario). Patches welcome!
|
42
49
|
* Support for time formatting by example is coming soon. Patches welcome!
|
43
50
|
|
44
51
|
Did I mention? Patches welcome!
|
@@ -60,6 +67,8 @@ More coming soon, including time formats by example.
|
|
60
67
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
61
68
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
62
69
|
* Fork the project
|
70
|
+
* Run `bundle install`
|
71
|
+
* Run `rake` to execute the cucumber specs and make sure they all pass
|
63
72
|
* Start a feature/bugfix branch
|
64
73
|
* Commit and push until you are happy with your contribution
|
65
74
|
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
data/cucumber.yml
CHANGED
data/features/stamp.feature
CHANGED
@@ -4,39 +4,44 @@ Feature: Stamping a date
|
|
4
4
|
formats a date given a human-readable example.
|
5
5
|
|
6
6
|
Scenario Outline: Formatting dates by example
|
7
|
-
Given the date
|
7
|
+
Given the date October 9, 2011
|
8
8
|
When I stamp the example "<example>"
|
9
9
|
Then I produce "<output>"
|
10
10
|
And I like turtles
|
11
11
|
|
12
12
|
Examples:
|
13
|
-
| example
|
14
|
-
| January
|
15
|
-
| Jan
|
16
|
-
| Jan 1
|
17
|
-
| Jan 01
|
18
|
-
| Jan 10
|
19
|
-
| Jan 1, 1999
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
| 01/1999
|
25
|
-
| 01/01
|
26
|
-
| 01/31
|
27
|
-
| 01/
|
28
|
-
| 01/01/
|
29
|
-
|
|
13
|
+
| example | output |
|
14
|
+
| January | October |
|
15
|
+
| Jan | Oct |
|
16
|
+
| Jan 1 | Oct 9 |
|
17
|
+
| Jan 01 | Oct 09 |
|
18
|
+
| Jan 10 | Oct 09 |
|
19
|
+
| Jan 1, 1999 | Oct 9, 2011 |
|
20
|
+
| Monday | Sunday |
|
21
|
+
| Mon | Sun |
|
22
|
+
| Tue, Jan 1 | Sun, Oct 9 |
|
23
|
+
| Tuesday, January 1, 1999 | Sunday, October 9, 2011 |
|
24
|
+
| 01/1999 | 10/2011 |
|
25
|
+
| 01/01 | 10/09 |
|
26
|
+
| 01/31 | 10/09 |
|
27
|
+
| 01/99 | 10/11 |
|
28
|
+
| 01/01/1999 | 10/09/2011 |
|
29
|
+
| 12/31/99 | 10/09/11 |
|
30
|
+
| 31/12 | 09/10 |
|
31
|
+
| 31/12/99 | 09/10/11 |
|
32
|
+
| 31-Jan-1999 | 09-Oct-2011 |
|
33
|
+
| 1999-12-31 | 2011-10-09 |
|
34
|
+
| DOB: 12-31-1999 | DOB: 10-09-2011 |
|
30
35
|
|
31
36
|
@wip
|
32
37
|
Scenario Outline: Examples that aren't supported quite yet
|
33
|
-
Given the date
|
38
|
+
Given the date October 9, 2011
|
34
39
|
When I stamp the example "<example>"
|
35
40
|
Then I produce "<output>"
|
36
41
|
|
37
42
|
Examples:
|
38
|
-
| example
|
39
|
-
|
|
43
|
+
| example | output |
|
44
|
+
| | |
|
40
45
|
|
41
46
|
Scenario: strftime directives just get passed through
|
42
47
|
Given the date December 21, 2012
|
data/lib/stamp.rb
CHANGED
@@ -23,8 +23,15 @@ module Stamp
|
|
23
23
|
TWO_DIGIT_REGEXP = /\d{2}/
|
24
24
|
FOUR_DIGIT_REGEXP = /\d{4}/
|
25
25
|
|
26
|
-
DATE_DELIMITER_REGEXP = /(\/|\-)/ # forward slash or dash
|
26
|
+
# DATE_DELIMITER_REGEXP = /(\/|\-)/ # forward slash or dash
|
27
27
|
|
28
|
+
# OBVIOUS_MINUTES = 32..59
|
29
|
+
# OBVIOUS_HOURS
|
30
|
+
# OBVIOUS_SECONDS
|
31
|
+
|
32
|
+
OBVIOUS_YEARS = 60..99
|
33
|
+
OBVIOUS_MONTHS = 12
|
34
|
+
OBVIOUS_DAYS = 28..31
|
28
35
|
|
29
36
|
def stamp(example)
|
30
37
|
strftime(strftime_directives(example))
|
@@ -40,7 +47,6 @@ module Stamp
|
|
40
47
|
directive = strftime_directive(term, previous_directive)
|
41
48
|
directives << (directive || term)
|
42
49
|
|
43
|
-
previous_term = term
|
44
50
|
previous_directive = directive unless directive.nil?
|
45
51
|
end
|
46
52
|
|
@@ -66,13 +72,25 @@ module Stamp
|
|
66
72
|
'%Y'
|
67
73
|
|
68
74
|
when TWO_DIGIT_REGEXP
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
75
|
+
# try to discern obvious intent based on the example value
|
76
|
+
case term.to_i
|
77
|
+
when OBVIOUS_YEARS
|
78
|
+
'%y'
|
79
|
+
when OBVIOUS_MONTHS
|
80
|
+
'%m'
|
81
|
+
when OBVIOUS_DAYS
|
82
|
+
'%d'
|
74
83
|
else
|
75
|
-
'
|
84
|
+
# the intent isn't obvious based on the example value, so try to
|
85
|
+
# disambiguate based on context
|
86
|
+
case previous_directive
|
87
|
+
when '%m', '%b', '%B' # a month
|
88
|
+
'%d' # day with leading zero
|
89
|
+
when '%d', '%e' # a day
|
90
|
+
'%y' # two-digit year
|
91
|
+
else
|
92
|
+
'%m' # month
|
93
|
+
end
|
76
94
|
end
|
77
95
|
|
78
96
|
when ONE_DIGIT_REGEXP
|
data/lib/stamp/version.rb
CHANGED