Skip to content

Commit

Permalink
[accel-record] marge arrays in Mix
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Jan 6, 2025
1 parent e64a9af commit 089e8bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/silly-apples-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"accel-record-core": patch
"accel-record": patch
---

Merge arrays in Mix()
10 changes: 9 additions & 1 deletion packages/accel-record-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ const assign = (target: object, source: object, key: string) => {
const desc = Object.getOwnPropertyDescriptor(source, key);
const targetDesc = findMethod(target, key);

if (typeof targetDesc?.value == "function" && typeof desc?.value == "function") {
if (Array.isArray(targetDesc?.value) && Array.isArray(desc?.value)) {
// merge arrays
Object.defineProperty(target, key, {
value: [...targetDesc.value, ...desc.value],
enumerable: targetDesc.enumerable,
writable: true,
configurable: true,
});
} else if (typeof targetDesc?.value == "function" && typeof desc?.value == "function") {
// already has a method, so we need to wrap it
Object.defineProperty(target, key, {
value: function (this: any, ...args: any[]) {
Expand Down
6 changes: 6 additions & 0 deletions tests/models/mix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Mix } from "accel-record-core";
class A {
static a = "a";

static validations = ["a"];

a = "a";
errors: string[] = [];

Expand All @@ -24,6 +26,8 @@ class AB extends Mix(A, B) {}
class C {
static c = "c";

static validations = ["c"];

c = "c";

validate<T extends A>(this: T) {
Expand All @@ -41,6 +45,8 @@ test("mix", () => {
expect(ABC.a).toBe("a");
expect(ABC.c).toBe("c");

expect(ABC.validations).toEqual(["a", "c"]);

const ab = new AB();
expect(ab.a).toBe("a");
expect(ab.aa).toBe("aa");
Expand Down

0 comments on commit 089e8bb

Please sign in to comment.