This commit is contained in:
@@ -1,74 +0,0 @@
|
|||||||
import { AbstractClass } from "type-fest"
|
|
||||||
import { expresses } from "./trait"
|
|
||||||
|
|
||||||
|
|
||||||
function inspectClass(class_: AbstractClass<any, any>) {
|
|
||||||
Object.getOwnPropertyNames(class_).forEach(name => {
|
|
||||||
console.log(
|
|
||||||
"[static]",
|
|
||||||
name,
|
|
||||||
Object.getOwnPropertyDescriptor(class_, name)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.getOwnPropertyNames(class_.prototype).forEach(name => {
|
|
||||||
console.log(
|
|
||||||
"[prototype]",
|
|
||||||
name,
|
|
||||||
Object.getOwnPropertyDescriptor(class_.prototype, name)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
abstract class Identified<ID> {
|
|
||||||
abstract id: ID
|
|
||||||
|
|
||||||
equals(el: Identified<ID>) {
|
|
||||||
return this.id === el.id
|
|
||||||
}
|
|
||||||
|
|
||||||
// initializer() {
|
|
||||||
// console.log("Identified initializer")
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
class ImplementsIdentifiable<ID> extends Identified<ID> {
|
|
||||||
id!: ID
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
abstract class Permissible {
|
|
||||||
static readonly defaultPermissions: string[] = []
|
|
||||||
permissions: string[] = []
|
|
||||||
// permissions!: string[]
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
console.log("Permissible constructor")
|
|
||||||
}
|
|
||||||
|
|
||||||
initializer() {
|
|
||||||
console.log("Permissible initializer")
|
|
||||||
this.permissions = []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class User extends expresses(
|
|
||||||
Identified as typeof Identified<bigint>,
|
|
||||||
// Identified<string>,
|
|
||||||
Permissible,
|
|
||||||
) {
|
|
||||||
readonly id: bigint
|
|
||||||
|
|
||||||
constructor(id: bigint) {
|
|
||||||
super()
|
|
||||||
this.id = id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const user1 = new User(BigInt(1))
|
|
||||||
const user2 = new User(BigInt(2))
|
|
||||||
|
|
||||||
console.log(user1)
|
|
||||||
console.log(user1.equals(user2))
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
import { AbstractClass, Class, UnionToIntersection } from "type-fest"
|
|
||||||
import { StaticMembers, copyProperties, getInheritanceHierarchy } from "./util/class"
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a trait that can be used to define common behavior
|
|
||||||
* for classes and abstract classes.
|
|
||||||
* @typeParam T - The type of the trait.
|
|
||||||
*/
|
|
||||||
export type Trait<T> =
|
|
||||||
AbstractClass<T, []>
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a link class that expresses the given traits.
|
|
||||||
* @param traits - An array of traits to be expressed by the link class.
|
|
||||||
* @returns A dynamically created class that expresses the given traits.
|
|
||||||
* @typeParam Traits - An array of traits that the link class expresses.
|
|
||||||
*/
|
|
||||||
export function expresses<
|
|
||||||
Traits extends readonly Trait<any>[]
|
|
||||||
>(
|
|
||||||
...traits: Traits
|
|
||||||
) {
|
|
||||||
return makeLinkClass(traits)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a link class that extends a base class and expresses the given traits.
|
|
||||||
* @param extend - The base class or abstract class to extend.
|
|
||||||
* @param traits - An array of traits to be expressed by the link class.
|
|
||||||
* @returns A dynamically created class that extends the given base class and expresses the given traits.
|
|
||||||
* @typeParam C - The type of the base class to extend.
|
|
||||||
* @typeParam Traits - An array of traits that the link class expresses.
|
|
||||||
*/
|
|
||||||
export function extendsAndExpresses<
|
|
||||||
C extends Class<any, any>
|
|
||||||
| AbstractClass<any, any>,
|
|
||||||
Traits extends readonly Trait<any>[],
|
|
||||||
>(
|
|
||||||
extend: C,
|
|
||||||
...traits: Traits
|
|
||||||
) {
|
|
||||||
return makeLinkClass(traits, extend)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a link class that expresses the given traits and optionally extends a base class.
|
|
||||||
* @param traits - An array of traits to be expressed by the link class.
|
|
||||||
* @param extend - The base class or abstract class to extend (optional).
|
|
||||||
* @returns A dynamically created class that expresses the given traits and extends the base class.
|
|
||||||
* @typeParam Traits - An array of traits that the link class expresses.
|
|
||||||
* @typeParam C - The type of the base class to extend (optional).
|
|
||||||
*/
|
|
||||||
export function makeLinkClass<
|
|
||||||
Traits extends readonly Trait<any>[],
|
|
||||||
C extends Class<any, any>
|
|
||||||
| AbstractClass<any, any>
|
|
||||||
| undefined = undefined,
|
|
||||||
>(
|
|
||||||
traits: Traits,
|
|
||||||
extend?: C,
|
|
||||||
) {
|
|
||||||
const class_ = extend
|
|
||||||
? class extends extend {
|
|
||||||
constructor(...args: any[]) {
|
|
||||||
super(...args)
|
|
||||||
|
|
||||||
traits.forEach(trait => {
|
|
||||||
trait.prototype.initializer?.call(this)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
: class {
|
|
||||||
constructor() {
|
|
||||||
traits.forEach(trait => {
|
|
||||||
trait.prototype.initializer?.call(this)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
traits.forEach(trait => {
|
|
||||||
getInheritanceHierarchy(trait).forEach(current => {
|
|
||||||
copyProperties(
|
|
||||||
current,
|
|
||||||
class_,
|
|
||||||
["name", "length"],
|
|
||||||
["constructor"],
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return class_ as unknown as (
|
|
||||||
(C extends Class<any, any> | AbstractClass<any, any>
|
|
||||||
? (
|
|
||||||
AbstractClass<
|
|
||||||
InstanceType<C> &
|
|
||||||
UnionToIntersection<
|
|
||||||
InstanceType<
|
|
||||||
Traits[number]
|
|
||||||
>
|
|
||||||
>,
|
|
||||||
|
|
||||||
ConstructorParameters<C>
|
|
||||||
> &
|
|
||||||
|
|
||||||
StaticMembers<C>
|
|
||||||
)
|
|
||||||
: Trait<
|
|
||||||
UnionToIntersection<
|
|
||||||
InstanceType<
|
|
||||||
Traits[number]
|
|
||||||
>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
) &
|
|
||||||
|
|
||||||
StaticMembers<
|
|
||||||
UnionToIntersection<
|
|
||||||
Traits[number]
|
|
||||||
>
|
|
||||||
>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
import { AbstractClass } from "type-fest"
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the static members of a class.
|
|
||||||
*
|
|
||||||
* @template C - The type of the class for which static members are extracted.
|
|
||||||
* @typeparam The static members of the class.
|
|
||||||
*/
|
|
||||||
export type StaticMembers<C> = Pick<C, keyof C>
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flattens the inheritance hierarchy of a given class by copying all properties
|
|
||||||
* from its superclass chain into a single object.
|
|
||||||
*
|
|
||||||
* @template C - The type of the class to be flattened, extending AbstractClass<any, any>.
|
|
||||||
* @param C - The class to be flattened.
|
|
||||||
* @returns A new class with properties flattened from the entire inheritance hierarchy.
|
|
||||||
*/
|
|
||||||
export function flattenClass<
|
|
||||||
C extends AbstractClass<any, any>
|
|
||||||
>(class_: C) {
|
|
||||||
const flattenedClass = class {} as unknown as C
|
|
||||||
|
|
||||||
getInheritanceHierarchy(class_).forEach(current => {
|
|
||||||
copyProperties(current, flattenedClass)
|
|
||||||
})
|
|
||||||
|
|
||||||
copyProperty(class_, flattenedClass, "name")
|
|
||||||
copyProperty(class_.prototype, flattenedClass.prototype, "constructor")
|
|
||||||
|
|
||||||
return flattenedClass
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the inheritance hierarchy of a given class, including itself.
|
|
||||||
*
|
|
||||||
* @param class_ - The class for which the inheritance hierarchy is retrieved.
|
|
||||||
* @returns An array representing the inheritance hierarchy, ordered from the furthest in the hierarchy to `class_` itself.
|
|
||||||
*/
|
|
||||||
export function getInheritanceHierarchy(
|
|
||||||
class_: AbstractClass<any, any>
|
|
||||||
): AbstractClass<any, any>[] {
|
|
||||||
const parent = Object.getPrototypeOf(class_)
|
|
||||||
|
|
||||||
return isClass(parent)
|
|
||||||
? [...getInheritanceHierarchy(parent), class_]
|
|
||||||
: [class_]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a given element appears to be a class based on its string representation.
|
|
||||||
*
|
|
||||||
* @param el - The element to check for being a class.
|
|
||||||
* @returns `true` if the element is likely a class; otherwise, `false`.
|
|
||||||
*/
|
|
||||||
export function isClass(el: { toString: () => string }) {
|
|
||||||
return Boolean(el.toString().match(/^class(?: [.\S]+)?(?: extends [.\S]+)? {[\s\S]*}$/))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copies properties from one class to another, including static and prototype properties.
|
|
||||||
*
|
|
||||||
* @param from - The source class to copy properties from.
|
|
||||||
* @param to - The destination class to copy properties to.
|
|
||||||
*/
|
|
||||||
export function copyProperties(
|
|
||||||
from: AbstractClass<any, any>,
|
|
||||||
to: AbstractClass<any, any>,
|
|
||||||
ignoreKeys: string[] = [],
|
|
||||||
ignorePrototypeKeys: string[] = [],
|
|
||||||
) {
|
|
||||||
Object.getOwnPropertyNames(from).forEach(name => {
|
|
||||||
if (name === "prototype"
|
|
||||||
|| ignoreKeys.find(v => v === name)
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
// console.log(from, to, name, Object.getOwnPropertyDescriptor(from, name))
|
|
||||||
|
|
||||||
copyProperty(from, to, name)
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.getOwnPropertyNames(from.prototype).forEach(name => {
|
|
||||||
if (ignorePrototypeKeys.find(v => v === name))
|
|
||||||
return
|
|
||||||
|
|
||||||
// console.log(from, to, name, Object.getOwnPropertyDescriptor(from, name))
|
|
||||||
|
|
||||||
copyProperty(from.prototype, to.prototype, name)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export function copyProperty(
|
|
||||||
from: unknown,
|
|
||||||
to: unknown,
|
|
||||||
name: string,
|
|
||||||
) {
|
|
||||||
Object.defineProperty(
|
|
||||||
to,
|
|
||||||
name,
|
|
||||||
Object.getOwnPropertyDescriptor(from, name) || Object.create(null),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
import { Trait, TraitClass } from ".."
|
import { Trait, TraitClass } from ".."
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an array of classes corresponding to the provided traits.
|
||||||
|
* @template Traits - An array of traits extending Trait<any>.
|
||||||
|
*/
|
||||||
export type TraitsClasses<Traits extends readonly Trait<any>[]> = (
|
export type TraitsClasses<Traits extends readonly Trait<any>[]> = (
|
||||||
Traits extends [infer T, ...infer Rest]
|
Traits extends [infer T, ...infer Rest]
|
||||||
? T extends Trait<any>
|
? T extends Trait<any>
|
||||||
|
|||||||
Reference in New Issue
Block a user