@jamilservices/custom-class-type-and-definition
v0.1.3
Published
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.
Downloads
2
Maintainers
Readme
@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.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 importingCustomTypeInterfaceDefinition
- 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: "[email protected]"
});
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