Skip to content

Commit 6a68b75

Browse files
authored
Merge pull request #1 from udibo/dev
Fix applyClassMixins overwritting name and constructor
2 parents 4ffccac + fcde42b commit 6a68b75

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Mixins
22

3-
[![version](https://img.shields.io/badge/release-0.7.4-success)](https://github.com/udibo/mixins/tree/0.7.4)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mixins@0.7.4/mod.ts)
3+
[![version](https://img.shields.io/badge/release-0.8.0-success)](https://github.com/udibo/mixins/tree/0.8.0)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/mixins@0.8.0/mod.ts)
55
[![CI](https://github.com/udibo/mixins/workflows/CI/badge.svg)](https://github.com/udibo/mixins/actions?query=workflow%3ACI)
66
[![codecov](https://codecov.io/gh/udibo/mixins/branch/master/graph/badge.svg?token=LIK8G3SMOC)](https://codecov.io/gh/udibo/mixins)
77
[![license](https://img.shields.io/github/license/udibo/mixins)](https://github.com/udibo/mixins/blob/master/LICENSE)
@@ -28,9 +28,9 @@ imported directly from GitHub using raw content URLs.
2828

2929
```ts
3030
// Import from Deno's third party module registry
31-
import { applyMixins } from "https://deno.land/x/mixins@0.7.4/mod.ts";
31+
import { applyMixins } from "https://deno.land/x/mixins@0.8.0/mod.ts";
3232
// Import from GitHub
33-
import { applyMixins } "https://raw.githubusercontent.com/udibo/mixins/0.7.4/mod.ts";
33+
import { applyMixins } "https://raw.githubusercontent.com/udibo/mixins/0.8.0/mod.ts";
3434
```
3535
3636
### Node.js
@@ -41,15 +41,15 @@ If a Node.js package has the type "module" specified in its package.json file,
4141
the JavaScript bundle can be imported as a `.js` file.
4242
4343
```js
44-
import { applyMixins } from "./mixins_0.7.4.js";
44+
import { applyMixins } from "./mixins_0.8.0.js";
4545
```
4646

4747
The default type for Node.js packages is "commonjs". To import the bundle into a
4848
commonjs package, the file extension of the JavaScript bundle must be changed
4949
from `.js` to `.mjs`.
5050

5151
```js
52-
import { applyMixins } from "./mixins_0.7.4.mjs";
52+
import { applyMixins } from "./mixins_0.8.0.mjs";
5353
```
5454

5555
See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
@@ -68,15 +68,15 @@ modules must have the type attribute set to "module".
6868

6969
```js
7070
// main.js
71-
import { applyMixins } from "./mixins_0.7.4.js";
71+
import { applyMixins } from "./mixins_0.8.0.js";
7272
```
7373

7474
You can also embed a module script directly into an HTML file by placing the
7575
JavaScript code within the body of the script tag.
7676

7777
```html
7878
<script type="module">
79-
import { applyMixins } from "./mixins_0.7.4.js";
79+
import { applyMixins } from "./mixins_0.8.0.js";
8080
</script>
8181
```
8282

@@ -96,7 +96,7 @@ Applies properties of mixins to instance.
9696
Using `applyMixins` to add properties to an object:
9797

9898
```ts
99-
import { applyMixins } from "https://deno.land/x/mixins@0.7.4/mod.ts";
99+
import { applyMixins } from "https://deno.land/x/mixins@0.8.0/mod.ts";
100100
interface Point {
101101
x: number;
102102
y: number;
@@ -118,7 +118,7 @@ point3; // { time: 5, x: 2, y: 3, z: 7 }
118118
Using `applyMixins` to add properties to a function:
119119

120120
```ts
121-
import { applyMixins } from "https://deno.land/x/mixins@0.7.4/mod.ts";
121+
import { applyMixins } from "https://deno.land/x/mixins@0.8.0/mod.ts";
122122
interface Point {
123123
x: number;
124124
y: number;
@@ -158,7 +158,7 @@ point3.toString(); // "2, 3, 7, 5"
158158
Using `applyMixins` to add properties to a class:
159159

160160
```ts
161-
import { applyMixins } from "https://deno.land/x/mixins@0.7.4/mod.ts";
161+
import { applyMixins } from "https://deno.land/x/mixins@0.8.0/mod.ts";
162162
interface Point {
163163
x: number;
164164
y: number;
@@ -215,7 +215,7 @@ Applies properties of base class prototypes to instance.
215215
import {
216216
applyInstanceMixins,
217217
applyMixins,
218-
} from "https://deno.land/x/mixins@0.7.4/mod.ts";
218+
} from "https://deno.land/x/mixins@0.8.0/mod.ts";
219219
class Point {
220220
constructor(public x: number, public y: number) {}
221221

@@ -265,7 +265,7 @@ Applies properties of base class prototypes to class prototype.
265265
import {
266266
applyClassMixins,
267267
applyMixins,
268-
} from "https://deno.land/x/mixins@0.7.4/mod.ts";
268+
} from "https://deno.land/x/mixins@0.8.0/mod.ts";
269269
class Point {
270270
constructor(public x: number, public y: number) {}
271271

apply.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
export function applyMixins<T>(instance: T, mixins: any[]) {
66
mixins.forEach((mixin) => {
77
Object.getOwnPropertyNames(mixin).forEach((name) => {
8-
if (name === "prototype") return;
8+
// if (name === "prototype") return;
9+
if (
10+
name === "prototype" || name === "name" || name === "constructor" ||
11+
name === "length"
12+
) return;
913
Object.defineProperty(
1014
instance,
1115
name,

apply_test.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { assertEquals } from "./test_deps.ts";
1+
import {
2+
assertEquals,
3+
assertObjectMatch,
4+
assertStrictEquals,
5+
} from "https://deno.land/[email protected]/assert/mod.ts";
26
import { applyClassMixins, applyInstanceMixins, applyMixins } from "./mod.ts";
37

48
Deno.test("applyMixins to object", () => {
@@ -181,7 +185,6 @@ Deno.test("applyInstanceMixins", () => {
181185
assertEquals(
182186
Object.getOwnPropertyNames(point).sort(),
183187
[
184-
"constructor",
185188
"getPosition",
186189
"getTime",
187190
"length",
@@ -198,6 +201,8 @@ Deno.test("applyInstanceMixins", () => {
198201

199202
Deno.test("applyClassMixins", () => {
200203
class Point {
204+
static x = 2;
205+
201206
constructor(public x: number, public y: number) {}
202207

203208
static defaultPosition(): [number, number] {
@@ -209,6 +214,7 @@ Deno.test("applyClassMixins", () => {
209214
}
210215
}
211216
class TimePoint {
217+
static time = 5;
212218
constructor(public time: number) {}
213219

214220
getTime(): number {
@@ -222,10 +228,10 @@ Deno.test("applyClassMixins", () => {
222228
toString(): string;
223229
}
224230
class Point4D {
225-
static x: number;
226-
static y: number;
227-
static z: number;
228-
static time: number;
231+
declare static x: number;
232+
declare static y: number;
233+
declare static z: number;
234+
declare static time: number;
229235

230236
constructor(
231237
public x: number,
@@ -248,7 +254,7 @@ Deno.test("applyClassMixins", () => {
248254
}
249255
}
250256
applyClassMixins(Point4D, [Point, TimePoint, Point4DPartial]);
251-
applyMixins(Point4D, [{ time: 5, x: 2, y: 3, z: 7 }]);
257+
applyMixins(Point4D, [{ y: 3, z: 7 }]);
252258

253259
assertEquals(Point4D.example(), "2, 3, 7, 5");
254260
assertEquals(
@@ -269,12 +275,21 @@ Deno.test("applyClassMixins", () => {
269275
Object.getOwnPropertyNames(Point4D.prototype).sort(),
270276
["constructor", "getPosition", "getTime", "toArray", "toString"],
271277
);
278+
assertEquals(Point4D.name, "Point4D");
279+
assertStrictEquals(Point4D.prototype.constructor, Point4D);
280+
assertObjectMatch(Point4D, {
281+
x: 2,
282+
y: 3,
283+
z: 7,
284+
time: 5,
285+
});
272286

273287
applyMixins(Point4D, [{ x: 10, y: 16 }, { y: 18, z: 22 }]);
274288
assertEquals(Point4D.example(), "10, 18, 22, 5");
275289

276290
const point: Point4D = new Point4D(1, 2, 3, 4);
277291

292+
assertStrictEquals(point.constructor, Point4D);
278293
assertEquals(point.toArray(), [1, 2, 3, 4]);
279294
assertEquals(point.toString(), "1, 2, 3, 4");
280295
assertEquals(point.getPosition(), "1, 2");

test_deps.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)