Skip to content

Commit

Permalink
Added version utilities
Browse files Browse the repository at this point in the history
Changed TS target from ES2023 to ES2022
  • Loading branch information
james-pre committed Oct 7, 2024
1 parent ac7bbb5 commit 3e50221
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ A bunch of utilies for Typescript. This includes:
- RNG functions
- `List`, a class that combines the best aspects of `Set` and arrays
- `JSONFileMap` and `FolderMap`
- Version utilities
44 changes: 44 additions & 0 deletions src/versions.ts
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>;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"lib": ["ESNext"],
"module": "NodeNext",
"target": "ES2023",
"target": "ES2022",
"moduleResolution": "NodeNext",
"outDir": "dist",
"typeRoots": ["node_modules/@types"],
Expand Down

0 comments on commit 3e50221

Please sign in to comment.