Skip to content

Commit 53340be

Browse files
Updated the README.md and the TSDoc.
1 parent ad59825 commit 53340be

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22

33
This library can be used to achieve multiple inheritance in JavaScript, but it sets itself apart from other libraries for multiple inheritance because it isolates the base classes and prevents the properties and methods from colliding, while still allowing easy access to the properties and methods of the base classes.
44

5+
## Installation
6+
7+
Simply run:
8+
9+
```
10+
npm add extend-bases
11+
```
12+
13+
Or:
14+
15+
```
16+
yarn add extend-bases
17+
```
18+
19+
To import:
20+
21+
```typescript
22+
import {bases, defineProperties, isInstanceOf} from "extend-bases";
23+
```
24+
25+
Or:
26+
27+
```typescript
28+
const {bases, defineProperties, isInstanceOf} = require("extend-bases");
29+
```
30+
31+
## Getting started
32+
33+
To inherit from multiple bases simply extend from `bases(Class1, Class2, ...)`. In the constructor pass the <b>*instances*</b> of the classes to the `super` constructor.
34+
35+
Then you can access the base properties and methods directly from `this.`, the first class with that property or method will be given precedence.
36+
37+
To access a particular base use `this.bases[index]`.
38+
39+
Replace all instances of `v instanceof Class` with `isInstanceOf(v, Class)`, it’s just that simple.
40+
41+
## For more details
42+
43+
- Read the [Handy manual](https://github.com/aryan-programmer/extend-bases/wiki/Handy-Manual), it’s very simple & small and gives enough information to get started.
44+
- Then read the [Documentation](https://github.com/aryan-programmer/extend-bases/wiki/Documentation). it’s not too big and easy to go through.
45+
- Then, if you have enough time or are interested, read the tests and source code of the project.
46+
547
## Implementation details
648

749
This library uses Proxies, so sadly no IE11 support, but otherwise this library also imports es-shims for `Reflect.ownKeys`(ES6 but used very often and an es-shim exists for it) and `Array.prototype.flatMap`(ES2019, very new), which were found to be the bottlenecks, for some browsers.
@@ -11,3 +53,7 @@ This library uses proxies for easy access in the class instances, and it merges
1153
## TypeScript support
1254

1355
The library is by default written in TypeScript, which you all should be using if you aren’t, for great code completion. The details of the TypeScript definitions are a bit fiddly, so it’s best not to worry about them, they work<small>, or you can just look at the source code</small>.
56+
57+
## Contributing
58+
59+
Feel free to open a pull request, fix bugs, or read the source code. It’s all up to you.

index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type UnionToIntersection<U> =
3333
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
3434

3535
/**
36-
* The type of an array containing the instances of specified constructors.
36+
* Specifies that the array contains all the instances of specified constructors in order.
3737
*/
3838
type InstancesArray<Ts extends Ctor[]> = {
3939
[I in keyof Ts]: Ts[I] extends Ctor ? InstanceType<Ts[I]> : never;
@@ -47,7 +47,7 @@ type HasBasesArray<TBases extends Ctor[]> = {
4747
}
4848

4949
/**
50-
* Specifies that the object is an intersection of all of the bases.
50+
* Specifies that the object is an intersection of all of the bases, and has an array containing all of the base instances.
5151
*/
5252
type HasBases<TBases extends Ctor[]> =
5353
UnionToIntersection<AnyInstancesOf<TBases>> &
@@ -108,6 +108,7 @@ function setPrototypeToProxy<TBases extends Ctor[]> (
108108
* But, this library isolates the derived and base classes, ie prevents collision of their properties and methods.
109109
* Thus, this problem can be avoided by using the <code>defineProperties</code> method from this library, if you use the <code>bases</code> methods as well.
110110
* @param baseClasses The base classes to be inherited.
111+
* @return A constructor taking in the *instances* of the base classes.
111112
*
112113
* @example
113114
* class Activatable {
@@ -263,7 +264,7 @@ function bases<TBases extends Ctor[]> (...baseClasses: TBases):
263264

264265
/**
265266
* Defines the properties on the given object, the key represents the name of the property and the value as the, well, value.
266-
* Moreover, if the property name if prefixed with "readonly " then the property will be set to be readonly, ie non-writable.
267+
* Moreover, if the property name if prefixed with `readonly` then the property will be set to be readonly, ie non-writable, ie any attempts to edit it in strict mode will fail with a `TypeError`.
267268
*
268269
* Use this function to set the properties of the objects inheriting from multiple base classes.
269270
*
@@ -277,7 +278,7 @@ function bases<TBases extends Ctor[]> (...baseClasses: TBases):
277278
* "readonly <>": <value>, // Define a readonly property on `this` with the name <prop> and value <value>, readonly ie any attempts to edit it in strict mode will fail with a TypeError.
278279
* });
279280
*/
280-
function defineProperties<T extends object> (v: T, props: { [key: string]: any, }) {
281+
function defineProperties<T extends object> (v: T, props: { [key: string]: any }) {
281282
for (const prop of Reflect.ownKeys(props)) {
282283
if (typeof prop !== "string") {
283284
continue;
@@ -304,6 +305,7 @@ function defineProperties<T extends object> (v: T, props: { [key: string]: any,
304305
*
305306
* @param v The object to check.
306307
* @param cls The constructor of the class to check.
308+
* @return Whether or not the object `v` is an instance of the given class `cls`.
307309
*/
308310
function isInstanceOf<T extends object, TBase extends Ctor> (v: T, cls: TBase): boolean {
309311
if (v instanceof cls) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "extend-bases",
33
"version": "1.0.0",
44
"description": "A simple library for multiple inheritance in JavaScript.",
5-
"repository": "a",
5+
"repository": "https://github.com/aryan-programmer/extend-bases",
66
"author": "[email protected]",
77
"license": "MIT",
88
"private": false,

0 commit comments

Comments
 (0)