Skip to content

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.

License

Notifications You must be signed in to change notification settings

jamilservices/custom-class-type-and-definition

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@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 Made with JavaScript Made with Node.js yarn - @jamilservices/custom-class-type-and-definition

jamilservicos - jamilservices-custom-class-type-and-definition stars - jamilservices-custom-class-type-and-definition forks - jamilservices-custom-class-type-and-definition issues - jamilservices-custom-class-type-and-definition GitHub release Publish package to GitHub Packages

Installation ways:

  • from github:
npm install --save git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.git
yarn add git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.git

  • from npm:
npm install --save @jamilservices/custom-class-type-and-definition
yarn add @jamilservices/custom-class-type-and-definition

Configure your class through an object:

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

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:

registerDefinition(UserTestModel);

To import the definition into other files, simply import the class name from CustomTypeInterfaceDefinition.

Usage Tests:

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);
 console.log("user instanceof UserTestModel from userInstanceDefinition", user instanceof UserTestModel);

License

Released under MIT by @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 Nodejs

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%