npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

10

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

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: "[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

JavaScript Nodejs