Skip to content

Commit dc93f66

Browse files
committed
Make applyClassMixins apply static methods
1 parent dccd6a7 commit dc93f66

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Install deno
2323
uses: denolib/setup-deno@master
2424
with:
25-
deno-version: 1.4.0
25+
deno-version: 1.4.2
2626
- name: Check formatting
2727
if: matrix.config.kind == 'lint'
2828
run: |

README.md

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

3-
[![version](https://img.shields.io/badge/release-v0.5.0-success)](https://github.com/udibo/mixins/tree/v0.5.0)
4-
[![deno doc](https://img.shields.io/badge/deno-doc-success?logo=deno)](https://doc.deno.land/https/deno.land/x/mixins@v0.5.0/mod.ts)
5-
[![deno version](https://img.shields.io/badge/deno-v1.4.0-success?logo=deno)](https://github.com/denoland/deno/tree/v1.4.0)
3+
[![version](https://img.shields.io/badge/release-v0.6.0-success)](https://github.com/udibo/mixins/tree/v0.6.0)
4+
[![deno doc](https://img.shields.io/badge/deno-doc-success?logo=deno)](https://doc.deno.land/https/deno.land/x/mixins@v0.6.0/mod.ts)
5+
[![deno version](https://img.shields.io/badge/deno-v1.4.2-success?logo=deno)](https://github.com/denoland/deno/tree/v1.4.2)
66
[![CI](https://github.com/udibo/mixins/workflows/CI/badge.svg)](https://github.com/udibo/mixins/actions?query=workflow%3ACI)
77
[![license](https://img.shields.io/github/license/udibo/mixins)](https://github.com/udibo/mixins/blob/master/LICENSE)
88

@@ -24,9 +24,9 @@ but can also be imported directly from GitHub using raw content URLs.
2424

2525
```ts
2626
// Import from Deno's third party module registry
27-
import { applyMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
27+
import { applyMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
2828
// Import from GitHub
29-
import { applyMixins } "https://raw.githubusercontent.com/udibo/mixins/v0.5.0/mod.ts";
29+
import { applyMixins } "https://raw.githubusercontent.com/udibo/mixins/v0.6.0/mod.ts";
3030
```
3131
3232
### Node.js
@@ -36,14 +36,14 @@ Node.js fully supports ES Modules.
3636
If a Node.js package has the type "module" specified in its package.json file, the JavaScript bundle can be imported as a `.js` file.
3737
3838
```js
39-
import { applyMixins } from "./mixins_v0.5.0.js";
39+
import { applyMixins } from "./mixins_v0.6.0.js";
4040
```
4141

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

4545
```js
46-
import { applyMixins } from "./mixins_v0.5.0.mjs";
46+
import { applyMixins } from "./mixins_v0.6.0.mjs";
4747
```
4848

4949
See [Node.js Documentation](https://nodejs.org/api/esm.html) for more information.
@@ -61,15 +61,15 @@ Script tags for ES modules must have the type attribute set to "module".
6161

6262
```js
6363
// main.js
64-
import { applyMixins } from "./mixins_v0.5.0.js";
64+
import { applyMixins } from "./mixins_v0.6.0.js";
6565
```
6666

6767
You can also embed a module script directly into an HTML file by placing the JavaScript code
6868
within the body of the script tag.
6969

7070
```html
7171
<script type="module">
72-
import { applyMixins } from "./mixins_v0.5.0.js";
72+
import { applyMixins } from "./mixins_v0.6.0.js";
7373
</script>
7474
```
7575

@@ -86,7 +86,7 @@ Applies properties of mixins to instance.
8686
Using `applyMixins` to add properties to an object:
8787

8888
```ts
89-
import { applyMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
89+
import { applyMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
9090
interface Point {
9191
x: number;
9292
y: number;
@@ -108,7 +108,7 @@ point3; // { time: 5, x: 2, y: 3, z: 7 }
108108
Using `applyMixins` to add properties to a function:
109109

110110
```ts
111-
import { applyMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
111+
import { applyMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
112112
interface Point {
113113
x: number;
114114
y: number;
@@ -145,7 +145,7 @@ point3.toString(); // "2, 3, 7, 5"
145145
Using `applyMixins` to add properties to a class:
146146

147147
```ts
148-
import { applyMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
148+
import { applyMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
149149
interface Point {
150150
x: number;
151151
y: number;
@@ -197,7 +197,7 @@ point3.toString(); // "1, 2, 3, 4"
197197
Applies properties of base class prototypes to instance.
198198

199199
```ts
200-
import { applyMixins, applyInstanceMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
200+
import { applyMixins, applyInstanceMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
201201
class Point {
202202
constructor(public x: number, public y: number) {}
203203

@@ -244,7 +244,7 @@ point.toString(); // "2, 3, 7, 5"
244244
Applies properties of base class prototypes to class prototype.
245245

246246
```ts
247-
import { applyMixins, applyClassMixins } from "https://deno.land/x/mixins@v0.5.0/mod.ts";
247+
import { applyMixins, applyClassMixins } from "https://deno.land/x/mixins@v0.6.0/mod.ts";
248248
class Point {
249249
constructor(public x: number, public y: number) {}
250250

apply.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
export function applyMixins<T>(instance: T, mixins: any[]) {
66
mixins.forEach((mixin) => {
77
Object.getOwnPropertyNames(mixin).forEach((name) => {
8+
if (name === "prototype") return;
89
Object.defineProperty(
910
instance,
1011
name,
@@ -22,16 +23,17 @@ export function applyMixins<T>(instance: T, mixins: any[]) {
2223
export function applyInstanceMixins<T>(instance: T, baseCtors: any[]) {
2324
applyMixins(
2425
instance,
25-
baseCtors.map((baseCtor) => baseCtor.prototype ?? baseCtor),
26+
baseCtors.map((baseCtor) => baseCtor.prototype),
2627
);
2728
}
2829

29-
/** Applies properties of base class prototypes to class prototype. */
30+
/** Applies properties of base classes to class. */
3031
export function applyClassMixins<T>(
3132
// deno-lint-ignore no-explicit-any
3233
ctor: { new (...args: any[]): T },
3334
// deno-lint-ignore no-explicit-any
3435
baseCtors: any[],
3536
) {
37+
applyMixins(ctor, baseCtors);
3638
applyInstanceMixins(ctor.prototype, baseCtors);
3739
}

apply_test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ Deno.test("applyClassMixins", () => {
202202
class Point {
203203
constructor(public x: number, public y: number) {}
204204

205+
static defaultPosition(): [number, number] {
206+
return [0, 0];
207+
}
208+
205209
getPosition(): string {
206210
return [this.x, this.y].join(", ");
207211
}
@@ -251,7 +255,17 @@ Deno.test("applyClassMixins", () => {
251255
assertEquals(Point4D.example(), "2, 3, 7, 5");
252256
assertEquals(
253257
Object.getOwnPropertyNames(Point4D).sort(),
254-
["example", "length", "name", "prototype", "time", "x", "y", "z"],
258+
[
259+
"defaultPosition",
260+
"example",
261+
"length",
262+
"name",
263+
"prototype",
264+
"time",
265+
"x",
266+
"y",
267+
"z",
268+
],
255269
);
256270
assertEquals(
257271
Object.getOwnPropertyNames(Point4D.prototype).sort(),

deps/std/testing/asserts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { assertEquals } from "https://deno.land/std@0.69.0/testing/asserts.ts";
1+
export { assertEquals } from "https://deno.land/std@0.71.0/testing/asserts.ts";

0 commit comments

Comments
 (0)