Skip to content

Commit

Permalink
feat(#125): finish PositiveFloatString<S>
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGw committed May 1, 2024
1 parent 1ca8deb commit beea535
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ export type PositiveFloat<N extends Numeric> = IfEquals<
never
>;

/**
* Represents a positive float parsed from a string.
* If the string does not represent a positive float, it resolves to `never`, else
* it resolves to its float representation.
* @example
* ````ts
PositiveFloatString<'0'>; // never
PositiveFloatString<'82739283293237'>; // works
PositiveFloatString<'-82739.283293237'>; // never
PositiveFloatString<'-1'>; // never
PositiveFloatString<'1.98'>; // works
PositiveFloatString<'-1.98'>; // never
* ````
*/
export type PositiveFloatString<S extends string> = IfEquals<
IsPositiveFloat<Float<NumerifyString<S>>>,
true,
Float<NumerifyString<S>>,
never
>;

/**
* Type representing a float that's in ]-∞, 0[
*/
Expand Down Expand Up @@ -167,6 +188,7 @@ export type NegativeFloatString<S extends string> = IfEquals<
Float<NumerifyString<S>>,
never
>;

/**
* Is it a negative float ?
* @return
Expand Down
1 change: 0 additions & 1 deletion tests/negative-float-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ test(() => {
const result: TestType<NegativeFloatString<'-1'>, never, true> = true;
expect(result).toBe(true);
});

39 changes: 39 additions & 0 deletions tests/positive-float-string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PositiveFloatString, TestType } from '@/types';
import { test, expect } from 'vitest';

test(() => {
const result: TestType<PositiveFloatString<'0'>, never, true> = true;
expect(result).toBe(true);
});

test(() => {
const result: TestType<
PositiveFloatString<'-82739283293237'>,
never,
true
> = true;
expect(result).toBe(true);
});

test(() => {
const result: TestType<
PositiveFloatString<'82739.283293237'>,
82739.283293237,
true
> = true;
expect(result).toBe(true);
});

test(() => {
const result: TestType<PositiveFloatString<'-0.54'>, never, true> = true;
expect(result).toBe(true);
});

test(() => {
const result: TestType<PositiveFloatString<'0.54'>, 0.54, true> = true;
expect(result).toBe(true);
});
test(() => {
const result: TestType<PositiveFloatString<'-1'>, never, true> = true;
expect(result).toBe(true);
});

0 comments on commit beea535

Please sign in to comment.