Skip to content

fix: use numeric prefixes in math filters - #1016

Open
draarivpatel-ui wants to merge 1 commit into
harttle:masterfrom
draarivpatel-ui:fix-numeric-prefix-math
Open

draarivpatel-ui wants to merge 1 commit into
harttle:masterfrom
draarivpatel-ui:fix-numeric-prefix-math

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Summary

  • parse numeric prefixes in math filter operands, matching Ruby Liquid conversion for strings such as 10px
  • preserve the existing boolean conversion used by integer arithmetic
  • add regression coverage for plus, minus, times, and non-decimal numeric prefixes

The other compatibility gaps in the report are already fixed on current master; this covers the remaining math conversion difference.

Fixes #995

Test plan

  • npx jest test/integration/filters/math.spec.ts src/util/underscore.spec.ts --runInBand
  • npm run lint

Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Comment thread src/util/underscore.ts
return +toValue(value) || 0
value = toValue(value)
if (typeof value === 'boolean') return Number(value)
const num = parseFloat(value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseFloat keeps the decimal part of a prefix, but Ruby Liquid does not. In Utils.to_number, a string is read as a decimal only when the whole string matches /\A-?\d+\.\d+\z/. Any other string goes through String#to_i, which keeps only the integer prefix. So a decimal prefix with a unit gives a different result here than in Ruby (Ruby Liquid 5.14.0 compared with this branch):

template Ruby this PR
{{ "3.5px" | plus: 0 }} 3 3.5
{{ "3.5px" | times: 2 }} 6 7
{{ "4.2kg" | ceil }} 4 5
{{ "2.5 apples" | round }} 2 3
{{ "7.5x" | modulo: 2 }} 1 1.5

The new tests only use integer prefixes (3 items, 10px, 16px), so they pass either way.

A version that keeps plain numeric strings as they are now ("1.5", ".5", "1e3"), and uses the integer prefix like to_i for everything else:

export function toNumber (value: any): number {
  value = toValue(value)
  if (typeof value === 'string' && !/^\s*[+-]?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?\s*$/i.test(value)) {
    const prefix = /^\s*[+-]?\d+/.exec(value)
    return prefix ? Number(prefix[0]) : 0
  }
  return +value || 0
}

With this, all 5 rows above match Ruby. The new tests still pass: "3 items" | minus: 1 gives 2, "10px" | plus: 5 gives 15, "0x10" | plus: 0 gives 0 and "16px" | times: 2 gives 32. true | plus: 1 is still 2. One side effect: "Infinity" now gives 0 (Ruby gives 0 too; master gives Infinity).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compatibility gaps with Shopify Liquid: {%- endraw %} throws, escape_once double-escapes entities, numeric filters drop leading digits

3 participants