From b429bd3e7509eafa3f72fb918e691bb8dacc7b19 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Tue, 6 Jun 2023 17:54:58 +0200 Subject: Invert nil checks to improve performance Check for `nil` before checking for more expensive conditions which include a method call or an array scan. Also removes redundant comments on self-explanatory code --- lib/axlsx/workbook/worksheet/cell.rb | 16 ++++++++-------- lib/axlsx/workbook/worksheet/cell_serializer.rb | 2 +- lib/axlsx/workbook/worksheet/rich_text_run.rb | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 3146bca4..b9f78fdb 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -177,12 +177,12 @@ module Axlsx # Indicates if the cell is good for shared string table def plain_string? - (type == :string || type == :text) && # String typed - !is_text_run? && # No inline styles - !@value.nil? && # Not nil - !@value.empty? && # Not empty - !is_formula? && # Not a formula - !is_array_formula? # Not an array formula + (type == :string || type == :text) && # String typed + !value.nil? && + !value.empty? && + !is_text_run? && # No inline styles + !is_formula? && + !is_array_formula? end # The inline font_name property for the cell @@ -426,7 +426,7 @@ module Axlsx # Attempts to determine the correct width for this cell's content # @return [Float] def autowidth - return if is_formula? || value.nil? + return if value.nil? || is_formula? if contains_rich_text? string_width('', font_size) + value.autowidth @@ -525,7 +525,7 @@ module Axlsx # About Time - Time in OOXML is *different* from what you might expect. The history as to why is interesting, but you can safely assume that if you are generating docs on a mac, you will want to specify Workbook.1904 as true when using time typed values. # @see Axlsx#date1904 def cast_value(v) - return v if v.is_a?(RichText) || v.nil? + return v if v.nil? || v.is_a?(RichText) case type when :date diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 30a2b5fe..9075c9c3 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -27,7 +27,7 @@ module Axlsx if cell.is_text_run? valid = RichTextRun::INLINE_STYLES - [:value, :type] data = Axlsx.instance_values_for(cell).transform_keys(&:to_sym) - data = data.select { |key, value| valid.include?(key) && !value.nil? } + data = data.select { |key, value| !value.nil? && valid.include?(key) } RichText.new(cell.value.to_s, data).to_xml_string(str) elsif cell.contains_rich_text? cell.value.to_xml_string(str) diff --git a/lib/axlsx/workbook/worksheet/rich_text_run.rb b/lib/axlsx/workbook/worksheet/rich_text_run.rb index dac27752..02c350a7 100644 --- a/lib/axlsx/workbook/worksheet/rich_text_run.rb +++ b/lib/axlsx/workbook/worksheet/rich_text_run.rb @@ -208,7 +208,7 @@ module Axlsx def to_xml_string(str = +'') valid = RichTextRun::INLINE_STYLES data = Axlsx.instance_values_for(self).transform_keys(&:to_sym) - data = data.select { |key, value| valid.include?(key) && !value.nil? } + data = data.select { |key, value| !value.nil? && valid.include?(key) } str << '' data.each do |key, val| -- cgit v1.2.3