Skip to content

Commit

Permalink
feat: implFor on trait (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryDodzin committed Jul 18, 2021
2 parents 0ccd635 + ca841d1 commit b604a26
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-planes-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"oxidation-ts": patch
---

feat: add implFor on ITrait
29 changes: 29 additions & 0 deletions docs/interfaces/ITrait.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
- [\_reflect](ITrait.md#_reflect)
- [key](ITrait.md#key)

### Methods

- [implFor](ITrait.md#implfor)

## Properties

### \_reflect
Expand All @@ -40,3 +44,28 @@ ___
#### Inherited from

IBaseType.key

## Methods

### implFor

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

#### Type parameters

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

#### Parameters

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

#### Returns

`ImplementedType`<`TraitDefinition`\> & `Type`
6 changes: 3 additions & 3 deletions docs/interfaces/IType.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ IBaseType.key

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Definition` | extends `Record`<`string`, [`IFunction`](IFunction.md)<`fn`\>\> |
| Name |
| :------ |
| `Definition` |

#### Parameters

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"coverage": "jest --coverage",
"deduplicate": "yarn-deduplicate -s fewer",
"docs": "yarn typedoc --disableSources index.ts",
"lint": "eslint . --cache",
"lint": "eslint --ext .ts . --cache",
"prepare": "husky install",
"prerelease": "yarn build",
"release": "changeset publish",
Expand Down
17 changes: 17 additions & 0 deletions src/trait/__tests__/trait.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Type } from '../../type';
import { Trait } from '../trait';

describe('traits', () => {
it('implements', () => {
const Foo = Trait.create();

const Bar = Foo.implFor(Type.create<string>(), {
foo(): string {
return 'bar';
},
});

expect(Bar.foo).toBeDefined();
expect(Bar.foo()).toStrictEqual('bar');
});
});
8 changes: 7 additions & 1 deletion src/trait/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { IBaseType } from '../interfaces';
import type { IType, ImplementedType } from '../type';

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

export interface ITraitApi {
create<Definition>(): ITrait<Definition>;
Expand Down
8 changes: 7 additions & 1 deletion src/trait/trait.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { IType, ImplementedType } from '../type';
import { Type } from '../type';
import type { ITrait, ITraitApi } from './interfaces';

const create = <Definition>(): ITrait<Definition> => Type.create<Definition>();
const implFor = <Definition, Static, ImplType extends IType<Static>>(
type: ImplType,
definition: ImplementedType<Definition>,
): ImplementedType<Definition> & ImplType => type.impl(definition);

const create = <Definition>(): ITrait<Definition> => Object.assign(Type.create<Definition>(), { implFor });

export const Trait: ITraitApi = {
create,
Expand Down
1 change: 0 additions & 1 deletion src/type/__tests__/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('types', () => {
});

expect(Bar.foo).toBeDefined();
/* eslint-disable-next-line @typescript-eslint/no-confusing-void-expression */
expect(Bar.foo()).toStrictEqual('bar');
});
});
2 changes: 1 addition & 1 deletion src/type/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { IType } from './interfaces';
export type { IType, ImplementedType } from './interfaces';
export { Type } from './type';
5 changes: 1 addition & 4 deletions src/type/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { IFunction } from '../function';
import type { IBaseType, Reflect } from '../interfaces';

export type ImplementedType<Definition> = { [Key in keyof Definition]: Reflect<Definition[Key]> };

export interface IType<Static> extends IBaseType<Static> {
impl<Definition extends Record<string, IFunction>>(
implmintation: ImplementedType<Definition>,
): ImplementedType<Definition> & this;
impl<Definition>(implmintation: ImplementedType<Definition>): ImplementedType<Definition> & this;
}

export interface ITypeApi {
Expand Down
5 changes: 1 addition & 4 deletions src/type/type.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { IFunction } from '../function';
import type { IType, ITypeApi, ImplementedType } from './interfaces';

const create = <Static>(): IType<Static> => ({
key: Symbol(),
get _reflect(): Static {
throw new Error('Should not be called');
},
impl<Definition extends Record<string, IFunction>>(
implmintation: ImplementedType<Definition>,
): ImplementedType<Definition> & IType<Static> {
impl<Definition>(implmintation: ImplementedType<Definition>): ImplementedType<Definition> & IType<Static> {
return Object.assign(this, implmintation);
},
});
Expand Down

0 comments on commit b604a26

Please sign in to comment.