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

react3l

v7.0.4

Published

React3L libraries for React projects

Downloads

260

Readme

react3l

React3L libraries for React projects

NPM JavaScript Style Guide

Install

npm install --save react3l

or using yarn:

yarn add react3l

Documentation

Introduction

react3l is a powerful library designed to simplify the creation of Model classes in React applications. It provides a set of TypeScript decorators that enable users to define Model classes effortlessly, while also offering numerous benefits in terms of data mapping and maintaining object integrity.

Decorators

Enum

The Enum decorator is used to mark a field as an enum value. It ensures that the assigned value is a member of the specified enum object.

Usage:

import { Enum } from 'react3l';

enum Status {
  Active = 'Active',
  Inactive = 'Inactive',
}

class User {
  @Enum(Status)
  status?: Status;
}
Field

The Field decorator marks a field with primitive data types such as String, Number, or Boolean. It also provides the ability to define custom prototype functions for data transformation.

Usage:

import { Field, AutoModel } from 'react3l';

@AutoModel()
class User {
  @Field(String)
  name?: string;

  @Field(Number)
  age?: number;

  @Field(Boolean)
  isActive?: boolean;
}
List

The List decorator marks a field as a list of primitive values. It allows for mapping each element of the list with a specified prototype function.

Usage:

import { List } from 'react3l';

class User {
  @List(String)
  hobbies?: string[];
}
MomentField

The MomentField decorator is used for fields that require moment formatting, typically used for handling date and time values.

import { MomentField } from 'react3l';

class Task {
  @MomentField()
  deadline?: moment.Moment;
}
ObjectField

The ObjectField decorator marks a field as a model relation. It allows for mapping nested objects or model instances.

Usage:

import { ObjectField } from 'react3l';

class Address {
  @Field(String)
  street?: string;

  @Field(String)
  city?: string;
}

class User {
  @ObjectField(Address)
  address?: Address;
}
ObjectList

The ObjectList decorator marks a field as a list of model relations. It enables mapping a list of nested objects or model instances.

Usage:

import { ObjectList } from 'react3l';

class Comment {
  @Field(String)
  content?: string;
}

class Post {
  @ObjectList(Comment)
  comments?: Comment[];
}

Effortless Model Classes

One of the significant benefits of using Model classes defined with react3l decorators is their seamless integration with assignment operations and Object.assign. Let's explore these benefits with examples:

Property Descriptor Preservation

Traditional approaches to data mapping in TypeScript often break the property descriptor when using assignment operations. However, react3l ensures that the property descriptor remains intact, even when mapping special data types such as Date objects or strings from JSON.

import { MomentField } from 'react3l';

class Task {
  @MomentField()
  deadline?: moment.Moment;
}

const task = new Task();
task.deadline = moment(); // Property descriptor preserved
Instanceof Operator Compatibility

Another common issue is that the instanceof operator may return true when checking if an object is an instance of a class, even if it's not intended. react3l prevents this behavior, ensuring that instanceof only returns true for instances of the specified class.

import { ObjectField } from 'react3l';

class Address {
  @Field(String)
  street?: string;

  @Field(String)
  city?: string;
}

class User {
  @ObjectField(Address)
  address?: Address;
}

const user = new User();
console.log(user.address instanceof Address); // Returns true
console.log(user.address instanceof Object); // Returns false

Custom Decorators

In addition to the built-in decorators, you can also create custom decorators in react3l to further enhance the functionality of your Model classes. For example, the AutoModel decorator automatically applies property descriptors from a base prototype to the decorated class.

Usage:

import { AutoModel } from 'react3l';

@AutoModel()
class MyModel {
  // Your model properties here
}

The AutoModel class decorator can be applied to a class that contains property decorators such as Field, allowing for automatic property descriptor application.

Usage:

import { AutoModel, Field } from 'react3l';

@AutoModel()
class User {
  @Field(String)
  public id: string;
}

Conclusion

In conclusion, react3l provides a powerful set of decorators for defining Model classes in React applications. These decorators simplify data mapping, enable type checking, and ensure data integrity throughout your application. With react3l, you can create robust and maintainable React components effortlessly.

https://react3l.github.io

License

MIT © thanhtunguet