I want to take a list of years and collapse them down to hyphenated ranges. It works better for small screen on mobile devices, displaying “2009-2016” instead of “2009, 2010, 2011, 2012, etc.”

e.g., there are 3 transforms on the array of integers [2015, 2017, 2006, 2010, 2009, 2018, 2008]

  1. sort the years

    [2006, 2008, 2009, 2010, 2015, 2017, 2018]

  2. group into subsequent years

    [[2006], [2008, 2009, 2010], [2015], [2017, 2018]]

  3. collapse into ranges

    [‘2006’, ‘2008-2010’, ‘2015’, ‘2017-2018’]

Read the rest of this entry »