diff --git a/Gemfile b/Gemfile index 82980ce..e854678 100644 --- a/Gemfile +++ b/Gemfile @@ -12,4 +12,4 @@ gem "rspec" # optional peer dependencies gem "i18n", "~> 1.14" gem "rails-i18n", "~> 8.0" -gem "trmnl-i18n", github: "usetrmnl/trmnl-i18n", branch: "main" \ No newline at end of file +gem "trmnl-i18n", github: "usetrmnl/trmnl-i18n", branch: "main" diff --git a/Gemfile.lock b/Gemfile.lock index c78b8cb..1422833 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,7 @@ GIT PATH remote: . specs: - trmnl-liquid (0.1.0) + trmnl-liquid (0.2.0) actionview (~> 8.0) liquid (~> 5.6) redcarpet (~> 3.6) diff --git a/lib/trmnl/liquid/filters.rb b/lib/trmnl/liquid/filters.rb index 1523b99..bfd2b03 100644 --- a/lib/trmnl/liquid/filters.rb +++ b/lib/trmnl/liquid/filters.rb @@ -69,6 +69,10 @@ module TRMNL JSON.generate(obj) end + def parse_json(obj) + JSON.parse(obj) + end + def sample(array) = array.sample private diff --git a/spec/trmnl/liquid/filters_spec.rb b/spec/trmnl/liquid/filters_spec.rb index 2724db4..8c0f36f 100644 --- a/spec/trmnl/liquid/filters_spec.rb +++ b/spec/trmnl/liquid/filters_spec.rb @@ -79,9 +79,12 @@ describe TRMNL::Liquid::Filters do expect_render('{{ data | json }}', '[{"a":1,"b":"c"},"d"]', 'data' => [{ 'a' => 1, 'b' => 'c' }, 'd']) end + it 'supports parse_json' do + expect_render('{{ data | parse_json }}', [{ 'a' => 1, 'b' => 'c' }, 'd'].to_json, 'data' => '[{"a":1,"b":"c"},"d"]'.to_json) + end + it 'supports sample' do expect(["1", "2", "3", "4", "5"].any? { |str| str == service.parse('{{ data | split: "," | sample }}').render({ "data" => "1,2,3,4,5" }) }) expect(["cat", "dog"].any? { |str| str == service.parse('{{ data | split: "," | sample }}').render({ "data" => "cat,dog" }) }) end - -end \ No newline at end of file +end