ts-transformer-dates
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

TypeScript Transformer Dates

Converts object properties from number or string to Date according to provided type having Date fields. Aimed to convert parsed JSON objects to match given TypeScript interface.

Inspired by ts-transformer-keys.

Requirement

TypeScript >= 2.4.1

How to use

In TypeScript source

import { toDates } from 'ts-transformer-dates';

interface Range {
    dateFrom: Date;
    dateTo: Date;
    notes: string;
}

const value = {
    dateFrom: '2020-01-12T03:25:45.000Z', // date as ISO string
    dateTo: 1578799545000, // date as number of milliseconds
    notes: 'brown fox'
};

toDates<Range>(value);

console.log(value);

Outputs:

{
  dateFrom: 2020-01-12T03:25:45.000Z,
  dateTo: 2020-02-12T03:25:45.000Z,
  notes: 'brown fox'
}

webpack

// webpack.config.js
const datesTransformer = require('ts-transformer-dates/lib/transformer').default;

module.exports = {
    // ...
    module: {
        rules: [
            {
                test: /\.ts$/,
                loader: 'ts-loader', // or 'awesome-typescript-loader'
                options: {
                    // make sure not to set `transpileOnly: true` here, otherwise it will not work
                    getCustomTransformers: program => ({
                        before: [datesTransformer(program)]
                    })
                }
            }
        ]
    }
};

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i ts-transformer-dates

Weekly Downloads

105

Version

1.1.2

License

MIT

Unpacked Size

11 kB

Total Files

7

Last publish

Collaborators

  • lduburas