This commit is contained in:
38
src/tests.ts
38
src/tests.ts
@@ -1,4 +1,24 @@
|
|||||||
import { mixTraits } from "./trait"
|
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 class Identified<ID> {
|
||||||
@@ -9,12 +29,18 @@ abstract class Identified<ID> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ImplementsIdentifiable<ID> extends Identified<ID> {
|
||||||
|
id!: ID
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract class Permissible {
|
abstract class Permissible {
|
||||||
|
static readonly defaultPermissions: string[] = []
|
||||||
permissions: string[] = []
|
permissions: string[] = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class User extends mixTraits(
|
class User extends expresses(
|
||||||
Identified<bigint>,
|
Identified<bigint>,
|
||||||
// Identified<string>,
|
// Identified<string>,
|
||||||
Permissible,
|
Permissible,
|
||||||
@@ -27,11 +53,13 @@ class User extends mixTraits(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(Permissible.constructor())
|
console.log(new User(BigInt(1)))
|
||||||
|
|
||||||
|
// console.log(Permissible.constructor())
|
||||||
// console.log(Object.getOwnPropertyNames(User.prototype))
|
// console.log(Object.getOwnPropertyNames(User.prototype))
|
||||||
|
|
||||||
const user1 = new User(BigInt(1))
|
// const user1 = new User(BigInt(1))
|
||||||
const user2 = new User(BigInt(2))
|
// const user2 = new User(BigInt(2))
|
||||||
|
|
||||||
// console.log(user1.equals(user2))
|
// console.log(user1.equals(user2))
|
||||||
// console.log(user1.permissions)
|
// console.log(user1.permissions)
|
||||||
|
|||||||
27
src/trait.ts
27
src/trait.ts
@@ -1,5 +1,5 @@
|
|||||||
import { AbstractClass, Class, UnionToIntersection } from "type-fest"
|
import { AbstractClass, Class, UnionToIntersection } from "type-fest"
|
||||||
import { StaticMembers, copyClassProperties, flattenClass } from "./util/class"
|
import { StaticMembers, copyProperties, flattenClass, getInheritanceHierarchy } from "./util/class"
|
||||||
|
|
||||||
|
|
||||||
export type Trait<T> =
|
export type Trait<T> =
|
||||||
@@ -40,7 +40,7 @@ export const extendAndApplyTraits = <
|
|||||||
traits: Traits,
|
traits: Traits,
|
||||||
) =>
|
) =>
|
||||||
traits.reduce((class_, trait) => {
|
traits.reduce((class_, trait) => {
|
||||||
copyClassProperties(flattenClass(trait), class_)
|
copyProperties(flattenClass(trait), class_)
|
||||||
return class_
|
return class_
|
||||||
}, class extends classToExtend {}) as (
|
}, class extends classToExtend {}) as (
|
||||||
AbstractClass<
|
AbstractClass<
|
||||||
@@ -61,15 +61,25 @@ export const extendAndApplyTraits = <
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
export const mixTraits = <
|
export function expresses<
|
||||||
Traits extends readonly Trait<any>[]
|
Traits extends readonly Trait<any>[]
|
||||||
>(
|
>(
|
||||||
...traits: Traits
|
...traits: Traits
|
||||||
) =>
|
) {
|
||||||
traits.reduce((class_, trait) => {
|
const class_ = class {}
|
||||||
copyClassProperties(flattenClass(trait), class_)
|
|
||||||
return class_
|
traits.forEach(trait => {
|
||||||
}, class {}) as (
|
getInheritanceHierarchy(trait).forEach(current => {
|
||||||
|
copyProperties(
|
||||||
|
current,
|
||||||
|
class_,
|
||||||
|
["name", "length"],
|
||||||
|
["constructor"],
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return class_ as unknown as (
|
||||||
Trait<
|
Trait<
|
||||||
UnionToIntersection<
|
UnionToIntersection<
|
||||||
InstanceType<
|
InstanceType<
|
||||||
@@ -84,3 +94,4 @@ export const mixTraits = <
|
|||||||
>
|
>
|
||||||
>
|
>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,47 +10,6 @@ import { AbstractClass } from "type-fest"
|
|||||||
export type StaticMembers<C> = Pick<C, keyof C>
|
export type StaticMembers<C> = Pick<C, keyof C>
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 copyClassProperties(
|
|
||||||
from: AbstractClass<any, any>,
|
|
||||||
to: AbstractClass<any, any>,
|
|
||||||
) {
|
|
||||||
Object.getOwnPropertyNames(from).forEach(name => {
|
|
||||||
// console.log(from, to, name, Object.getOwnPropertyDescriptor(from, name))
|
|
||||||
|
|
||||||
if (name === "name"
|
|
||||||
|| name === "length"
|
|
||||||
|| name === "prototype"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
Object.defineProperty(
|
|
||||||
to,
|
|
||||||
name,
|
|
||||||
Object.getOwnPropertyDescriptor(from, name) || Object.create(null),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.getOwnPropertyNames(from.prototype).forEach(name => {
|
|
||||||
// console.log(from, to, name, Object.getOwnPropertyDescriptor(from, name))
|
|
||||||
|
|
||||||
if (name === "constructor")
|
|
||||||
return
|
|
||||||
|
|
||||||
Object.defineProperty(
|
|
||||||
to.prototype,
|
|
||||||
name,
|
|
||||||
Object.getOwnPropertyDescriptor(from.prototype, name) || Object.create(null),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flattens the inheritance hierarchy of a given class by copying all properties
|
* Flattens the inheritance hierarchy of a given class by copying all properties
|
||||||
* from its superclass chain into a single object.
|
* from its superclass chain into a single object.
|
||||||
@@ -62,11 +21,16 @@ export function copyClassProperties(
|
|||||||
export function flattenClass<
|
export function flattenClass<
|
||||||
C extends AbstractClass<any, any>
|
C extends AbstractClass<any, any>
|
||||||
>(class_: C) {
|
>(class_: C) {
|
||||||
return getInheritanceHierarchy(class_)
|
const flattenedClass = class {} as unknown as C
|
||||||
.reduce((flattened, current) => {
|
|
||||||
copyClassProperties(current, flattened)
|
getInheritanceHierarchy(class_).forEach(current => {
|
||||||
return flattened
|
copyProperties(current, flattenedClass)
|
||||||
}, class {}) as C
|
})
|
||||||
|
|
||||||
|
copyProperty(class_, flattenedClass, "name")
|
||||||
|
copyProperty(class_.prototype, flattenedClass.prototype, "constructor")
|
||||||
|
|
||||||
|
return flattenedClass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,7 +38,7 @@ export function flattenClass<
|
|||||||
* Retrieves the inheritance hierarchy of a given class, including itself.
|
* Retrieves the inheritance hierarchy of a given class, including itself.
|
||||||
*
|
*
|
||||||
* @param class_ - The class for which the inheritance hierarchy is retrieved.
|
* @param class_ - The class for which the inheritance hierarchy is retrieved.
|
||||||
* @returns An array representing the inheritance hierarchy.
|
* @returns An array representing the inheritance hierarchy, ordered from the furthest in the hierarchy to `class_` itself.
|
||||||
*/
|
*/
|
||||||
export function getInheritanceHierarchy(
|
export function getInheritanceHierarchy(
|
||||||
class_: AbstractClass<any, any>
|
class_: AbstractClass<any, any>
|
||||||
@@ -96,3 +60,50 @@ export function getInheritanceHierarchy(
|
|||||||
export function isClass(el: { toString: () => string }) {
|
export function isClass(el: { toString: () => string }) {
|
||||||
return Boolean(el.toString().match(/^class(?: [.\S]+)?(?: extends [.\S]+)? {[\s\S]*}$/))
|
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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user