forked from Joxit/docker-registry-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(taglist-order): add new option
TAGLIST_ORDER
(Joxit#307)
Create new option named `TAGLIST_ORDER` that will order tags using user defined configuration. Available values are: | value | description | | --- | --- | | `num-asc;alpha-asc` | Numbers come first in **ascending** order then alphabet in **ascending** order | | `num-desc;alpha-asc` | Numbers come first in **descending** order then alphabet in **ascending** order | | `num-asc;alpha-desc` | Numbers come first in **ascending** order then alphabet in **descending** order | | `num-desc;alpha-desc` | Numbers come first in **descending** order then alphabet in **descending** order | | `alpha-asc;num-asc` | Alphabet come first in **ascending** order then numbers in **ascending** order | | `alpha-asc;num-desc` (default) | Alphabet come first in **ascending** order then numbers in **descending** order | | `alpha-desc;num-asc` | Alphabet come first in **descending** order then numbers in **ascending** order | | `alpha-desc;num-desc` | Alphabet come first in **descending** order then numbers in **descending** order | Some examples in [test/taglist-order.test.js](https://github.com/Joxit/docker-registry-ui/blob/8bbfc5c390c334e66741d5a639d4b17c8fe6fcea/test/taglist-order.test.js) closes Joxit#294
- Loading branch information
Showing
10 changed files
with
277 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class DockerRegistryUIError extends Error { | ||
constructor(msg) { | ||
super(msg); | ||
this.isError = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { DockerRegistryUIError } from './error.js'; | ||
import { isDigit } from './utils.js'; | ||
|
||
const TAGLIST_ORDER_REGEX = /(alpha-(asc|desc);num-(asc|desc))|(num-(asc|desc);alpha-(asc|desc))/; | ||
|
||
export const taglistOrderVariants = (taglistOrder) => { | ||
switch (taglistOrder) { | ||
case 'desc': | ||
return 'alpha-desc;num-desc'; | ||
case 'asc': | ||
return 'num-asc;alpha-asc'; | ||
case 'alpha-desc': | ||
case 'alpha-asc': | ||
case 'num-desc': | ||
case 'num-asc': | ||
return `${taglistOrder};${taglistOrder.startsWith('num') ? 'alpha' : 'num'}-asc`; | ||
default: | ||
if (!taglistOrder) { | ||
return 'alpha-asc;num-desc'; | ||
} else if (TAGLIST_ORDER_REGEX.test(taglistOrder)) { | ||
return taglistOrder; | ||
} | ||
throw new DockerRegistryUIError(`The taglist order \`${taglistOrder}\` is not recognized.`); | ||
} | ||
}; | ||
|
||
export const taglistOrderParser = (taglistOrder) => { | ||
const orders = taglistOrderVariants(taglistOrder) | ||
.split(';') | ||
.filter((e) => e) | ||
.map((e) => e.split('-').filter((e) => e)) | ||
.reduce((acc, e, idx) => { | ||
if (e.length > 1) { | ||
acc[e[0] + 'Asc'] = e[1] === 'asc'; | ||
} | ||
if (idx === 0) { | ||
acc.numFirst = e[0] === 'num'; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
return orders; | ||
}; | ||
|
||
export const tagReduce = (acc, e) => { | ||
if (acc.length > 0 && isDigit(acc[acc.length - 1].charAt(0)) == isDigit(e)) { | ||
acc[acc.length - 1] += e; | ||
} else { | ||
acc.push(e); | ||
} | ||
return acc; | ||
}; | ||
|
||
export const splitTagToArray = (tag) => | ||
tag | ||
.split('') | ||
.reduce(tagReduce, []) | ||
.map((e) => (isDigit(e.charAt(0)) ? parseInt(e) : e)); | ||
|
||
const applyOrder = (order, e1, e2) => { | ||
if (e1 === e2) { | ||
return 0; | ||
} | ||
const numFirst = order.numFirst ? 1 : -1; | ||
if (typeof e1 === 'number') { | ||
const factor = order.numAsc ? 1 : -1; | ||
return typeof e2 === 'number' ? (e1 - e2) * factor : -1 * numFirst; | ||
} else if (typeof e2 === 'number') { | ||
return 1 * numFirst; | ||
} else { | ||
const factor = order.alphaAsc ? 1 : -1; | ||
return e1.localeCompare(e2) * factor; | ||
} | ||
}; | ||
|
||
export const getTagComparator = (order) => { | ||
return (e1, e2) => { | ||
const tag1 = splitTagToArray(e1.tag || e1); | ||
const tag2 = splitTagToArray(e2.tag || e2); | ||
|
||
for (var i = 0; i < tag1.length && i < tag2.length; i++) { | ||
const compare = applyOrder(order, tag1[i], tag2[i]); | ||
if (compare != 0) { | ||
return compare; | ||
} | ||
} | ||
return (e1.tag || e1).length - (e2.tag || e2).length; | ||
}; | ||
}; |
Oops, something went wrong.