Gathering detailed insights and metrics for mixwith
Gathering detailed insights and metrics for mixwith
Gathering detailed insights and metrics for mixwith
Gathering detailed insights and metrics for mixwith
npm install mixwith
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
779 Stars
22 Commits
58 Forks
13 Watching
1 Branches
3 Contributors
Updated on 21 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
6.6%
22,596
Compared to previous day
Last week
2%
113,270
Compared to previous week
Last month
22.9%
471,177
Compared to previous month
Last year
-9.9%
4,897,500
Compared to previous year
A simple and powerful mixin library for ES6.
mixwith
differs from other mixin approaches because it does not copy properties from one object to another. Instead, mixwith
works with "subclass factories" which create a new class that extends a superclass with the mixin - this is called a mixin application.
my-mixin.js:
1let MyMixin = (superclass) => class extends superclass { 2 // mixin methods here 3};
my-class.js
1class MyClass extends MyMixin(MySuperClass) { 2 // class methods here, go ahead, use super! 3}
The subclass factory pattern does not require any support from a library. It's just a natural use of JavaScript class expressions. mixwith.js provides a few helpers that make mixins a little more powerful and easier to use.
mixwith.js makes some use cases very easy:
instanceof
support to a mixin function.mixwith.js also provides a little bit of sugar with the mix()
function that makes applying mixins read a little more naturally:
1class MyClass extends mix(MySuperClass).with(MyMixin, OtherMixin) { 2 // class methods here, go ahead, use super! 3}
Subclass factory style mixins preserve the object-oriented inheritance properties that classes provide, like method overriding and super
calls, while letting you compose classes out of mixins without being constrained to a single inheritance hierarchy, and without monkey-patching or copying.
Methods in subclasses can naturally override methods in the mixin or superclass, and mixins override methods in the superclass. This means that precedence is preserved - the order is: subclass -> mixin__1 -> ... -> mixin__N -> superclass.
super
worksSubclasses and mixins can use super
normally, as defined in standard Javascript, and without needing the mixin library to do special chaining of functions.
Since super()
works, mixins can define constructors. Combined with ES6 rest arguments and the spread operator, mixins can have generic constructors that work with any super constructor by passing along all arguments.
Typical JavaScript mixins usually used to either mutate each instance as created, which can be bad for performance and maintainability, or modify a prototype, which means every object inheriting from that prototype gets the mixin. Subclass factories don't mutate objects, they define new classes to subclass, leaving the original superclass intact.
The Mixin
decorator function wraps a plain subclass factory to add deduplication, caching and instanceof
support:
1let MyMixin = Mixin((superclass) => class extends superclass { 2 3 constructor(args...) { 4 // mixins should either 1) not define a constructor, 2) require a specific 5 // constructor signature, or 3) pass along all arguments. 6 super(...args); 7 } 8 9 foo() { 10 console.log('foo from MyMixin'); 11 // this will call superclass.foo() 12 super.foo(); 13 } 14 15});
Mixins defined with the mixwith.js decorators do not require any helpers to use, they still work like plain subclass factories.
Classes use mixins in their extends
clause. Classes that use mixins can define and override constructors and methods as usual.
1class MyClass extends mix(MySuperClass).with(MyMixin) { 2 3 constructor(a, b) { 4 super(a, b); // calls MyMixin(a, b) 5 } 6 7 foo() { 8 console.log('foo from MyClass'); 9 super.foo(); // calls MyMixin.foo() 10 } 11 12}
function
Applies mixin
to superclass
.
apply
stores a reference from the mixin application to the unwrapped mixin
to make isApplicationOf
and hasMixin
work.
This function is usefull for mixin wrappers that want to automatically enable hasMixin support.
Kind: global function
Returns: function
- A subclass of superclass
produced by mixin
Param | Type | Description |
---|---|---|
superclass | function | A class or constructor function |
mixin | MixinFunction | The mixin to apply |
Example
1const Applier = (mixin) => wrap(mixin, (superclass) => apply(superclass, mixin)); 2 3// M now works with `hasMixin` and `isApplicationOf` 4const M = Applier((superclass) => class extends superclass {}); 5 6class C extends M(Object) {} 7let i = new C(); 8hasMixin(i, M); // true
boolean
Returns true
iff proto
is a prototype created by the application of
mixin
to a superclass.
isApplicationOf
works by checking that proto
has a reference to mixin
as created by apply
.
Kind: global function
Returns: boolean
- whether proto
is a prototype created by the application of
mixin
to a superclass
Param | Type | Description |
---|---|---|
proto | Object | A prototype object created by apply. |
mixin | MixinFunction | A mixin function used with apply. |
boolean
Returns true
iff o
has an application of mixin
on its prototype
chain.
Kind: global function
Returns: boolean
- whether o
has an application of mixin
on its prototype
chain
Param | Type | Description |
---|---|---|
o | Object | An object |
mixin | MixinFunction | A mixin applied with apply |
MixinFunction
Sets up the function mixin
to be wrapped by the function wrapper
, while
allowing properties on mixin
to be available via wrapper
, and allowing
wrapper
to be unwrapped to get to the original function.
wrap
does two things:
mixin
to wrapper
so that properties set on
mixin
inherited by wrapper
.mixin
that points back to mixin
so that
it can be retreived from wrapper
Kind: global function
Returns: MixinFunction
- wrapper
Param | Type | Description |
---|---|---|
mixin | MixinFunction | A mixin function |
wrapper | MixinFunction | A function that wraps mixin |
MixinFunction
Unwraps the function wrapper
to return the original function wrapped by
one or more calls to wrap
. Returns wrapper
if it's not a wrapped
function.
Kind: global function
Returns: MixinFunction
- The originally wrapped mixin
Param | Type | Description |
---|---|---|
wrapper | MixinFunction | A wrapped mixin produced by wrap |
MixinFunction
Decorates mixin
so that it caches its applications. When applied multiple
times to the same superclass, mixin
will only create one subclass, memoize
it and return it for each application.
Note: If mixin
somehow stores properties its classes constructor (static
properties), or on its classes prototype, it will be shared across all
applications of mixin
to a super class. It's reccomended that mixin
only
access instance state.
Kind: global function
Returns: MixinFunction
- a new mixin function
Param | Type | Description |
---|---|---|
mixin | MixinFunction | The mixin to wrap with caching behavior |
MixinFunction
Decorates mixin
so that it only applies if it's not already on the
prototype chain.
Kind: global function
Returns: MixinFunction
- a new mixin function
Param | Type | Description |
---|---|---|
mixin | MixinFunction | The mixin to wrap with deduplication behavior |
MixinFunction
Adds [Symbol.hasInstance] (ES2015 custom instanceof support) to mixin
.
Kind: global function
Returns: MixinFunction
- the given mixin function
Param | Type | Description |
---|---|---|
mixin | MixinFunction | The mixin to add [Symbol.hasInstance] to |
MixinFunction
A basic mixin decorator that applies the mixin with apply so that it can be used with isApplicationOf, hasMixin and the other mixin decorator functions.
Kind: global function
Returns: MixinFunction
- a new mixin function
Param | Type | Description |
---|---|---|
mixin | MixinFunction | The mixin to wrap |
MixinFunction
Decorates a mixin function to add deduplication, application caching and instanceof support.
Kind: global function
Returns: MixinFunction
- a new mixin function
Param | Type | Description |
---|---|---|
mixin | MixinFunction | The mixin to wrap |
MixinBuilder
A fluent interface to apply a list of mixins to a superclass.
1class X extends mix(Object).with(A, B, C) {}
The mixins are applied in order to the superclass, so the prototype chain will be: X->C'->B'->A'->Object.
This is purely a convenience function. The above example is equivalent to:
1class X extends C(B(A(Object))) {}
Kind: global function
Param | Type | Default |
---|---|---|
[superclass] | function | Object |
function
A function that returns a subclass of its argument.
Kind: global typedef
Returns: function
- A subclass of superclass
Param | Type |
---|---|
superclass | function |
Example
1const M = (superclass) => class extends superclass { 2 getMessage() { 3 return "Hello"; 4 } 5}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/21 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More