# autoclass

> Define argument types for functions and automatically add methods to prototype.

Latest version **0.4.3** (published 2017-02-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install autoclass
pnpm add autoclass
yarn add autoclass
bun add autoclass
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.3 |
| Published | 2017-02-04 |
| First published | 2015-06-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Arno Saine |
| Maintainers | arnosaine |
| Keywords | oop, prototype |

## Links

- npm: https://www.npmjs.com/package/autoclass
- Repository: https://github.com/ArnoSaine/AutoClass
- Homepage: https://github.com/ArnoSaine/AutoClass#readme
- Issues: https://github.com/ArnoSaine/AutoClass/issues
- npm.io page: https://npm.io/package/autoclass

## Dependencies (3)

- [ramda](https://npm.io/package/ramda.md) ^0.21.0
- [is-plain-object](https://npm.io/package/is-plain-object.md) ^2.0.1
- [get-parameter-names](https://npm.io/package/get-parameter-names.md) ^0.3.0

## Recent versions

- 0.4.3 (latest) — 2017-02-04
- 0.4.2 — 2016-07-29
- 0.4.1 — 2016-07-11
- 0.4.0 — 2015-10-11
- 0.3.0 — 2015-10-10
- 0.2.2 — 2015-10-04
- 0.2.1 — 2015-10-02
- 0.1.0 — 2015-06-14
- 0.0.0 — 2015-06-06

## README

# AutoClass

Define argument types for functions and automatically add methods to prototype.

## Installation

```sh
npm install autoclass
```

## Example

```js
import AutoClass from 'autoclass';

const Rider = AutoClass(
    'Rider',
    String,
    Number,
    function (nickname, number) {
        return {nickname, number};
    }
);

const ShowInfo = AutoClass(
    'ShowInfo',
    Rider,
    function ({nickname, number}) {
        console.log(`${nickname}, #${number}`);
    }
);

const dovizioso = Rider('Dovi', 4);
const iannone = Rider('The Maniac', 29);

dovizioso.ShowInfo(); // Logs: "Dovi, #4"
iannone.ShowInfo(); // Logs: "The Maniac, #29"
```

### AutoClass(name[, ...type], func)

##### name: **String** or **Any**

A string to use as a method name. If an empty string or not a string, no methods are added.

##### type: **Constructor** or [Type](#type)

Zero or more constructors. `func` arguments are validated using these corresponding types. Methods are added to prototypes of constructors. Methods are not added to array or variadic types.

##### func: **Function**

Function to execute, when [instance](#values-and-instances) as function or instance's method is invoked. `func` should have equal amount of named arguments as types are declared. `func` gets called with formatted and validated (see [type](#type)) argument [values](#values-and-instances). Its `this` value is set to object with argument names of `func` as keys and argument instances as values.

##### Returns: [Instance](#values-and-instances)

```js
const MyClass = AutoClass(
    'MyClass',
    String,
    function (text) {
        return text;
    }
);
```

### Type

Three kinds of types can be declared - basic, array and variadic.

```js
const MyClass = AutoClass(
    'MyClass',
    String, // Basic types. Validated using `instanceof` operator.
    Object,
    [Number], // Array types. Each element is validated like basic type.
    [[String]], // Variadic type. Function may also have one variadic type. Validated like array type.
    Boolean, // More types...
    function (someText, obj, nums, texts, bool) {
        console.log('MyClass');
    }
);

MyClass('text', null, [1, 2, 3], 'a', 'b', 'c', true); // Logs: "MyClass"
```

### Type conversions

If `instanceof` test fails, instance is passed to constructor.

```js
const Rider = AutoClass(
    'Rider',
    String,
    Number,
    function (nickname, number) {
        console.log(typeof number); // Logs: "number"
        return {nickname, number};
    }
);

const dovizioso = Rider('Dovi', '04');
```

If class requires at least two types (variadic type is not considered), constructing from object literal is possible.

```js
// `ShowInfo` requires `Rider`
ShowInfo({
    nickname: 'Petrux',
    number: 9
}); // Logs: "Petrux, #9"
```

### Values and instances

Function arguments are **values**. Use `this.argumentName` to access **instance**.

```js
const MyClass = AutoClass('MyClass', Number, function (number) {return number;});

const TestType = AutoClass(
    'TestType',
    MyClass,
    AutoClass,
    function (a, type) {
        console.log(a);
        console.log(this.a instanceof type);
    }
);

TestType(123, MyClass); // Logs: 123; true
```

Function and method calls return **instance**. Use `.valueOf()` to access return **value**.

```js
const MyClass = AutoClass(
    'MyClass',
    Object,
    function (obj) {
        return obj;
    }
);

const ref = {};

console.log(MyClass(ref).valueOf() === ref); // Logs: true
```

### Create classes

Validate inputs, return value.

```js
const BikeNumber = AutoClass(
    'BikeNumber',
    Number,
    function (number) {
        if (isNaN(number)) {
            throw new Error('Bike number must be positive integer, got NaN.');
        }
        if (number < 1) {
            throw new Error(`Bike number must be positive integer, got ${number}.`);
        }
        return Math.floor(number);
    }
);

const Rider = AutoClass(
    'Rider',
    String,
    BikeNumber,
    function (nickname, bikeNumber) {
        return {nickname, bikeNumber};
    }
);

try {
    Rider('Kallio', -36); // Throws: "Error: Bike number must be positive integer, got -36."
} catch (e) {
    console.log(e);
}

try {
    Rider('The Doctor', 'VR46'); // Throws: "Error: Bike number must be positive integer, got NaN."
} catch (e) {
    console.log(e);
}
```

### Method chaining

Functions that return nothing or undefined can be chained.

```js
AutoClass(
    'SetNumber',
    Rider,
    BikeNumber,
    function (rider, bikeNumber) {
        rider.bikeNumber = bikeNumber;
    }
);

AutoClass(
    'ShowInfo',
    Rider,
    function ({nickname, bikeNumber}) {
        console.log(`${nickname}, #${bikeNumber}`);
    }
);

Rider('Stoner', 27)
    .ShowInfo() // Logs: "Stoner, #27"
    .SetNumber(1)
    .ShowInfo(); // Logs: "Stoner, #1"
```

### Multiple methods from same type

Function may require same type multiple times. Primary method uses instance as first argument of its type. To use instance as another argument of its type, use `instance.Method.argumentName(...other args)`.

```js
AutoClass(
    'Append',
    String,
    String,
    function (to, text) {
        return to + text;
    }
);

// "Hello" as `to` argument
console.log('Hello'.Append(' world!').valueOf()); // Logs: "Hello world!"
// "Hello" as `text` argument
console.log('Hello'.Append.text(' world!').valueOf()); // Logs: " world!Hello"
```

## License

ISC

---
_Source: https://npm.io/package/autoclass · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
