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

js-ts-mapper

v1.2.0

Published

Mapping json object to Typescript class

Downloads

102

Readme

js-ts-mapper

Mapping Json object to Typescript class

Installing

For installing of the npm-package run command: npm install js-ts-mapper.

Commands

Building into ./dist: npm run compile.

Running of tests: npm run test.

Publication:

  1. Update a version in the package.json.
  2. Run command npm run version.

Example of using

import { JsonProperty, JsTsCustomConvert } from 'js-ts-mapper';
import * as moment from 'moment';

export class Address {    
    @JsonProperty('Value')
    value: string;
}

export class ClientComponent {
    @JsonProperty('Id')
    id: number;

    @JsonProperty('Name')
    name: string;

    @JsonProperty('Gender')
    gender: Gender = Gender.Male;

    @JsonProperty('DateBirth', DateConverter)
    dateBirth: Date;

    // The case of using with Array
    @JsonProperty('BankAccounts', [BankAccount])
    bankAccounts?: Array<BankAccount>;

    @JsonProperty("Address")
    address?: Address;
}

export class BankAccount {

    @JsonProperty('Id')
    id: number;
    
    @JsonProperty('Number')
    number: string;
}

export enum Gender {
    Female = 1,
    Male = 2
}

export class DateConverter implements JsTsCustomConvert<Date> {
    serialize(date: Date): any {
        if (date) {
          return moment(date).format('YYYY-MM-DDT00:00:00');
        }
    }
    deserialize(date: string): Date {
        if (date) {
            return moment(date, 'YYYY-MM-DDT00:00:00').toDate();
        }
    }
}

// Main serializer
let mapper = new JsTsMapper();

// Mocks
let client = new ClientComponent();
client.id = 2563;
client.name = 'Test Test Test';
client.gender = Gender.Male;
client.dateBirth = new Date(1990, 5, 10);
client.address = new Address();
client.address.value = '27 Old Gloucester Street, London';
client.bankAccounts = [new BankAccount(), new BankAccount()];
client.bankAccounts[0].id = 256;
client.bankAccounts[0].number = '545454549';
client.bankAccounts[1].id = 253;
client.bankAccounts[1].number = '545674423';

// try serialize 
let serializedClient = map.serialize(client); 
console.log(serializedClient);

/*
    returns 
    {
        Id: 2563,
        Name: 'Test Test Test',
        Gender: 2,
        DateBirth: '1990-06-10T00:00:00',
        BankAccounts: [
            { Id: 256, Number: '545454549' },
            { Id: 253, Number: '545674423' }
        ],
        Address: {
            Value: '27 Old Gloucester Street, London'
        }
    }
*/

// try deserialize 
let deserializedClient = map.deserialize(serializedClient, ClientComponent); 
console.log(deserializedClient);

/*
    returns
    {
        gender: 2,
        id: 2563,
        name: 'Test Test Test',
        dateBirth: '1990-06-09T20:00:00.000Z',
        bankAccounts: [
            { id: 256, number: '545454549' },
            { id: 253, number: '545674423' }
        ],
        address: { value: '27 Old Gloucester Street, London' }
    }
*/

By default all undecorated properties (which don't have a decorator @JsonProperty()) pass through the serialization/deserialization. Decorator @SerializeOnlyDecorated can corrects this case and switch on the ignoring such properties.


import { SerializeUndecorated, JsonProperty, JsTsMapper } from "js-ts-mapper";

@SerializeOnlyDecorated()
export class Employeer {
    constructor(o) {
        Object.assign(this, o);
    }

    @JsonProperty('Id')
    id: number;

    @JsonProperty('FirstName')
    firstName: string;

    @JsonProperty('LastName')
    lastName: string;

    @JsonProperty('MiddleName')
    middleName: string;

    selected: boolean = true;
}

let test_entity = new Employeer({
    id: 256,
    firstName: 'Test',
    lastName: 'Test',
    middleName: 'Test',
    selected: true
});

let out = mapper.serialize(test_entity);    
/*
    returns
    {
        Id: 256,
        FirstName: 'Test',
        LastName: 'Test',
        MiddleName: 'Test'
    }
*/

Converters

Converters included in build

Date converters

  • DateConverter
  • DateTimeOffsetConverter
  • DateTimeOffsetConverterWithTimeZone
  • DateTimeOffsetConverterWithoutTimeZone

To use date converters you must imported the "moment" library.

npm install moment --save

Example of using

import { JsonProperty } from 'js-ts-mapper';
import { DateConverter } from 'js-ts-mapper/converters';
import * as moment from 'moment';

export class Example {    
    @JsonProperty('dateBirth', DateConverter)
    dateBirth: Date;
}

Common converters

  • StringToArray