Sha256: d4823695178131c33812409b66e2115750d3a014b1c1fca7c51dd4046f1eba14

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Jekyll
  module Filters
    module Assertion
      # Compares two values.
      #
      # Example usage:
      #
      # {{ item.url | equals: page.url }}
      #
      # @param [Any]
      # @param [Any]
      # @return [TrueClass|FalseClass]
      def equals(input, comparison)
        input == comparison
      end

      # Ternary operator.  If the input value is truthy, return first
      # argument, otherwise returns the last.
      #
      # Example usage:
      #
      # {% assign active = item.url | equals: page.url %}
      # {{ active | ternary: 'active', '' }}
      #
      # @param [Any]
      # @param [Any]
      # @param [Any]
      # @return [Any]
      def ternary(input, value, alternative)
        empty = case input
                when Integer then input.zero?
                when Float then input.zero?
                when TrueClass then input
                when FalseClass then input
                when NilClass then false
                else input.empty?
                end

        (empty || !!input) ? value : alternative
      end
    end
  end
end

Liquid::Template.register_filter(Jekyll::Filters::Assertion)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sutty-liquid-0.4.1 lib/jekyll/filters/assertion.rb
sutty-liquid-0.4.0 lib/jekyll/filters/assertion.rb
sutty-liquid-0.3.0 lib/jekyll/filters/assertion.rb