liquidjs icon indicating copy to clipboard operation
liquidjs copied to clipboard

Liquid filter: where - compatibility with Jekyll

Open Nowaker opened this issue 3 years ago • 2 comments
trafficstars

where filter in Liquidjs is not compatible with Jekyll syntax. This is a reimplemented version of where that works the same as Jekyll:

  engine.registerFilter('where', function (input, property, value) {
    const array = (function (val) {
      if (_.isArray(val)) {
        return val
      }

      if (_.isString(val) && val.length > 0) {
        return [val]
      }

      if (_.isObject(val)) {
        return Object.keys(val).map(function (key) {
          return val[key]
        })
      }

      return []
    })(input)

    let valueString = value
    if (_.isBoolean(value)) {
      valueString = value.toString()
    }

    return array.filter((obj) => {
      let prop = obj[property]
      if (prop == value) {
        return true
      }

      if (_.isBoolean(prop) && prop.toString() == valueString) {
        return true
      }

      return false
    })
  })

How would we go about this? Maybe rename jekyllInclude option into jekyllMode, and enable the include feature, and this filter when enabled?

Or implement jekyllWhere that enables a different implementation, plus jekyllMode that when enabled, enables all Jekyll compatibilities?

Nowaker avatar Jan 22 '22 01:01 Nowaker

@Nowaker , thank you for digging into this!

This is a reimplemented version of where that works the same as Jekyll

I'm OK to merge this into LiquidJS, but we need test cases to highlight the difference. Or, a diff patch to highlight the change for https://github.com/harttle/liquidjs/blob/1e69d86b0c474054cdf2a4d47e0f799d837dd2ef/src/builtin/filters/array.ts#L40-L45

I guess maybe we need a toJekyllArray?

Or implement jekyllWhere that enables a different implementation, plus jekyllMode that when enabled, enables all Jekyll compatibilities?

I prefer the latter, it's backward-compatible.

harttle avatar Jan 22 '22 14:01 harttle

but we need test cases to highlight the difference

This is totally understood. After I've finished my Jekyll-in-Gatsby project, including business deliverables, I'll work on putting that code into Liquidjs and submitting PRs.

Nowaker avatar Jan 28 '22 00:01 Nowaker