transform-class-properties
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta • Public • Published

transform-class-properties

Automatically transforms values of class properties.

Prerequisites

  • TypeScript >= 1.5

  • Enable the experimentalDecorators in tsconfig.json.

Note: ES5 is min target minimun to build.

Installation

npm install transform-class-properties --save

or

yarn add transform-class-properties

Usage

import {
  Float,
  RemoveNonNumeric,
  ToUpperCase,
  transform,
  Trim
} from 'transform-class-properties';
 
class Book {
  @ToUpperCase()
  @Trim()
  public title: string;
 
  @Float(2)
  public price: number;
 
  @RemoveNonNumeric()
  public issn: string;
}
 
const book1 = new Book();
book1.title = '    My first book    ';
book1.price = 12.3210;
book1.issn = '1234-5679'
 
console.log(transform(book1)); // Book { title: 'MY FIRST BOOK', price: 12.32, issn: '12345679' }

Decorators

Decorator Description Class property type
@Append(additionalValue: string | number) Adds value at the end. string
@Capitalize() Capitalize the first letter and lowercase the rest. string
@Float(fractionDigits?: number) Converts number to float. number
@Integer() Converts number to integer. number
@Prepend(additionalValue: string | number) Adds value at the beginning. string
@Regex(expression: RegExp) Returns matched value with regular expression. string
@RemoveNonNumeric() Removes anything non-numeric character. string
@RemoveNumeric() Removes numeric characters. string
@Replace(searchValue: string | RegExp, replaceValue: string) Find and replace value. string
@ToLowerCase() Lower case characters. string
@ToUpperCase() Upper case characters. string
@Trim() Trim characters. string

License

MIT

Package Sidebar

Install

npm i transform-class-properties

Weekly Downloads

19,718

Version

1.0.0-beta

License

MIT

Unpacked Size

69.6 kB

Total Files

171

Last publish

Collaborators

  • lindomar-oliveira