-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-assets.ts
More file actions
44 lines (39 loc) · 1.27 KB
/
github-assets.ts
File metadata and controls
44 lines (39 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @fileoverview Asset matching helpers for GitHub releases.
*/
import picomatch from '../external/picomatch'
import {
StringPrototypeEndsWith,
StringPrototypeStartsWith,
} from '../primordials'
/**
* Create a matcher function for a pattern using picomatch for glob patterns
* or simple prefix/suffix matching for object patterns.
*
* @param pattern - Pattern to match (string glob, prefix/suffix object, or RegExp)
* @returns Function that tests if a string matches the pattern
*
* @example
* ```typescript
* const isMatch = createAssetMatcher('tool-*-linux-x64')
* isMatch('tool-v1.0-linux-x64') // true
* isMatch('tool-v1.0-darwin-arm64') // false
* ```
*/
export function createAssetMatcher(
pattern: string | { prefix: string; suffix: string } | RegExp,
): (input: string) => boolean {
if (typeof pattern === 'string') {
// Use picomatch for glob pattern matching.
const isMatch = picomatch(pattern)
return (input: string) => isMatch(input)
}
if (pattern instanceof RegExp) {
return (input: string) => pattern.test(input)
}
// Prefix/suffix object pattern (backward compatible).
const { prefix, suffix } = pattern
return (input: string) =>
StringPrototypeStartsWith(input, prefix) &&
StringPrototypeEndsWith(input, suffix)
}