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

@traitjs/core

v1.0.12

Published

This library was created as a foundation for a unique architectural idea. The goal was to unify all CRUD operations into a single codebase, eliminating redundancy and the need for copy/pasting. However, there was a need to specialize some business rules,

Downloads

13

Readme

Overview

This library was created as a foundation for a unique architectural idea. The goal was to unify all CRUD operations into a single codebase, eliminating redundancy and the need for copy/pasting. However, there was a need to specialize some business rules, which led to the concept of features/traits. A trait is a list of dependencies and a function that unites all dependency functionalities into a class, creating a new functionality on top of that. This new functionality can now be used as a dependency. For example, a CRUD service is now a trait and can be extended for more specialization.

Features

The traitjs library provides the following helper functions:

  • applyTraits: This function applies the functionality of an array of traits to a class. It allows you to extend a class with multiple traits, combining their functionalities into a single class.
  • compileTraits: Similar to applyTraits, this function also applies the functionality of an array of traits to a class. However, it doesn't expect a class as a parameter. Instead, it creates a new class with the traits applied, starting from the first trait in the array.
  • makeTrait: This function is used to create a new trait. It allows you to define a trait with a specific set of functionalities. The target parameter provided by makeTrait allows TypeScript and IDEs to infer the type of all the functionalities provided by the trait array.

These helper functions in traitjs enable you to easily implement and apply traits to your classes, reducing redundancy and promoting code reuse.

Usage Examples

Example 1: Extending a class with CRUD traits

import { makeTrait, applyTraits } from '@traitjs/core';

// Create a trait for CRUD operations
const crudTrait = makeTrait((target, options) => {
  return class extends target {
    create() {
      console.log('Create operation');
    }

    read() {
      console.log('Read operation');
    }

    update() {
      console.log('Update operation');
    }

    delete() {
      console.log('Delete operation');
    }
  };
});

class User {}
const UserWithCRUD = applyTraits(User, [crudTrait]);
const user = new UserWithCRUD();
user.create();  // Outputs: Create operation
user.read();  // Outputs: Read operation
user.update();  // Outputs: Update operation
user.delete();  // Outputs: Delete operation

Example 2: Creating a class with CRUD traits

import { makeTrait, compileTraits } from '@traitjs/core';

// Create a trait for CRUD operations
const crudTrait = makeTrait((target, options) => {
  return class extends (target ?? Object) {
    create() {
      console.log('Create operation');
    }

    read() {
      console.log('Read operation');
    }

    update() {
      console.log('Update operation');
    }

    delete() {
      console.log('Delete operation');
    }
  };
});

const UserWithCRUD = compileTraits([crudTrait]);
const user = new UserWithCRUD();
user.create();  // Outputs: Create operation
user.read();  // Outputs: Read operation
user.update();  // Outputs: Update operation
user.delete();  // Outputs: Delete operation

Example 3: Creating a trait with overridden functionality

import { makeTrait, applyTraits } from '@traitjs/core';

// Create a trait for CRUD operations
const crudTrait = makeTrait((target, options) => {
  return class extends target {
    create() {
      console.log('Create operation');
    }

    read() {
      console.log('Read operation');
    }

    update() {
      console.log('Update operation');
    }

    delete() {
      console.log('Delete operation');
    }
  };
});

// Create a trait that overrides the create functionality and adds a new functionality called readAll
const newTrait = makeTrait(
  (target, options) => {
    return class extends target {
      create() {
        console.log('Overridden create operation');
      }

      readAll() {
        console.log('Read all operation');
      }
    };
  },
  [crudTrait]
);

class User {}
const UserWithTraits = applyTraits(User, [newTrait]);
const user = new UserWithTraits();
user.create();  // Outputs: Overridden create operation
user.read();  // Outputs: Read operation
user.readAll();  // Outputs: Read all operation
user.update();  // Outputs: Update operation
user.delete();  // Outputs: Delete operation

Conclusion

The traitjs library provides a proof of concept for implementing traits in TypeScript. With the help of the provided helper functions, you can experiment with creating and applying traits to your classes, exploring the potential benefits of reducing redundancy and promoting code reuse. It's important to note that this library is not fully polished or finished, and it serves as a starting point for further exploration. Your feedback, additional functionalities, and different perspectives are highly encouraged and welcomed. Feel free to contribute and enhance the traitjs library according to your specific needs and requirements.