Skip to content

Commit

Permalink
perf: Improve performance of build_style method
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Sep 29, 2024
1 parent 4185c5d commit fe32ae3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/phlex/variants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ def build_style(**variants)

# @api private
def build_variants_style(variants)
self::STYLE_DEFAULTS.merge(variants.compact).map do |variant, option|
value = self::STYLE_VARIANTS.dig(variant, option)
variants = variants.compact
variants = self::STYLE_DEFAULTS.merge(variants) unless self::STYLE_DEFAULTS.empty?

if value.nil?
raise VariantNotFoundError, "Variant `#{variant}: #{option.inspect}` doesn't exist"
variants.map do |variant, option|
options = self::STYLE_VARIANTS[variant]

if options
value = options[option]

next value if value
end

value
raise VariantNotFoundError, "Variant `#{variant}: #{option.inspect}` doesn't exist"
end
end
end
Expand Down

0 comments on commit fe32ae3

Please sign in to comment.