Skip to content

Commit

Permalink
feat(types): v0.0.12 (#4)
Browse files Browse the repository at this point in the history
* feat(types): init v0.0.12

* fix(types): Remove duplicated tests (#5)

* feat(types): ArrayOfN  type now accepts any type as Fill (#6)

* Feat(types): Add equal type and derivatives (#7)

* feat(types): Added the Box helper type

* feat(types): Added the Equal type

* feat(types): Added the LessThanEqual derived type

* chore(types): Exposed the Box, Equal, and LessThanEqual types

* chore(types): added patch notes

* chore(*): Updated versions
  • Loading branch information
YLfjuk authored Jan 23, 2025
1 parent 16df300 commit 78db8c7
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .markdownlint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "@ylfjuk/md"
"extends": "@ylfjuk/md",
"MD013": {
"line_length": 120
}
}
9 changes: 9 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @ylfjuk/core

## 0.0.13

### Patch Changes

- Updated dependencies [4af919a]
- Updated dependencies [78da6dd]
- Updated dependencies [457d7e7]
- @ylfjuk-core/types@0.0.12

## 0.0.12

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ylfjuk/core",
"version": "0.0.12",
"version": "0.0.13",
"description": "The core packages",
"author": "YLfjuk",
"type": "module",
Expand Down Expand Up @@ -33,7 +33,7 @@
"test:types": "tsc --project tsconfig.test.json"
},
"dependencies": {
"@ylfjuk-core/types": "0.0.11",
"@ylfjuk-core/types": "0.0.12",
"@ylfjuk-core/utils": "0.0.6"
},
"devDependencies": {
Expand Down
20 changes: 17 additions & 3 deletions packages/types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
# @ylfjuk-core/types

## 0.0.12

### Patch Changes

#### Features 🆕

- Added Types
- Box | Boxes union types
- Equal | Returns if two types are strictly equal
- LessThanEqual | Returns if a number is less than or equal to another number | ⚠️ Floats Unsupported

- Modified Types
- ArrayOfN | Fill now accepts any type

## 0.0.11

### Patch Changes

#### Features 🆕

- Added types
- Added Types
- `Increment` | Increments a N ∈ ℕ number by 1 | ⚠️ Negative/Floats Unsupported

#### Fixes 🩹
Expand All @@ -23,7 +37,7 @@

#### Features 🆕

- Added types
- Added Types
- Abs | Returns the absolute value of a number
- ArrayOfN | Returns an array of n length
- AtLeastOne | Requires at least one of the fields in an object
Expand All @@ -47,7 +61,7 @@

#### Features 🆕

- Added types
- Added Types
- DeepDict
- EmptyObject
- InverseExtract
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ylfjuk-core/types",
"version": "0.0.11",
"version": "0.0.12",
"author": "YLfjuk",
"type": "module",
"license": "MIT",
Expand Down
6 changes: 2 additions & 4 deletions packages/types/src/array-of-n.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
//? maybe change the order to check if eq before limit (would lead to change in less-than -> less-than-eq-to)

import type { Abs } from './abs';

/**
* @param Amount - amount of zeros
* @param Limit - exclusive upper bound
* @param Acc - A prior accumulation of 0[]
* @param Fill - A number to fill the array with
* @param Fill - What to fill the array with
*/
export type ArrayOfN<
Amount extends number,
Limit extends number = -1,
Acc extends Fill[] = [],
Fill extends number = 0
Fill = 0
> = Abs<Amount> extends Amount
? Acc['length'] extends Limit
? never
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Box<T> = { _: T };
11 changes: 11 additions & 0 deletions packages/types/src/equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Box } from './box';

/**
* @alternative
* type Equal<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false
*/
export type Equal<A, B> = Box<A> extends Box<B>
? Box<B> extends Box<A>
? true
: false
: false;
2 changes: 1 addition & 1 deletion packages/types/src/increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import type { IsPositiveOrZero } from './is-positive-or-zero';
*/
export type Increment<N extends number> = IsPositiveOrZero<N> extends true
? [...ArrayOfN<N>, N]['length']
: 'error: Unsupported numeric value';
: 'Error: Unsupported numeric value';

// TODO: add support for Z, R values
3 changes: 3 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export type * from './abs';
export type * from './array-of-n';
export type * from './at-least-one';
export type * from './box';
export type * from './deep-dict';
export type * from './empty-object';
export type * from './equal';
export type * from './exactly-one';
export type * from './extend';
export type * from './extract-literals';
Expand All @@ -13,6 +15,7 @@ export type * from './inverse-extract';
export type * from './is-disjoint-union';
export type * from './is-positive-or-zero';
export type * from './is-tuple';
export type * from './less-than-equal';
export type * from './less-than';
export type * from './mapped-enum';
export type * from './mask-literals';
Expand Down
15 changes: 15 additions & 0 deletions packages/types/src/less-than-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Equal } from './equal';
import type { LessThan } from './less-than';

/**
* @description
* A, B ∈ ℤ (integers)
*
* @note does not support float values
*/
export type LessThanEqual<A extends number, B extends number> = Equal<
A,
B
> extends true
? true
: LessThan<A, B>;
9 changes: 8 additions & 1 deletion packages/types/tests/array-of-n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ describe('Array of n', () => {
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('Acc can not contain numbers other than Fill value', () => {
test('Acc can not contain values other than Fill value', () => {
// @ts-expect-error: testing
type Actual = ArrayOfN<4, -1, [1], 2>;
type Expected = [2, 2, 2, 2];

expectTypeOf<Actual>().not.toEqualTypeOf<Expected>();
});

test('Can be filled with any type', () => {
type Actual = ArrayOfN<4, -1, [], true>;
type Expected = [true, true, true, true];

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
});
17 changes: 17 additions & 0 deletions packages/types/tests/box.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expectTypeOf, it } from 'vitest';
import type { InverseExtract } from '../src';
import type { Box } from './../src/box';

describe('A type to Box union types', () => {
it('should match full unions instead of testing each element separately', () => {
type UnionIsTestedSeparately = InverseExtract<string | number, string>;

type Actual = InverseExtract<Box<string | number>, Box<string>>;

type Expected = never;

expectTypeOf<UnionIsTestedSeparately>().toEqualTypeOf<string>();
expectTypeOf<UnionIsTestedSeparately>().not.toEqualTypeOf<Expected>();
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
});
39 changes: 39 additions & 0 deletions packages/types/tests/equal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { Equal } from './../src/equal';

describe('A type that checks if two other types are strictly equal', () => {
test('diff types', () => {
type Actual = Equal<string, number>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('same types', () => {
type Actual = Equal<number, number>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('same literals', () => {
type Actual = Equal<2, 2>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('literals and mask', () => {
type Actual = Equal<2, number>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('type and union with it', () => {
type Actual = Equal<string | number, number>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
});
17 changes: 1 addition & 16 deletions packages/types/tests/increment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,7 @@ describe('returns an increment of the provided value', () => {

test('negative int', () => {
type Actual = Increment<-1>;
type Expected = 'error: Unsupported numeric value';

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test.todo('negative int', () => {
type Actual = Increment<-1>;
type Expected = 0;

// @ts-expect-error: unsupported
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});

test('negative int', () => {
type Actual = Increment<-1>;
type Expected = 'error: Unsupported numeric value';
type Expected = 'Error: Unsupported numeric value';

expectTypeOf<Actual>().toEqualTypeOf<Expected>();
});
Expand Down
97 changes: 97 additions & 0 deletions packages/types/tests/less-than-equal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { LessThanEqual } from '../src/less-than-equal';

describe('returns if a number is less than another number', () => {
test('+x < +y', () => {
type Actual = LessThanEqual<1, 2>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('+x = +y', () => {
type Actual = LessThanEqual<1, 1>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('+x > +y', () => {
type Actual = LessThanEqual<2, 1>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('+x > 0', () => {
type Actual = LessThanEqual<1, 0>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('0 < +y', () => {
type Actual = LessThanEqual<0, 1>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('0 = 0', () => {
type Actual = LessThanEqual<0, 0>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('-x < +y', () => {
type Actual = LessThanEqual<-3, 3>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('+x > -y', () => {
type Actual = LessThanEqual<3, -2>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('-x = -y', () => {
type Actual = LessThanEqual<-2, -2>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('-x < -y', () => {
type Actual = LessThanEqual<-3, -2>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('-x > -y', () => {
type Actual = LessThanEqual<-2, -3>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('-x < 0', () => {
type Actual = LessThanEqual<-3, 0>;
type Expected = true;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test('0 > -y', () => {
type Actual = LessThanEqual<0, -3>;
type Expected = false;

expectTypeOf<Actual>().toEqualTypeOf<Expected>;
});

test.todo('add float support');
});
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"devDependencies": {
"@ylfjuk/tsconfigs": "^0.0.6",
"@ylfjuk-core/types": "^0.0.11",
"@ylfjuk-core/types": "^0.0.12",
"tsup": "^8.3.5",
"typescript": "^5.7.2"
}
Expand Down
2 changes: 1 addition & 1 deletion playground/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@hono/node-server": "^1.13.7",
"@ylfjuk/core": "^0.0.12",
"@ylfjuk/core": "^0.0.13",
"hono": "^4.6.13"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion playground/vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"vite": "^6.0.3"
},
"dependencies": {
"@ylfjuk/core": "^0.0.12"
"@ylfjuk/core": "^0.0.13"
}
}

0 comments on commit 78db8c7

Please sign in to comment.