https://github.com/ivteplo/mixins
JavaScript library for mixins
https://github.com/ivteplo/mixins
class-composition javascript mixins object-oriented-programming oop typescript
Last synced: about 1 month ago
JSON representation
JavaScript library for mixins
- Host: GitHub
- URL: https://github.com/ivteplo/mixins
- Owner: ivteplo
- License: apache-2.0
- Created: 2022-02-27T11:57:22.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-08T07:39:18.000Z (about 4 years ago)
- Last Synced: 2025-02-01T14:45:28.387Z (over 1 year ago)
- Topics: class-composition, javascript, mixins, object-oriented-programming, oop, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/package/@teplovs/mixins
- Size: 249 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mixins
JavaScript library for mixins
## Installation
```bash
npm install @teplovs/mixins
# or, if you prefer yarn:
yarn add @teplovs/mixins
```
## Usage Example
```javascript
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
```
## API
### `createMixin(createClass: (ParentClass, ...props) => Class): (...props) => Class`
Function to create a new mixin.
#### Mixin without properties
```javascript
class Animal {}
const canRun = createMixin(ParentClass =>
class CanRun extends ParentClass {
canRun = true
}
)
const AnimalThatCanRun = canRun(Animal)
new AnimalThatCanRun().canRun // true
```
#### Mixin with properties
```javascript
class Person {}
const hasHobby = createMixin((ParentClass, personsHobby) =>
class HasHobby extends ParentClass {
hobby = personsHobby
}
)
const Artist = hasHobby(Person, "drawing")
new Artist().hobby // "drawing"
```
## Development
### Requirements
- Node.js and npm
### Setup
1. Clone the repository
```bash
git clone https://github.com/teplovs/mixins
```
2. Navigate to the project folder
```bash
cd mixins
```
3. Install dependencies
```bash
npm install
# or, if you prefer yarn:
yarn install
```
4. To run tests:
```bash
npm test
# or:
yarn test
```
5. To build:
```bash
npm run build
# or:
yarn build
```
6. Happy hacking! 🎉