@telperion/js-utils 3.7.0 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/class/mixin.cjs +1 -1
- package/class/mixin.js +1 -1
- package/class/mixin.js.map +1 -1
- package/package.json +1 -1
package/class/mixin.cjs
CHANGED
|
@@ -54,7 +54,7 @@ function mixin(Base, Mixin) {
|
|
|
54
54
|
// Copy prototype methods from mixin to the mixed class
|
|
55
55
|
const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
|
|
56
56
|
for (const prop of mixinKeys) {
|
|
57
|
-
Mixed.prototype
|
|
57
|
+
Object.defineProperty(Mixed.prototype, prop, Object.getOwnPropertyDescriptor(Mixin.prototype, prop));
|
|
58
58
|
}
|
|
59
59
|
return Mixed;
|
|
60
60
|
}
|
package/class/mixin.js
CHANGED
|
@@ -51,7 +51,7 @@ export function mixin(Base, Mixin) {
|
|
|
51
51
|
// Copy prototype methods from mixin to the mixed class
|
|
52
52
|
const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
|
|
53
53
|
for (const prop of mixinKeys) {
|
|
54
|
-
Mixed.prototype
|
|
54
|
+
Object.defineProperty(Mixed.prototype, prop, Object.getOwnPropertyDescriptor(Mixin.prototype, prop));
|
|
55
55
|
}
|
|
56
56
|
return Mixed;
|
|
57
57
|
}
|
package/class/mixin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/js-utils/src/class/mixin.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAUvD,MAAM,eAAe,GAAG,CAAC,aAAa,CAAC,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,KAAK,CACnB,IAAO,EACP,KAAQ;IAKR,qEAAqE;IACrE,MAAM,KAAM,SAAQ,IAAI;QACtB,YAAY,QA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AAK,CAAC,SAAS,CAAC,CAAC,MAAM,CACvD,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAa,CAAC,CAClD,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;QAC7B,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,EAAE,MAAM,CAAC,wBAAwB,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAE,CAAC,CAAC;IACxG,CAAC;IAED,OAAO,KAAY,CAAC;AACtB,CAAC","file":"mixin.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\n/**\n * Type helpers for Mixin\n */\ntype Constructor<T extends object = object, U extends any[] = any[]> = new (...args: U) => T;\ntype ConstructorParams<T> = T extends new (...args: infer U) => any ? U : never;\ntype InstanceType<T> = T extends new (...args: any[]) => infer U ? U : never;\ntype MergeInstances<T, U> = { [P in keyof T]: P extends keyof U ? U[P] : T[P] } & U;\n\nconst PROPS_TO_FILTER = ['constructor'];\n\n/**\n * #### Mixin\n *\n * Creates a class that combines two classes using the mixin pattern.\n * The resulting class extends the first class and includes all properties and methods from the second class.\n *\n * * * *\n * Example:\n * ```typescript\n * import { mixin } from \"@telperion/js-utils/class\";\n *\n * class Timestamped {\n * timestamp = Date.now();\n * getAge() {\n * return Date.now() - this.timestamp;\n * }\n * }\n *\n * class User {\n * constructor(public name: string) {}\n * greet() {\n * return `Hello, ${this.name}`;\n * }\n * }\n *\n * class TimestampedUser extends mixin(User, Timestamped) {}\n *\n * const user = new TimestampedUser(['John'], []);\n * console.log(user.greet()); // \"Hello, John\"\n * console.log(user.getAge()); // Time since creation\n * ```\n *\n * @param Base The base class to extend\n * @param Mixin The mixin class to combine\n * @returns A new class that combines both classes\n */\nexport function mixin<T extends Constructor<any>, U extends Constructor<any>>(\n Base: T,\n Mixin: U\n): Constructor<\n MergeInstances<InstanceType<T>, InstanceType<U>>,\n [ConstructorParams<T>, ConstructorParams<U>]\n> {\n // @ts-ignore - TypeScript doesn't fully understand the mixin pattern\n class Mixed extends Base implements InstanceType<U> {\n constructor(baseArgs: ConstructorParams<T>, mixinArgs: ConstructorParams<U>) {\n super(...baseArgs);\n\n // Create an instance of the mixin class and copy its properties\n const mixinInstance = new Mixin(...mixinArgs);\n\n for (const key of Reflect.ownKeys(mixinInstance)) {\n (this as any)[key] = (mixinInstance as any)[key];\n }\n }\n }\n\n // Copy prototype methods from mixin to the mixed class\n const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter(\n (key) => !PROPS_TO_FILTER.includes(key as string)\n );\n\n for (const prop of mixinKeys) {\n Object.defineProperty(Mixed.prototype, prop, Object.getOwnPropertyDescriptor(Mixin.prototype, prop)!);\n }\n\n return Mixed as any;\n}\n"]}
|