mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 21:40:39 -07:00
fix: math filters coerce invalid string to 0, #813
This commit is contained in:
+13
-18
@@ -1,24 +1,19 @@
|
||||
import { toValue, argumentsToValue } from '../util/underscore'
|
||||
import { toNumber, argumentsToNumber } from '../util/underscore'
|
||||
|
||||
export const abs = argumentsToValue(Math.abs)
|
||||
export const at_least = argumentsToValue(Math.max)
|
||||
export const at_most = argumentsToValue(Math.min)
|
||||
export const ceil = argumentsToValue(Math.ceil)
|
||||
export const divided_by = argumentsToValue((dividend: number, divisor: number, integerArithmetic = false) => integerArithmetic ? Math.floor(dividend / divisor) : dividend / divisor)
|
||||
export const floor = argumentsToValue(Math.floor)
|
||||
export const minus = argumentsToValue((v: number, arg: number) => v - arg)
|
||||
export const modulo = argumentsToValue((v: number, arg: number) => v % arg)
|
||||
export const times = argumentsToValue((v: number, arg: number) => v * arg)
|
||||
export const abs = argumentsToNumber(Math.abs)
|
||||
export const at_least = argumentsToNumber(Math.max)
|
||||
export const at_most = argumentsToNumber(Math.min)
|
||||
export const ceil = argumentsToNumber(Math.ceil)
|
||||
export const divided_by = argumentsToNumber((dividend: number, divisor: number, integerArithmetic = false) => integerArithmetic ? Math.floor(dividend / divisor) : dividend / divisor)
|
||||
export const floor = argumentsToNumber(Math.floor)
|
||||
export const minus = argumentsToNumber((v: number, arg: number) => v - arg)
|
||||
export const plus = argumentsToNumber((lhs: number, rhs: number) => lhs + rhs)
|
||||
export const modulo = argumentsToNumber((v: number, arg: number) => v % arg)
|
||||
export const times = argumentsToNumber((v: number, arg: number) => v * arg)
|
||||
|
||||
export function round (v: number, arg = 0) {
|
||||
v = toValue(v)
|
||||
arg = toValue(arg)
|
||||
v = toNumber(v)
|
||||
arg = toNumber(arg)
|
||||
const amp = Math.pow(10, arg)
|
||||
return Math.round(v * amp) / amp
|
||||
}
|
||||
|
||||
export function plus (v: number, arg: number) {
|
||||
v = toValue(v)
|
||||
arg = toValue(arg)
|
||||
return Number(v) + Number(arg)
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,5 +1,4 @@
|
||||
import { assert } from './assert'
|
||||
import { toNumber } from './underscore'
|
||||
|
||||
export class Limiter {
|
||||
private message: string
|
||||
@@ -10,12 +9,12 @@ export class Limiter {
|
||||
this.limit = limit
|
||||
}
|
||||
use (count: number) {
|
||||
count = toNumber(count)
|
||||
count = +count || 0
|
||||
assert(this.base + count <= this.limit, this.message)
|
||||
this.base += count
|
||||
}
|
||||
check (count: number) {
|
||||
count = toNumber(count)
|
||||
count = +count || 0
|
||||
assert(count <= this.limit, this.message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,8 +67,7 @@ export function toValue (value: any): any {
|
||||
}
|
||||
|
||||
export function toNumber (value: any): number {
|
||||
value = Number(value)
|
||||
return isNaN(value) ? 0 : value
|
||||
return +toValue(value) || 0
|
||||
}
|
||||
|
||||
export function isNumber (value: any): value is number {
|
||||
@@ -191,6 +190,10 @@ export function argumentsToValue<F extends (...args: any) => any, T> (fn: F) {
|
||||
return function (this: T, ...args: Parameters<F>) { return fn.call(this, ...args.map(toValue)) }
|
||||
}
|
||||
|
||||
export function argumentsToNumber<F extends (...args: any) => any, T> (fn: F) {
|
||||
return function (this: T, ...args: Parameters<F>) { return fn.call(this, ...args.map(toNumber)) }
|
||||
}
|
||||
|
||||
export function escapeRegExp (text: string) {
|
||||
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user