mirror of
https://github.com/usetrmnl/trmnl-liquid.git
synced 2026-08-13 23:18:19 -07:00
Added locale date and ordinalize support for special words
Necessary to match link:https://shopify.github.io/liquid/filters/date[Liquid Date] behavior where you can use special words (i.e. "now" or "today") to equate to `Time.now`. The specs use regular expressions because we are dealing with relative dates which would cause build failures if using exact string matches. Milestone: minor
This commit is contained in:
@@ -151,6 +151,7 @@ module TRMNL
|
||||
|
||||
def to_time value
|
||||
case value
|
||||
when "now", "today" then Time.now
|
||||
when Integer then Time.at(value)
|
||||
else Time.parse value
|
||||
end
|
||||
|
||||
@@ -171,6 +171,16 @@ RSpec.describe TRMNL::Liquid::Filters do
|
||||
end
|
||||
|
||||
describe "#l_date" do
|
||||
it "answers now as date/time" do
|
||||
content = renderer.call %({{ "now" | l_date: "%Y-%m-%d" }}), {}
|
||||
expect(content).to match(/\d{4}-\d{2}-\d{2}/)
|
||||
end
|
||||
|
||||
it "answers today as date/time" do
|
||||
content = renderer.call %({{ "today" | l_date: "%Y-%m-%d" }}), {}
|
||||
expect(content).to match(/\d{4}-\d{2}-\d{2}/)
|
||||
end
|
||||
|
||||
it "answers UNIX timestamp as date/time" do
|
||||
content = renderer.call %({{ 1770134949 | l_date: "%Y %b" }}), {}
|
||||
expect(content).to eq("2026 Feb")
|
||||
@@ -297,6 +307,16 @@ RSpec.describe TRMNL::Liquid::Filters do
|
||||
end
|
||||
|
||||
describe "#ordinalize" do
|
||||
it "answers now as date/time" do
|
||||
content = renderer.call %({{ "now" | ordinalize: "%B <<ordinal_day>>" }}), {}
|
||||
expect(content).to match(/[a-zA-Z]+ \d+[a-z]{2}/)
|
||||
end
|
||||
|
||||
it "answers today as date/time" do
|
||||
content = renderer.call %({{ "today" | ordinalize: "%B <<ordinal_day>>" }}), {}
|
||||
expect(content).to match(/[a-zA-Z]+ \d+[a-z]{2}/)
|
||||
end
|
||||
|
||||
it "answers UNIX timestamp as date/time" do
|
||||
content = renderer.call %({{ 1770134949 | ordinalize: "%A, %B <<ordinal_day>>, %Y" }}), {}
|
||||
expect(content).to eq("Tuesday, February 3rd, 2026")
|
||||
|
||||
Reference in New Issue
Block a user