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

class-converter-ts

v0.1.25

Published

class converter for typescript is a transformer to deserializable xml to typescript class and serializable class to xml

Downloads

12

Readme

class-converter-ts

class converter for typescript is a transformer to deserializable xml to typescript class and serializable class to xml.

Why use it ?

Maybe this project is the best xml2ob solution in JS / TS, maybe not.

Npmjs.com has many XML to object projects. Why should I reinvent a wheel?

I have an AS3 project, which uses flexxb [https://github.com/alexandruciobanu/flexxb] for persistence support. Unfortunately, after the project is migrated to typescript, all projects cannot solve my problem:

<root>

<a>1</a>

<b>2</b>

<a>3</a>

</root>

After it is converted into JSON, it becomes {A: [1,3], B: 2}. After it is converted into XML again, the order changes to:

<root>

<a>1</a>

<a>3</a>

<b>2</b>

</root>

This is not what I want, so I created this project. According to the design idea of flexxb, I realized the sequential reservation function of mixed object nodes.

mixed xml node:

<contents>

<calendar/>

<text_layer/>

<photo_layer/>

<decorate_layer/>

<photo_layer/>

</contents>

Class:

export class Contents extends GraphiceObject {



@detect('layers')

@array ()

@element('calendar',CalendarLayer)

private calendar:CalendarLayer



@detect('layers')

@array ()

@element('text_layer',TextLayer)

private text_layer:TextLayer



@detect('layers')

@array ()

@element('decorate_layer',DecorateLayer)

private decorate_layer:DecorateLayer



@detect('layers')

@array ()

@element('photo_layer',PhotoLayer)

private photo_layer:PhotoLayer



public layers: Array < any >;



}

There is a simple example:

@alias("user") // use to automatic detection type in  a mixed array, 
class UserModel {
  @property('version')
  private version: string;

  @element('i')
  id: number;

  @element('n')
  name: string;
}

const userXML = '<?xml version="1.0" encoding="utf-8" ?>
<user version="0.1">
    <i>123456</i>
    <n>user-name</n>
    <e>[email protected]</e>
    <at>1a1b1b3b4c34d234</at>
</user>'

// use toClass to convert plain object to class
const userModel = toClass(userXML, UserModel);
// you will get a class, just like below one
// UserModel {
//   version: '0.1',
//   id: 1234,
//   name: 'name',
// }

const userVO = toPlain(userModel, UserModel);
// you will get a json object, just like below one
// {
//   _attribute: { version: '0.1' },
//   id: 1234,
//   name: 'name',
// }


const outputXML = toXMLString(userModel, UserModel);
// you will get xml like userXML

Installation

npm install class-converter-ts npm install --save

Methods

toClass(raw: { [key: stirng]: any }, clazzType: ClassType) / toClasses(raw: { [key: stirng]: any }[], clazzType: ClassType)

convert a XML object to class

const userModel = toClass(userRaw), UserModel);
const userModels = toClasses(userRaws), UserModel);

toPlain(instance: ClassType | { [key: stirng]: any }, clazzType: ClassType) / toPlains(instances: ClassType | { [key: stirng]: any }[], clazzType: ClassType)

convert a plain/class object to rawPlain

const userRaw = toPlain(userModel, UserModel);
const userRaws = toPlains(userModels, UserModel);
### toXMLString<T>(instance: any, Clazz: BasicClass<T>, key?: any): any /  toXMLStrings<T>(instances: (T | JosnType)[], Clazz: BasicClass<T>, key?: any): any[]

convert a class to xml string
### alias(classAlias: string)
define a model, classAlias will use to set the xml node name when convert class to XML .
model:

@alias("animal")
xml:
<animal>
</animal>
### element(originalKey: string, targetClass?: { new (...args: any[]): any }, required = false)

convert a original key to your customized key, like `version => ver`
model:
@property('version')
private ver: string;
xml:
<node version="1.0"/>
### property(key: string, clazzType?: any, required = false)

convert a original key to your customized key, like `i => id`
model:
@element('i')
id: number;
xml:
<i>
</i>
import { property, deserialize } from 'class-converter-ts';
import moment from 'moment';

class UserEduModel {
  @element('i')
  id: number;

  @element('n')
  name: string;
}

class UserModel {
  @element('i')
  id: number;

  @element('n')
  name: string;

  @element('a', null, false)
  avatarUrl: string;

  @element('e', UserEduModel)
  edu: UserEduModel;
}

export class AdminUserModel extends UserModel {
  @element('r')
  role: number;
}

detect(convertKey: string)

If the node is a mixed object, you need to use the detect keyword to set the object and node name that can be detected, and the parameter convertkey is the array attribute name added to the detected object

array(dimension?: 1 | 2)

  • [optional]dimension dimension of the array, default 1
import { property, deserialize, array } from 'class-converter';
import moment from 'moment';

class UserModel {
  @element('i')
  id: number;

  @element('n')
  name: string;
}

class DepartmentModel {
  @element('i')
  id: number;

  @element('n')
  name: string;

  @array()
  @element('e', UserModel)
  employees: UserModel[];
}

deserialize(deserializer: (value: any, instance: any, origin: any) => any

convert original value to customized data, it only be used when use toClass/toClasses function

import { property, deserialize, toClass } from 'class-converter';

class UserModel {
  @element('i')
  id: number;

  @element('n')
  name: string;

  @deserialize((value: string) => `${value}@xxx.com`)
  @element('m')
  mail: string;
}

const user = toClass(
  {
    i: 1234,
    n: 'name',
    m: 'mail',
  },
  UserModel,
);

// you will get like this
// {
//   id: 1234,
//   name: 'name',
//   mail: '[email protected]',
// };

serialize(serializer: (value: any, instance: any, origin: any) => any

convert customized value to original value, it only be used when use toPlain/toPlains function

import { property, deserialize, serialize, toPlain } from 'class-converter';

class UserModel {
  @serialize((mail: string) => mail.replace('@xxx.com', ''))
  @deserialize((value: string) => `${value}@xxx.com`)
  @element('e')
  mail: string;
}

const user = toPlain({
  mail: '[email protected]',
}, UserModel);

// you will get like this
// const user = {
//   e: '[email protected]',
// };