npm package: @jamilservices/custom-class-type-and-definition
Description: create custom classes with methods to identify their constructor as a type, and with native methods to obtain json, object and immutability. In addition to having type validation for instance creation.
Author: Jamil Services
Latest version: 0.1.3

### @jamilservices/custom-class-type-and-definition
#### create custom classes with methods to identify their constructor as a type, and with native methods to obtain json, object and immutability. In addition to having type validation for instance creation.
#
## All descriptions in `commits` were automatically generated by chatGPT
#
[![OS - Linux](https://img.shields.io/badge/OS-Linux-blue?logo=linux&logoColor=white)](https://www.linux.org/)
[![Made with JavaScript](https://img.shields.io/badge/Made_with-JavaScript-blue?logo=javascript&logoColor=white)](https://www.javascript.com/)
[![Made with Node.js](https://img.shields.io/badge/Node.js->=18.19-blue?logo=node.js&logoColor=white)](https://nodejs.org)
[![yarn - @jamilservices/custom-class-type-and-definition](https://img.shields.io/badge/yarn-%40jamilservices%2Fcustom--class--type--and--definition-blue?logo=yarn&logoColor=white)](https://yarnpkg.com/package/@jamilservices/custom-class-type-and-definition)

[![jamilservicos - jamilservices-custom-class-type-and-definition](https://img.shields.io/static/v1?label=jamilservicos&message=custom-class-type-and-definition&color=blue&logo=github)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition)
[![stars - jamilservices-custom-class-type-and-definition](https://img.shields.io/github/stars/jamilservicos/jamilservices-custom-class-type-and-definition?style=social)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition)
[![forks - jamilservices-custom-class-type-and-definition](https://img.shields.io/github/forks/jamilservicos/jamilservices-custom-class-type-and-definition?style=social)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition)
[![issues - jamilservices-custom-class-type-and-definition](https://img.shields.io/github/issues/jamilservicos/jamilservices-custom-class-type-and-definition)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition/issues)
[![GitHub release](https://img.shields.io/github/release/jamilservicos/jamilservices-custom-class-type-and-definition?include_prereleases=&sort=semver)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition/releases/)
[![Publish package to GitHub Packages](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition/actions/workflows/github-publish.yml/badge.svg?event=registry_package)](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition/actions/workflows/github-publish.yml)

#
#### Installation ways:

- from github:
```bash
npm install --save git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.git
```
```bash
yarn add git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.git
```
#
- from npm:
```bash
npm install --save @jamilservices/custom-class-type-and-definition
```
```bash
yarn add @jamilservices/custom-class-type-and-definition
```
#
### Configure your class through an object:
```js
const UserTestModelSettings = {
    immutable: false, // When activated, you will not be able to change the instance while it exists. irreversible
    interface: {
        fields: {
            first_name: {
                type: "string",
                required: true
            },
            last_name: {
                type: "string",
                required: true
            },
            password: {
                type: "string",
                required: true
            },
            email: {
                type: "string",
                required: true
            },
            city: "string",
            state: "string",
            address: "string"
        }
    }
};
```
## Features
- Make the instance immutable against any modification.
- Create a DTO with primitive field types and marking the mandatory ones.
- Validate that the instance is of the desired type with custom `.typeOf("className")`.
- Register your `class` as a definition to be used by instanceof in any part of the application just by importing `CustomTypeInterfaceDefinition`
- Extract the `object` just using `.toObject()`.
- Extract the `json` just using `.toJson()`.
#
### how to start
1 - import/require `{CustomTypeInterface, registerDefinition}`  
2 - class `classNameExample extends CustomTypeInterface`  
3 - create your settings object  
4 - create your `constructor` to import the settings, and create the instance  
5 - add field variables to `class`  

## Example
```js
class UserTestModel extends CustomTypeInterface {
    first_name;
    last_name;
    password;
    email;
    city;
    state;
    address;
    constructor(data) {
        super();
        this.interface = UserTestModelSettings.interface.fields;
        if (data) this.populate(data);
        if(UserTestModelSettings.immutable) this.immutable(this);
    }
}
```
#
If you want to add the definition to your `class`, just follow the example below:  
```js
registerDefinition(UserTestModel);
```
#
To import the definition into other files, simply import the class name from `CustomTypeInterfaceDefinition`.
###
### Usage Tests:
```js
const user = new UserTestModel({
    first_name: "User Test First Name",
    last_name: "User Test Last Name",
    password: "UserPassword",
    email: "usertest@test.local"
});

console.log("instance", user);
console.log("instance.toJson()", user.toJson());
console.log("instance.toObject()", user.toObject());
console.log("instance.typeOf('UserTestModel')", user.typeOf('UserTestModel'));
console.log("user instanceof UserTestModel", user instanceof UserTestModel);
console.log("user.customType === 'UserTestModel'", user.customType === 'UserTestModel');
console.log("user.instanceOf === user.customType", user.instanceOf === user.customType);
console.log("user.interface", user.interface);
```  
```js
 console.log("user instanceof UserTestModel from userInstanceDefinition", user instanceof UserTestModel);
```  
#
- [See the example complete codes in the `examples` directory](https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition/blob/main/examples)
#
### License
Released under [MIT](/LICENSE) by [@jamilservicos](https://github.com/jamilservicos).
* You can freely modify and reuse.
* The original license must be included with copies of this software.
* Please link back to this repo if you use a significant portion the source code.
#
### 👩‍💻💻 Technologies
![JavaScript](https://img.shields.io/badge/-JavaScript-F7DF1E?style=for-the-badge&logo=javascript&logoColor=black)
![Nodejs](https://img.shields.io/badge/-Nodejs-339933?style=for-the-badge&logo=node-dot-js&logoColor=white)