diff --git a/.markdownlint.json b/.markdownlint.json index d42c879..f080e0f 100644 --- a/.markdownlint.json +++ b/.markdownlint.json @@ -1,3 +1,6 @@ { - "extends": "@ylfjuk/md" + "extends": "@ylfjuk/md", + "MD013": { + "line_length": 120 + } } \ No newline at end of file diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 81402f0..55bb7b1 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -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 diff --git a/packages/core/package.json b/packages/core/package.json index bd14f7f..f9a47f7 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@ylfjuk/core", - "version": "0.0.12", + "version": "0.0.13", "description": "The core packages", "author": "YLfjuk", "type": "module", @@ -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": { diff --git a/packages/types/CHANGELOG.md b/packages/types/CHANGELOG.md index 1bcfab6..6b490f4 100644 --- a/packages/types/CHANGELOG.md +++ b/packages/types/CHANGELOG.md @@ -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 🩹 @@ -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 @@ -47,7 +61,7 @@ #### Features 🆕 -- Added types +- Added Types - DeepDict - EmptyObject - InverseExtract diff --git a/packages/types/package.json b/packages/types/package.json index e404e4a..c44cc07 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -1,6 +1,6 @@ { "name": "@ylfjuk-core/types", - "version": "0.0.11", + "version": "0.0.12", "author": "YLfjuk", "type": "module", "license": "MIT", diff --git a/packages/types/src/array-of-n.ts b/packages/types/src/array-of-n.ts index a7795af..5bc8830 100644 --- a/packages/types/src/array-of-n.ts +++ b/packages/types/src/array-of-n.ts @@ -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 extends Amount ? Acc['length'] extends Limit ? never diff --git a/packages/types/src/box.ts b/packages/types/src/box.ts new file mode 100644 index 0000000..0d486b2 --- /dev/null +++ b/packages/types/src/box.ts @@ -0,0 +1 @@ +export type Box = { _: T }; diff --git a/packages/types/src/equal.ts b/packages/types/src/equal.ts new file mode 100644 index 0000000..a54c634 --- /dev/null +++ b/packages/types/src/equal.ts @@ -0,0 +1,11 @@ +import type { Box } from './box'; + +/** + * @alternative + * type Equal = (() => G extends A ? 1 : 2) extends (() => G extends B ? 1 : 2) ? true : false + */ +export type Equal = Box extends Box + ? Box extends Box + ? true + : false + : false; diff --git a/packages/types/src/increment.ts b/packages/types/src/increment.ts index 232476f..bc9ba7d 100644 --- a/packages/types/src/increment.ts +++ b/packages/types/src/increment.ts @@ -7,6 +7,6 @@ import type { IsPositiveOrZero } from './is-positive-or-zero'; */ export type Increment = IsPositiveOrZero extends true ? [...ArrayOfN, N]['length'] - : 'error: Unsupported numeric value'; + : 'Error: Unsupported numeric value'; // TODO: add support for Z, R values diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index a0b8000..41d58f2 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -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'; @@ -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'; diff --git a/packages/types/src/less-than-equal.ts b/packages/types/src/less-than-equal.ts new file mode 100644 index 0000000..a744dd6 --- /dev/null +++ b/packages/types/src/less-than-equal.ts @@ -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 = Equal< + A, + B +> extends true + ? true + : LessThan; diff --git a/packages/types/tests/array-of-n.spec.ts b/packages/types/tests/array-of-n.spec.ts index 6a0e477..3cecb21 100644 --- a/packages/types/tests/array-of-n.spec.ts +++ b/packages/types/tests/array-of-n.spec.ts @@ -70,11 +70,18 @@ describe('Array of n', () => { expectTypeOf().toEqualTypeOf(); }); - 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().not.toEqualTypeOf(); }); + + test('Can be filled with any type', () => { + type Actual = ArrayOfN<4, -1, [], true>; + type Expected = [true, true, true, true]; + + expectTypeOf().toEqualTypeOf(); + }); }); diff --git a/packages/types/tests/box.spec.ts b/packages/types/tests/box.spec.ts new file mode 100644 index 0000000..1adadc8 --- /dev/null +++ b/packages/types/tests/box.spec.ts @@ -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; + + type Actual = InverseExtract, Box>; + + type Expected = never; + + expectTypeOf().toEqualTypeOf(); + expectTypeOf().not.toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + }); +}); diff --git a/packages/types/tests/equal.spec.ts b/packages/types/tests/equal.spec.ts new file mode 100644 index 0000000..83e7fb9 --- /dev/null +++ b/packages/types/tests/equal.spec.ts @@ -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; + type Expected = false; + + expectTypeOf().toEqualTypeOf(); + }); + + test('same types', () => { + type Actual = Equal; + type Expected = true; + + expectTypeOf().toEqualTypeOf(); + }); + + test('same literals', () => { + type Actual = Equal<2, 2>; + type Expected = true; + + expectTypeOf().toEqualTypeOf(); + }); + + test('literals and mask', () => { + type Actual = Equal<2, number>; + type Expected = false; + + expectTypeOf().toEqualTypeOf(); + }); + + test('type and union with it', () => { + type Actual = Equal; + type Expected = false; + + expectTypeOf().toEqualTypeOf(); + }); +}); diff --git a/packages/types/tests/increment.spec.ts b/packages/types/tests/increment.spec.ts index 9b9f74f..d30b11e 100644 --- a/packages/types/tests/increment.spec.ts +++ b/packages/types/tests/increment.spec.ts @@ -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().toEqualTypeOf(); - }); - - test.todo('negative int', () => { - type Actual = Increment<-1>; - type Expected = 0; - - // @ts-expect-error: unsupported - expectTypeOf().toEqualTypeOf(); - }); - - test('negative int', () => { - type Actual = Increment<-1>; - type Expected = 'error: Unsupported numeric value'; + type Expected = 'Error: Unsupported numeric value'; expectTypeOf().toEqualTypeOf(); }); diff --git a/packages/types/tests/less-than-equal.spec.ts b/packages/types/tests/less-than-equal.spec.ts new file mode 100644 index 0000000..3956bb4 --- /dev/null +++ b/packages/types/tests/less-than-equal.spec.ts @@ -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().toEqualTypeOf; + }); + + test('+x = +y', () => { + type Actual = LessThanEqual<1, 1>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('+x > +y', () => { + type Actual = LessThanEqual<2, 1>; + type Expected = false; + + expectTypeOf().toEqualTypeOf; + }); + + test('+x > 0', () => { + type Actual = LessThanEqual<1, 0>; + type Expected = false; + + expectTypeOf().toEqualTypeOf; + }); + + test('0 < +y', () => { + type Actual = LessThanEqual<0, 1>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('0 = 0', () => { + type Actual = LessThanEqual<0, 0>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('-x < +y', () => { + type Actual = LessThanEqual<-3, 3>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('+x > -y', () => { + type Actual = LessThanEqual<3, -2>; + type Expected = false; + + expectTypeOf().toEqualTypeOf; + }); + + test('-x = -y', () => { + type Actual = LessThanEqual<-2, -2>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('-x < -y', () => { + type Actual = LessThanEqual<-3, -2>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('-x > -y', () => { + type Actual = LessThanEqual<-2, -3>; + type Expected = false; + + expectTypeOf().toEqualTypeOf; + }); + + test('-x < 0', () => { + type Actual = LessThanEqual<-3, 0>; + type Expected = true; + + expectTypeOf().toEqualTypeOf; + }); + + test('0 > -y', () => { + type Actual = LessThanEqual<0, -3>; + type Expected = false; + + expectTypeOf().toEqualTypeOf; + }); + + test.todo('add float support'); +}); diff --git a/packages/utils/package.json b/packages/utils/package.json index c8656b2..82c177a 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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" } diff --git a/playground/server/package.json b/playground/server/package.json index 62a509c..5135222 100644 --- a/playground/server/package.json +++ b/playground/server/package.json @@ -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": { diff --git a/playground/vanilla/package.json b/playground/vanilla/package.json index e05c2b8..9fb9b98 100644 --- a/playground/vanilla/package.json +++ b/playground/vanilla/package.json @@ -12,6 +12,6 @@ "vite": "^6.0.3" }, "dependencies": { - "@ylfjuk/core": "^0.0.12" + "@ylfjuk/core": "^0.0.13" } }