mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
avoid new regex allocation in util functions
This commit is contained in:
+5
-2
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module Utils
|
module Utils
|
||||||
|
DECIMAL_REGEX = /\A-?\d+\.\d+\z/
|
||||||
|
UNIX_TIMESTAMP_REGEX = /\A\d+\z/
|
||||||
|
|
||||||
def self.slice_collection(collection, from, to)
|
def self.slice_collection(collection, from, to)
|
||||||
if (from != 0 || !to.nil?) && collection.respond_to?(:load_slice)
|
if (from != 0 || !to.nil?) && collection.respond_to?(:load_slice)
|
||||||
collection.load_slice(from, to)
|
collection.load_slice(from, to)
|
||||||
@@ -52,7 +55,7 @@ module Liquid
|
|||||||
when Numeric
|
when Numeric
|
||||||
obj
|
obj
|
||||||
when String
|
when String
|
||||||
/\A-?\d+\.\d+\z/.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
|
DECIMAL_REGEX.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
|
||||||
else
|
else
|
||||||
if obj.respond_to?(:to_number)
|
if obj.respond_to?(:to_number)
|
||||||
obj.to_number
|
obj.to_number
|
||||||
@@ -73,7 +76,7 @@ module Liquid
|
|||||||
case obj
|
case obj
|
||||||
when 'now', 'today'
|
when 'now', 'today'
|
||||||
Time.now
|
Time.now
|
||||||
when /\A\d+\z/, Integer
|
when UNIX_TIMESTAMP_REGEX, Integer
|
||||||
Time.at(obj.to_i)
|
Time.at(obj.to_i)
|
||||||
when String
|
when String
|
||||||
Time.parse(obj)
|
Time.parse(obj)
|
||||||
|
|||||||
Reference in New Issue
Block a user