mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-19 06:20:38 -07:00
+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