Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquid filter: where - compatibility with Jekyll #447

Open
Nowaker opened this issue Jan 22, 2022 · 2 comments
Open

Liquid filter: where - compatibility with Jekyll #447

Nowaker opened this issue Jan 22, 2022 · 2 comments

Comments

@Nowaker
Copy link
Contributor

Nowaker commented Jan 22, 2022

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?

@harttle
Copy link
Owner

harttle commented Jan 22, 2022

@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

export function where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): T[] {
return toArray(arr).filter(obj => {
const value = this.context.getFromScope(obj, String(property).split('.'))
return expected === undefined ? isTruthy(value, this.context) : value === expected
})
}

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.

@Nowaker
Copy link
Contributor Author

Nowaker commented Jan 28, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants