-
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.
Changed TS target from ES2023 to ES2022
- Loading branch information
Showing
4 changed files
with
49 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { capitalize } from './string.js'; | ||
|
||
export type Sep = '_' | '-'; | ||
|
||
export type Part = `${number}.${number}.${number}`; | ||
|
||
export type Full = Part | `${Part}${Sep}${string}${Sep | '.'}${Part | number}`; | ||
|
||
type Type<S extends string, Acc extends string = ''> = S extends `${infer First}${infer Rest}` ? (First extends Sep | '.' ? Acc : Type<Rest, `${Acc}${First}`>) : Acc; | ||
|
||
export type Parse<T extends Full, StripCore extends boolean> = T extends `${infer Core}${Sep}${infer Rest}` | ||
? Rest extends `${infer U}` | ||
? { | ||
full: T; | ||
core: Core; | ||
type: Type<U>; | ||
pre: U extends `${Type<U>}${Sep | '.'}${infer Pre}` ? Pre : ''; | ||
display: `${StripCore extends true | ||
? Core extends '1.0.0' | ||
? '' | ||
: `${Core} ` | ||
: `${Core} `}${Capitalize<Type<U>>}${U extends `${Type<U>}${Sep | '.'}${infer Pre}` ? ` ${Pre}` : ''}`; | ||
} | ||
: never | ||
: T extends Part | ||
? { | ||
full: T; | ||
core: T; | ||
display: T; | ||
} | ||
: never; | ||
|
||
/** | ||
* Parses a semver version, including compile-time results | ||
* @param full the full version to parse | ||
* @param stripCore Whether to strip the leading core version in the display version when the core version is 1.0.0 (default false) | ||
*/ | ||
export function parse<const T extends Full>(full: T): Parse<T, false>; | ||
export function parse<const T extends Full, const S extends boolean>(full: T, stripCore: S): Parse<T, S>; | ||
export function parse<const T extends Full, const S extends boolean>(full: T, stripCore?: S): Parse<T, S> { | ||
const { type, pre, core } = /^(?<core>\d+\.\d+\.\d+)(?:[-_](?<type>[^-_.]+)[-_.](?<pre>\d*(?:\.\d+)*))?/.exec(full)!.groups!; | ||
const display = type ? `${stripCore && core == '1.0.0' ? '' : core + ' '}${capitalize(type)}${pre ? ` ${pre}` : ''}` : core; | ||
return { full, core, pre, type, display } as Parse<T, S>; | ||
} |
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