Updated comments
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2023-12-29 00:22:59 +01:00
parent b248f878a1
commit 13c2113aa2
2 changed files with 69 additions and 10 deletions

View File

@@ -39,10 +39,45 @@ export type UnwrapTraitC<T> =
/**
* Creates a trait instance using the provided trait applier function.
* Creates a trait using the provided trait applier function.
* @template C - The abstract class type.
* @param applier - The trait applier function.
* @returns A trait instance.
* @returns A trait.
* @example
* Creates a trait:
* ```ts
* const Permissible = trait(Parent => {
* abstract class Permissible extends Parent {
* static readonly defaultPermissions: string[] = []
* permissions: string[] = []
*
* constructor(...args: any[]) {
* super(...args)
* }
* }
*
* return Permissible
* })
* ```
* Creates a generic trait:
* ```ts
* const Identifiable = <ID>() =>
* trait(Parent => {
* abstract class Identifiable extends Parent {
* abstract readonly id: ID
*
* equals(el: Identifiable) {
* return this.id === el.id
* }
*
* constructor(...args: any[]) {
* super(...args)
* }
* }
*
* return Identifiable
* })
* ```
*/
export function trait<
C extends AbstractClass<any>
@@ -56,10 +91,22 @@ export function trait<
/**
* Extends a class with the given traits and expresses their combined functionality.
* @template C - The abstract class type.
* @template Traits - An array of trait instances.
* @template Traits - An array of traits.
* @param extend - The class to extend.
* @param traits - An array of trait instances to apply.
* @param traits - An array of traits to apply.
* @returns A new class type expressing the combined functionality of the base class and traits.
* @example
* Extends a superclass and applies traits:
* ```ts
* class User extends extendsAndExpresses(Entity, [Identifiable<bigint>(), Permissible]) {
* readonly id: bigint
*
* constructor(id: bigint) {
* super()
* this.id = id
* }
* }
* ```
*/
export function extendsAndExpresses<
C extends AbstractClass<any>,
@@ -98,9 +145,21 @@ export function extendsAndExpresses<
/**
* Expresses the combined functionality of multiple traits.
* @template Traits - An array of trait instances.
* @param traits - An array of trait instances to apply.
* @template Traits - An array of trait.
* @param traits - An array of trait to apply.
* @returns A new class type expressing the combined functionality of the traits.
* @example
* Applies traits to a class:
* ```ts
* class User extends expresses(Identifiable<bigint>(), Permissible) {
* readonly id: bigint
*
* constructor(id: bigint) {
* super()
* this.id = id
* }
* }
* ```
*/
export function expresses<
Traits extends readonly Trait<any>[],

View File

@@ -3,10 +3,10 @@ import { expresses, trait } from "."
const Identifiable = <ID>() =>
trait(Parent => {
abstract class Identified extends Parent {
abstract id: ID
abstract class Identifiable extends Parent {
abstract readonly id: ID
equals(el: Identified) {
equals(el: Identifiable) {
return this.id === el.id
}
@@ -16,7 +16,7 @@ const Identifiable = <ID>() =>
}
}
return Identified
return Identifiable
})
const ImplementsIdentifiable = <ID>(defaultID: ID) =>