Last active
April 25, 2024 13:20
-
-
Save WebReflection/f1666535e732ae254c8666b783cbcefb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// now an npm package qith 100% code coverage: | |
// https://github.com/WebReflection/endow#endow--- | |
// (c) Andrea Giammarchi, @WebReflection (ISC) | |
const mix = Super => ({ | |
with: (...mixins) => | |
mixins.reduce( | |
(Class, Mixin) => { | |
Class = Mixin(Class); | |
if (!Mixin.hasOwnProperty(Symbol.hasInstance)) { | |
Object.defineProperties(Mixin, { | |
// if only WeakSet were iterable ... | |
__class__: {value: []}, | |
[Symbol.hasInstance]: { | |
configurable: true, | |
value(instance) { | |
return this.__class__.some(Class => instance instanceof Class); | |
} | |
} | |
}); | |
} | |
(Mixin.__class__ || []).push(Class); | |
return Class; | |
}, | |
Super | |
) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm package
This is now a proper package you can install and use as needed.
Test/Coverage
Example