mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix(filters): modulo should follow divisor sign for negative operands (#922)
The `modulo` filter used JavaScript's `%` (truncated remainder, sign follows the dividend). Shopify/Ruby Liquid uses floored modulo, where the result takes the sign of the divisor. Since liquidjs advertises Shopify compatibility, negative operands produced the wrong sign. Use `((v % arg) + arg) % arg` to match Ruby's `%`. Positive-operand results are unchanged.
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ export const divided_by = argumentsToNumber((dividend: number, divisor: number,
|
|||||||
export const floor = argumentsToNumber(Math.floor)
|
export const floor = argumentsToNumber(Math.floor)
|
||||||
export const minus = argumentsToNumber((v: number, arg: number) => v - arg)
|
export const minus = argumentsToNumber((v: number, arg: number) => v - arg)
|
||||||
export const plus = argumentsToNumber((lhs: number, rhs: number) => lhs + rhs)
|
export const plus = argumentsToNumber((lhs: number, rhs: number) => lhs + rhs)
|
||||||
export const modulo = argumentsToNumber((v: number, arg: number) => v % arg)
|
export const modulo = argumentsToNumber((v: number, arg: number) => ((v % arg) + arg) % arg)
|
||||||
export const times = argumentsToNumber((v: number, arg: number) => v * arg)
|
export const times = argumentsToNumber((v: number, arg: number) => v * arg)
|
||||||
|
|
||||||
export function round (v: number, arg = 0) {
|
export function round (v: number, arg = 0) {
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ describe('filters/math', function () {
|
|||||||
expect(Number(html)).toBeCloseTo(3.357, 3)
|
expect(Number(html)).toBeCloseTo(3.357, 3)
|
||||||
})
|
})
|
||||||
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
|
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
|
||||||
|
it('should follow divisor sign for negative dividend', () => test('{{ -7 | modulo: 3 }}', '2'))
|
||||||
|
it('should follow divisor sign for negative divisor', () => test('{{ 7 | modulo: -3 }}', '-2'))
|
||||||
|
it('should follow divisor sign for negative float', () => test('{{ -4.5 | modulo: 3 }}', '1.5'))
|
||||||
})
|
})
|
||||||
describe('plus', function () {
|
describe('plus', function () {
|
||||||
it('should return "6" for 4,2', () => test('{{ 4 | plus: 2 }}', '6'))
|
it('should return "6" for 4,2', () => test('{{ 4 | plus: 2 }}', '6'))
|
||||||
|
|||||||
Reference in New Issue
Block a user