Skip to content

Commit

Permalink
Added export for version utils
Browse files Browse the repository at this point in the history
Added version utils regex export
  • Loading branch information
james-pre committed Oct 7, 2024
1 parent c1416b5 commit 10e8e79
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './random.js';
export * from './string.js';
export * from './struct.js';
export * from './types.js';
export * as version from './version.js';
4 changes: 3 additions & 1 deletion src/versions.ts → src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type Parse<T extends Full, StripCore extends boolean> = T extends `${infe
}
: never;

export const regex = /^(?<core>\d+\.\d+\.\d+)(?:[-_](?<type>[^-_.]+)[-_.](?<pre>\d*(?:\.\d+)*))?/;

/**
* Parses a semver version, including compile-time results
* @param full the full version to parse
Expand All @@ -38,7 +40,7 @@ export type Parse<T extends Full, StripCore extends boolean> = T extends `${infe
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 { type, pre, core } = regex.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>;
}

0 comments on commit 10e8e79

Please sign in to comment.