Skip to content

Commit 2d8753b

Browse files
committed
feat: support exclusive asset keyword matching
1 parent c08b751 commit 2d8753b

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/utils/asset/load-assets.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ export type MatchAssetOpts = {
2626
*/
2727
version: string
2828
/**
29-
* The keywords that must be in the asset name
29+
* The keywords that must be in the asset name.
30+
* If the element is a string, the keyword must be in the asset name.
31+
* If the element is an array, one of the keywords must be in the asset name.
3032
* @default []
3133
*/
32-
keywords?: string[]
34+
keywords?: (string | string[])[]
3335
/**
3436
* Optional keywords that are not required to be in the asset name
3537
* but increase the score of the asset if they are present
38+
*
39+
* if the element is a string, the keyword must be in the asset name
40+
* if the element is an array, one of the keywords must be in the asset name
3641
* @default []
3742
*/
38-
optionalKeywords?: string[]
43+
optionalKeywords?: (string | string[])[]
3944
/**
4045
* Custom version compare function
4146
* @param candidate The candidate version
@@ -158,7 +163,17 @@ function matchAssetName(tag: string, assetNames: string[], opts: MatchAssetOpts)
158163
&& opts.keywords.length !== 0
159164
) {
160165
for (const name of assetNames) {
161-
if (opts.keywords!.every((keyword) => name.includes(keyword))) {
166+
if (
167+
opts.keywords.every((keyword) => {
168+
// single keyword
169+
if (typeof keyword === "string" && name.includes(keyword)) {
170+
return true
171+
}
172+
// keyword choices
173+
return Array.isArray(keyword)
174+
&& keyword.some((k) => name.includes(k))
175+
})
176+
) {
162177
candidates.push(name)
163178
}
164179
}
@@ -179,7 +194,11 @@ function matchAssetName(tag: string, assetNames: string[], opts: MatchAssetOpts)
179194
const candidateScores = candidates.map((name) => {
180195
let score = 0
181196
for (const keyword of opts.optionalKeywords!) {
182-
if (name.includes(keyword)) {
197+
// single keyword
198+
if (typeof keyword === "string" && name.includes(keyword)) {
199+
score++
200+
} // keyword choices
201+
else if (Array.isArray(keyword) && keyword.some((k) => name.includes(k))) {
183202
score++
184203
}
185204
}

0 commit comments

Comments
 (0)