Sha256: 48c4949138afe9921d3d005fed9f4b0bac8b19aa51e4986c48548a0a9da98344

Contents?: true

Size: 772 Bytes

Versions: 10

Compression:

Stored size: 772 Bytes

Contents

require_relative 'string'

class Array
  def combine_zips
    zips = []
    start = nil
    last = nil

    self.sort.each do |zip|
      zip_int = zip.to_i

      if start == nil
        start = zip_int
        last = zip_int
      else
        if zip_int == last+1
          last = zip_int
        else
          if last == start
            zips << start.to_zip
          else
            zips << "#{start.to_zip}-#{last.to_zip}"
          end
          start = zip_int
          last = zip_int
        end
      end

    end

    # Return nil if the array is empty
    if last == nil and start == nil
        return ''
    end

    if last == start
      zips << start.to_zip
    else
      zips << "#{start.to_zip}-#{last.to_zip}"
    end

    zips.join ","
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
zipcoder-0.9.2 lib/zipcoder/ext/array.rb
zipcoder-0.9.1 lib/zipcoder/ext/array.rb
zipcoder-0.9.0 lib/zipcoder/ext/array.rb
zipcoder-0.8.4 lib/zipcoder/ext/array.rb
zipcoder-0.8.3 lib/zipcoder/ext/array.rb
zipcoder-0.8.2 lib/zipcoder/ext/array.rb
zipcoder-0.8.1 lib/zipcoder/ext/array.rb
zipcoder-0.8.0 lib/zipcoder/ext/array.rb
zipcoder-0.7.4 lib/zipcoder/ext/array.rb
zipcoder-0.7.3 lib/zipcoder/ext/array.rb