Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/path-parse-1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryDodzin committed Aug 29, 2021
2 parents 038decb + 6ed5708 commit 0f1727b
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 249 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-tools-talk.md
@@ -0,0 +1,5 @@
---
"oxidation-ts": minor
---

feat: change api to use runtypes
42 changes: 0 additions & 42 deletions docs/interfaces/IFunction.md

This file was deleted.

45 changes: 6 additions & 39 deletions docs/interfaces/ITrait.md
Expand Up @@ -8,64 +8,31 @@
| :------ |
| `Definition` |

## Hierarchy

- `IBaseType`<`Definition`\>

**`ITrait`**

## Table of contents

### Properties

- [\_reflect](ITrait.md#_reflect)
- [key](ITrait.md#key)

### Methods

- [implFor](ITrait.md#implfor)

## Properties

### \_reflect

`Readonly` **\_reflect**: `Definition`

#### Inherited from

IBaseType.\_reflect

___

### key

`Readonly` **key**: `Symbol`

#### Inherited from

IBaseType.key
- [impl](ITrait.md#impl)

## Methods

### implFor
### impl

**implFor**<`TraitDefinition`, `Static`, `Type`\>(`type`, `definition`): `ImplementedType`<`TraitDefinition`\> & `Type`
**impl**<`Type`\>(`type`, `definition`): `StaticDefinition`<`Definition`\> & `Type`

#### Type parameters

| Name | Type |
| :------ | :------ |
| `TraitDefinition` | `TraitDefinition` |
| `Static` | `Static` |
| `Type` | extends [`IType`](IType.md)<`Static`, `Type`\> |
| `Type` | extends `Runtype`<`unknown`, `Type`\> |

#### Parameters

| Name | Type |
| :------ | :------ |
| `type` | `Type` |
| `definition` | `ImplementedType`<`TraitDefinition`\> |
| `definition` | `StaticDefinition`<`Definition`\> |

#### Returns

`ImplementedType`<`TraitDefinition`\> & `Type`
`StaticDefinition`<`Definition`\> & `Type`
68 changes: 0 additions & 68 deletions docs/interfaces/IType.md

This file was deleted.

9 changes: 0 additions & 9 deletions docs/modules.md
Expand Up @@ -6,23 +6,14 @@

### Interfaces

- [IFunction](interfaces/IFunction.md)
- [ITrait](interfaces/ITrait.md)
- [IType](interfaces/IType.md)

### Variables

- [Trait](modules.md#trait)
- [Type](modules.md#type)

## Variables

### Trait

`Const` **Trait**: `ITraitApi`

___

### Type

`Const` **Type**: `ITypeApi`
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -29,6 +29,9 @@
"resolutions": {
"prettier": "2.3.2"
},
"peerDependencies": {
"runtypes": ">=6.3.0"
},
"devDependencies": {
"@changesets/changelog-github": "0.4.0",
"@changesets/cli": "2.16.0",
Expand All @@ -41,6 +44,7 @@
"jest": "27.0.6",
"jest-junit": "12.2.0",
"lint-staged": "11.0.1",
"runtypes": "6.3.1",
"ts-jest": "27.0.3",
"ts-node": "10.1.0",
"typedoc": "0.21.4",
Expand Down
1 change: 0 additions & 1 deletion src/function/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/function/interfaces.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/index.ts
@@ -1,6 +1,2 @@
export type { IFunction } from './function';

export type { ITrait } from './trait';
export { Trait } from './trait';
export type { IType } from './type';
export { Type } from './type';
6 changes: 0 additions & 6 deletions src/interfaces.ts

This file was deleted.

37 changes: 27 additions & 10 deletions src/trait/__tests__/trait.spec.ts
@@ -1,17 +1,34 @@
import { Type } from '../../type';
import { Trait } from '../trait';
import type { Static } from 'runtypes';
import { Record, String } from 'runtypes';

import { Trait } from '../..';

describe('traits', () => {
it('implements', () => {
const Foo = Trait.create();
it('create', () => {
const Foo = Trait.create<{
print(): typeof String;
}>();

expect(Foo).toBeDefined();
});

it('impl', () => {
const Foo = Trait.create<{
print(self: any): string;
}>();

const Bar = Foo.implFor(Type.create<string>(), {
foo(): string {
return 'bar';
const MyType = Foo.impl(
Record({
foobar: String,
}),
{
print(self: Static<typeof MyType>) {
return self.foobar;
},
},
});
);

expect(Bar.foo).toBeDefined();
expect(Bar.foo()).toStrictEqual('bar');
expect(MyType.print).toBeDefined();
expect(MyType.print({ foobar: 'baz' })).toStrictEqual('baz');
});
});
85 changes: 78 additions & 7 deletions src/trait/interfaces.ts
@@ -1,13 +1,84 @@
import type { IBaseType } from '../interfaces';
import type { IType, ImplementedType } from '../type';
import type { Runtype, Static } from 'runtypes';

export interface ITrait<Definition> extends IBaseType<Definition> {
implFor<TraitDefinition, Static, Type extends IType<Static>>(
type: Type,
definition: ImplementedType<TraitDefinition>,
): ImplementedType<TraitDefinition> & Type;
export interface ITrait<Definition> {
impl<Type extends Runtype>(type: Type, definition: StaticDefinition<Definition>): StaticDefinition<Definition> & Type;
}

export interface ITraitApi {
create<Definition>(): ITrait<Definition>;
}

export type StaticDefinition<Definition> = Definition extends Runtype
? Static<Definition>
: Definition extends () => infer Result
? () => StaticDefinition<Result>
: Definition extends (arg1: infer Arg1) => infer Result
? (arg1: StaticDefinition<Arg1>) => StaticDefinition<Result>
: Definition extends (arg1: infer Arg1, arg2: infer Arg2) => infer Result
? (arg1: StaticDefinition<Arg1>, arg2: StaticDefinition<Arg2>) => StaticDefinition<Result>
: Definition extends (arg1: infer Arg1, arg2: infer Arg2, arg3: infer Arg3) => infer Result
? (
arg1: StaticDefinition<Arg1>,
arg2: StaticDefinition<Arg2>,
arg3: StaticDefinition<Arg3>,
) => StaticDefinition<Result>
: Definition extends (arg1: infer Arg1, arg2: infer Arg2, arg3: infer Arg3, arg4: infer Arg4) => infer Result
? (
arg1: StaticDefinition<Arg1>,
arg2: StaticDefinition<Arg2>,
arg3: StaticDefinition<Arg3>,
arg4: StaticDefinition<Arg4>,
) => StaticDefinition<Result>
: Definition extends (
arg1: infer Arg1,
arg2: infer Arg2,
arg3: infer Arg3,
arg4: infer Arg4,
arg5: infer Arg5,
) => infer Result
? (
arg1: StaticDefinition<Arg1>,
arg2: StaticDefinition<Arg2>,
arg3: StaticDefinition<Arg3>,
arg4: StaticDefinition<Arg4>,
arg5: StaticDefinition<Arg5>,
) => StaticDefinition<Result>
: Definition extends (
arg1: infer Arg1,
arg2: infer Arg2,
arg3: infer Arg3,
arg4: infer Arg4,
arg5: infer Arg5,
arg6: infer Arg6,
) => infer Result
? (
arg1: StaticDefinition<Arg1>,
arg2: StaticDefinition<Arg2>,
arg3: StaticDefinition<Arg3>,
arg4: StaticDefinition<Arg4>,
arg5: StaticDefinition<Arg5>,
arg6: StaticDefinition<Arg6>,
) => StaticDefinition<Result>
: Definition extends (
arg1: infer Arg1,
arg2: infer Arg2,
arg3: infer Arg3,
arg4: infer Arg4,
arg5: infer Arg5,
arg6: infer Arg6,
arg7: infer Arg7,
) => infer Result
? (
arg1: StaticDefinition<Arg1>,
arg2: StaticDefinition<Arg2>,
arg3: StaticDefinition<Arg3>,
arg4: StaticDefinition<Arg4>,
arg5: StaticDefinition<Arg5>,
arg6: StaticDefinition<Arg6>,
arg7: StaticDefinition<Arg7>,
) => StaticDefinition<Result>
: Definition extends (...args: infer Args) => infer Result
? (...args: StaticDefinition<Args[keyof Args]>[]) => StaticDefinition<Result>
: Definition extends Record<string, any>
? { [Key in keyof Definition]: StaticDefinition<Definition[Key]> }
: unknown;

0 comments on commit 0f1727b

Please sign in to comment.