shinken008 / converter

Convert data value by data type.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

converter

Javascript conmunicates to java with js-to-java need special schemas, we will preprocess data (if you need a number but get a string parameter) then we can use js-to-java to wrap js object to java object.

Build Status npm version

Install

$ npm install converter-z

Usage

Convert primitives data type, like number, string, boolean, and more.

@converter()
method(
  @convert({ type: 'number' }) id: number, // recommend
  @convert({ id: { type: 'number' } }) id: number, // deprecated
) {
  ...
}

Convert primtives data type in array

@converter()
method(
  @convert({ type: 'number' }) ids: number[], // recommend
  @convert({ ids: { type: 'number' } }) ids: number[], // deprecated
) {
  ...
}

Convert shallow object

@converter()
method(
  @convert({ id: { type: 'number' } }) param: { id: number },
) {
  ...
}

Convert object in array

@converter()
method(
  @convert({ id: { type: 'number' } }) param: Array<{ id: number }>,
) {
  ...
}

Add a new convert type or rewrite convert type

converter.config({
  type: 'increase',
  adaptor: (value, required?: boolean, message?:string) => {
    let nextValue = value
    if (value === '' || value === null || value === undefined) {
      if (required) throw new Error(message)
      nextValue = null
    } else {
      nextValue = +value + 1
    }
    return nextValue
  },
})

Convert deep object

@converter()
method(
  @convert({
    ids: {
      id: { type: 'number' },
    },
  }) param: { ids: { id: number } },
) {
  ...
}

Project progress

  • Converter develop
    • Convert primitives data type, like number, string, boolean, and more
    • Convert primtives data type in array
    • Convert shallow object
    • Convert object in array
    • Add a new convert type or rewrite convert type
    • Convert deep object
    • More

License

MIT

About

Convert data value by data type.


Languages

Language:TypeScript 59.9%Language:JavaScript 40.1%