From 13c2113aa2359076feb29dc7a5a142d314b054e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 29 Dec 2023 00:22:59 +0100 Subject: [PATCH] Updated comments --- src/index.ts | 71 +++++++++++++++++++++++++++++++++++++++++++++++----- src/tests.ts | 8 +++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9acfe8e..fc70958 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,10 +39,45 @@ export type UnwrapTraitC = /** - * 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 = () => + * 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 @@ -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(), Permissible]) { + * readonly id: bigint + * + * constructor(id: bigint) { + * super() + * this.id = id + * } + * } + * ``` */ export function extendsAndExpresses< C extends AbstractClass, @@ -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(), Permissible) { + * readonly id: bigint + * + * constructor(id: bigint) { + * super() + * this.id = id + * } + * } + * ``` */ export function expresses< Traits extends readonly Trait[], diff --git a/src/tests.ts b/src/tests.ts index b19ad37..37e9412 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -3,10 +3,10 @@ import { expresses, trait } from "." const Identifiable = () => 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 = () => } } - return Identified + return Identifiable }) const ImplementsIdentifiable = (defaultID: ID) =>