JavaScript library for mixins
npm install @teplovs/mixins
# or, if you prefer yarn:
yarn add @teplovs/mixins
import { createMixin } from "@teplovs/mixins"
class Animal {}
const canFly = createMixin(ParentClass =>
class CanFly extends ParentClass {
canFly = true
}
)
const canRun = createMixin(ParentClass =>
class CanRun extends ParentClass {
canRun = true
}
)
const canGreet = createMixin((ParentClass, sound) =>
class CanGreet extends ParentClass {
greet(name) {
return `${sound} ${name}!`
}
}
)
class Dog extends canRun(canGreet(Animal, "Woof woof")) {
// ...
}
class Cat extends canRun(canGreet(Animal, "Meow")) {
// ...
}
class Parrot extends canFly(canGreet(Animal, "Chirp chirp. Hi")) {
// ...
}
new Dog().greet("y'all") // "Woof woof y'all!"
new Cat().greet("y'all") // "Meow y'all!"
new Parrot().greet("y'all") // "Chirp chirp. Hi y'all!"
new Dog().canRun // true
new Cat().canRun // true
new Parrot().canFly // true
Function to create a new mixin.
class Animal {}
const canRun = createMixin(ParentClass =>
class CanRun extends ParentClass {
canRun = true
}
)
const AnimalThatCanRun = canRun(Animal)
new AnimalThatCanRun().canRun // true
class Person {}
const hasHobby = createMixin((ParentClass, personsHobby) =>
class HasHobby extends ParentClass {
hobby = personsHobby
}
)
const Artist = hasHobby(Person, "drawing")
new Artist().hobby // "drawing"
- Node.js and npm
- Clone the repository
git clone https://github.com/teplovs/mixins
- Navigate to the project folder
cd mixins
- Install dependencies
npm install
# or, if you prefer yarn:
yarn install
- To run tests:
npm test
# or:
yarn test
- To build:
npm run build
# or:
yarn build
- Happy hacking! 🎉