0.1.3 • Published 2 years ago
@jamilservices/custom-class-type-and-definition v0.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
Installation ways:
- from github:
npm install --save git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.gityarn add git+https://github.com/jamilservicos/jamilservices-custom-class-type-and-definition.git- from npm:
npm install --save @jamilservices/custom-class-type-and-definitionyarn add @jamilservices/custom-class-type-and-definitionConfigure 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
classas a definition to be used by instanceof in any part of the application just by importingCustomTypeInterfaceDefinition - Extract the
objectjust using.toObject(). - Extract the
jsonjust 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