mirror of
https://github.com/TrentSPalmer/hugo_themes_report.git
synced 2025-11-21 02:08:25 -08:00
add test_sort_by.py (and theme_compare.py and add function to
database.py)
This commit is contained in:
43
test/theme_compare.py
Normal file
43
test/theme_compare.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import re
|
||||
MATCH = re.compile(r'\d+')
|
||||
|
||||
|
||||
def compare_jk(j, k):
|
||||
if j == k: return 0
|
||||
else: return 1 if j < k else -1
|
||||
|
||||
|
||||
def semver_split(item):
|
||||
split_list = [MATCH.search(i).group() if
|
||||
len(item) > 0 else '0' for i in item.split('.')]
|
||||
if len(split_list) == 1: split_list.append('0')
|
||||
if len(split_list) == 2: split_list.append('0')
|
||||
return [int(x) for x in split_list]
|
||||
|
||||
|
||||
def compare_theme(x, y, sort_key):
|
||||
if sort_key == 'sortByName':
|
||||
return compare_jk(y['name'].lower(), x['name'].lower())
|
||||
|
||||
elif sort_key == 'sortByStars':
|
||||
return compare_jk(int(x['stars']), int(y['stars']))
|
||||
|
||||
elif sort_key == 'sortByMinVer':
|
||||
x_list = semver_split(x['min_ver'])
|
||||
y_list = semver_split(y['min_ver'])
|
||||
return compare_jk(x_list, y_list)
|
||||
|
||||
elif sort_key == 'sortByDate':
|
||||
return compare_jk(x['date'], y['date'])
|
||||
|
||||
elif sort_key == 'sortByLicense':
|
||||
return compare_jk(y['license'].lower(), x['license'].lower())
|
||||
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def theme_compare(a, b, y):
|
||||
if len(y) == 0: return -1
|
||||
rslt = compare_theme(a, b, y[0])
|
||||
return theme_compare(a, b, y[1:]) if rslt == 0 else rslt
|
||||
Reference in New Issue
Block a user