From aebc1ba88087a996fe0de0f5f1507003b17b9c60 Mon Sep 17 00:00:00 2001 From: koilebeit Date: Fri, 1 Nov 2024 08:17:32 +0100 Subject: [PATCH] improve naming --- _plugins/sort_edtf.rb | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/_plugins/sort_edtf.rb b/_plugins/sort_edtf.rb index 90542c7b..cfe2bac0 100644 --- a/_plugins/sort_edtf.rb +++ b/_plugins/sort_edtf.rb @@ -1,20 +1,37 @@ module Jekyll module SortEDTF - def clean_date_string(str) + def sort_edtf(array_of_strings) + non_empty_dates = array_of_strings.reject { |str| str.strip.empty? } + + processed_dates = non_empty_dates.map do |str| + normalized_date_str = replace_x_with_parsable_0(str) + date_as_integer = normalize_to_integer(normalized_date_str) + display_format = create_display_format(str) + + { numeric: date_as_integer, original: str, display_format: display_format } + end + + sorted_dates = processed_dates.sort_by { |date| -date[:numeric] } + sorted_dates.map { |date| "#{date[:display_format]}:#{date[:original]}" } + end + + private + + def replace_x_with_parsable_0(str) str.gsub(/[Xu]/, '0') end - def compute_numeric_value(cleaned_str) - numeric_str = if cleaned_str.start_with?('-') - cleaned_str.sub(/^-0+/, '-') + def normalize_to_integer(year_str) + numeric_str = if year_str.start_with?('-') + year_str.sub(/^-0+/, '-') else - cleaned_str.sub(/^0+(?=\d)/, '') + year_str.sub(/^0+(?=\d)/, '') end if numeric_str.match?(/^-?\d+$/) numeric_str.to_i else - raise ArgumentError, "Invalid date format: #{cleaned_str}" + raise ArgumentError, "Invalid year format: #{year_str}" end end @@ -26,20 +43,7 @@ def create_display_format(str) end end - def sort_edtf(array_of_strings) - valid_dates = array_of_strings.reject { |str| str.strip.empty? } - - parsed_dates = valid_dates.map do |str| - cleaned_str = clean_date_string(str) - numeric_value = compute_numeric_value(cleaned_str) - display_format = create_display_format(str) - - { numeric: numeric_value, original: str, display_format: display_format } - end - sorted_dates = parsed_dates.sort_by { |date| -date[:numeric] } - sorted_dates.map { |date| "#{date[:display_format]}:#{date[:original]}" } - end end end