Skip to content

Commit

Permalink
improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
koilebeit committed Nov 1, 2024
1 parent 6f8611d commit aebc1ba
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions _plugins/sort_edtf.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down

0 comments on commit aebc1ba

Please sign in to comment.