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

type-json-mapper

v1.2.9

Published

Instead of directly using api data, we definitely require an adapter layer to transform data as needed. Furthermore, the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and highly unreliable as data structur

Downloads

40

Readme

type-json-mapper

codecov

Introduction

Instead of directly using api data, we definitely require an adapter layer to transform data as needed. Furthermore, the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter which becomes reliable. Thus, this library is created as the adapter. It also supports mock data.

Doc

中文文档

Get Started

npm install type-json-mapper --save

Example

Here is a complex example, hopefully could give you an idea of how to use it:

import { mapperProperty, deepMapperProperty, filterMapperProperty } from "type-json-mapper";

class Lesson {
  @mapperProperty('ClassName', 'string')
  public name: string;

  @mapperProperty('Teacher', 'string')
  public teacher: string;

  @mapperProperty('DateTime', 'datetime')
  public datetime: string;

  @mapperProperty('Date', 'date')
  public date: string;

  @mapperProperty('Time', 'time')
  public time: string;

  @mapperProperty('Compulsory', 'boolean')
  public compulsory: boolean;

  constructor() {
    this.name = '';
    this.teacher = '';
    this.datetime = '';
    this.date = '';
    this.time = '';
    this.compulsory = false;
  }
}

class Address {
  @mapperProperty('province', 'string')
  public province: string;
  @mapperProperty('city', 'string')
  public city: string;
  @mapperProperty('full_address', 'string')
  public fullAddress: string;

  constructor() {
    this.province = '';
    this.city = '';
    this.fullAddress = '';
  }
}

class Student {
  @mapperProperty('StudentID', 'string')
  public id: string;

  @mapperProperty('StudentName', 'string')
  public name: string;

  @mapperProperty('StudentAge', 'int')
  public age: number;

  @mapperProperty('NotFloat', 'float')
  public notFloat: number;

  @mapperProperty('NotANum', 'int')
  public notNum: number;

  @mapperProperty('UnknownType', 'String')
  public unknownType: string;

  @mapperProperty('Grade', 'float')
  public grade: number;

  @mapperProperty('AddressIds', 'string')
  public addressIds?: string[];

  @deepMapperProperty('Address', Address)
  public address?: Address;

  @deepMapperProperty('Lessons', Lesson)
  public lessons?: Lesson[];

  @filterMapperProperty('State', (val = 0) => {
    const map = { '0': '未知', '1': '读书中', '2': '辍学', '3': '毕业' };
    return map[`${val}`];
  })
  public status: string;

  public extra: string;

  constructor() {
    this.id = '';
    this.name = '';
    this.age = 0;
    this.notNum = 0;
    this.notFloat = 0;
    this.unknownType = '';
    this.grade = 0;
    this.addressIds = [];
    this.address = null;
    this.lessons = [];
    this.status = '';
    this.extra = '';
  }
}

Now here is what API server return, assume it is already parsed to JSON object.

const json = {
  StudentID: '123456',
  StudentName: '李子明',
  StudentAge: '10',
  NotANum: 'lol',
  NotFloat: 'def',
  UnknownType: 'funny',
  Grade: '98.6',
  AddressIds: [1, 2],
  Address: {
    province: '广东',
    city: '深圳',
    full_address: 'xxx小学三年二班'
  },
  Lessons: [
    {
      ClassName: '中国上下五千年',
      Teacher: '建国老师',
      DateTime: 1609430399000,
      Date: 1609430399000,
      Time: 1609430399000,
      Compulsory: 1
    },
    {
      ClassName: '古筝的魅力',
      Teacher: '美丽老师',
      DateTime: ''
    }
  ],
  State: 1,
  extra: '额外信息'
  }

Deserialize

Simply, just map it use following code.

import { deserialize } from 'type-json-mapper';
try {
  const student = deserialize(Student, json);
  console.log(student);
} catch(err) {
  console.error(err);
}

the result

{
  id: '123456',
  name: '李子明',
  age: 10,
  notNum: 'lol',
  notFloat: 'def',
  unknownType: 'funny',
  grade: 98.6,
  addressIds: ['1', '2'],
  address: { province: '广东', city: '深圳', fullAddress: 'xxx小学三年二班' },
  lessons: [
    {
      name: '中国上下五千年',
      teacher: '建国老师',
      datetime: '2020-12-31 23:59:59',
      date: '2020-12-31',
      time: '23:59:59',
      compulsory: true
    },
    {
      name: '古筝的魅力',
      teacher: '美丽老师',
      datetime: '',
      date: undefined,
      time: undefined,
      compulsory: undefined
    }
  ],
  status: '读书中',
  extra: '额外信息'
}

Serialize

import { serialize } from 'type-json-mapper';
try {
  const params = serialize(Student, student);
  console.log(params);
} catch(err) {
  console.error(err);
}
{
  StudentID: '123456',
  StudentName: '李子明',
  StudentAge: 10,
  NotANum: 'lol',
  NotFloat: 'def',
  UnknownType: 'funny',
  Grade: 98.6,
  AddressIds: [ '1', '2' ],
  Address: { province: '广东', city: '深圳', full_address: 'xxx小学三年二班' },
  Lessons: [
    {
      ClassName: '中国上下五千年',
      Teacher: '建国老师',
      DateTime: '2020-12-31 23:59:59',
      Date: '2020-12-31',
      Time: '23:59:59',
      Compulsory: true
    },
    { ClassName: '古筝的魅力', Teacher: '美丽老师', DateTime: '' }
  ],
  State: '读书中',
  extra: '额外信息'
}

Mock data

import { mock } from 'type-json-mapper';
const res = mock(Student, { fieldLength: { age: 20, grade: 4, name: 6 }, arrayFields: ['lessons'] });
console.dir(res);
{
  id: 'uBP2ug',
  name: 'EmcuTs',
  age: 7,
  notNum: 4,
  notFloat: 624.775,
  unknownType: '',
  grade: 537.3,
  addressIds: 'cjcpoC',
  address: { province: '1VSl9Z', city: 'tCtF9K', fullAddress: 'Ves3rZ' },
  lessons: [
    {
      name: 'm2WqeY',
      teacher: 'UdIuLT',
      datetime: '2024-03-17 03:54:59',
      date: '2024-03-17',
      time: '03:54:59',
      compulsory: false
    },
    {
      name: 'dCEoGG',
      teacher: 'gJrmkV',
      datetime: '2024-03-17 03:54:59',
      date: '2024-03-17',
      time: '03:54:59',
      compulsory: false
    },
    {
      name: 'JDVZqP',
      teacher: 'dV8ITf',
      datetime: '2024-03-17 03:54:59',
      date: '2024-03-17',
      time: '03:54:59',
      compulsory: false
    }
  ],
  status: '未知',
  extra: ''
}

Make sure the decorator is assigned a type

Test

npm run test