diff --git a/packages/data-mapping-common/src/normalizers/base.normalizer.ts b/packages/data-mapping-common/src/normalizers/base.normalizer.ts new file mode 100644 index 00000000..7ba378c2 --- /dev/null +++ b/packages/data-mapping-common/src/normalizers/base.normalizer.ts @@ -0,0 +1,9 @@ +export abstract class BaseNormalizer { + constructor(protected readonly defaultOptions?:Partial) { + } + + getOptions(options?: Options): Options { + // Merge the options into the default options (Not doing a deep merge) + return Object.assign({}, this.defaultOptions, options); + } +} \ No newline at end of file diff --git a/packages/data-mapping-common/src/normalizers/date.normalizer.spec.ts b/packages/data-mapping-common/src/normalizers/date.normalizer.spec.ts index c8db33f4..4f47c5ff 100644 --- a/packages/data-mapping-common/src/normalizers/date.normalizer.spec.ts +++ b/packages/data-mapping-common/src/normalizers/date.normalizer.spec.ts @@ -96,4 +96,19 @@ describe('DateNormalizer', () => { expect(normalizedDate!.getMonth()).toEqual(expectedDate.getMonth()); expect(normalizedDate!.getFullYear()).toEqual(expectedDate.getFullYear()); }); + + it('should return a Date object for a number representing milliseconds when the default options is seconds and it gets overwritten', () => { + const normalizer = new DateNormalizer({treatNumbers: "seconds"}); + const milliseconds = 1675275580000; // Represents 2024-01-31T18:53:00Z + const expectedDate = new Date(milliseconds); + expect(normalizer.normalize(milliseconds, new DateNormalizerOptions({treatNumbers: "milliseconds"}))).toEqual(expectedDate); + }); + + it('should treat a number as seconds when default options.treatNumbers is "seconds"', () => { + const normalizer = new DateNormalizer({treatNumbers: "seconds"}) + const seconds = 1675275580; // Represents 2024-01-31T18:53:00Z in seconds + const expectedDate = new Date(seconds * 1000); + expect(normalizer.normalize(seconds)).toEqual(expectedDate); + }); + }); \ No newline at end of file diff --git a/packages/data-mapping-common/src/normalizers/date.normalizer.ts b/packages/data-mapping-common/src/normalizers/date.normalizer.ts index a37fcc35..0fbb00fa 100644 --- a/packages/data-mapping-common/src/normalizers/date.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/date.normalizer.ts @@ -1,8 +1,9 @@ import {DataNormalizerInterface} from "../interfaces/data-normalizer.interface"; import {DateNormalizerOptions} from "../normalizer-options/date-normalizer.options"; import {TypeEnum, TypeUtils} from "@pristine-ts/metadata"; +import {BaseNormalizer} from "./base.normalizer"; -export class DateNormalizer implements DataNormalizerInterface { +export class DateNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { return DateNormalizer.name; } @@ -10,6 +11,8 @@ export class DateNormalizer implements DataNormalizerInterface{ +export class LowercaseNormalizer extends BaseNormalizer implements DataNormalizerInterface{ getUniqueKey(): DataNormalizerUniqueKey { return LowercaseNormalizer.name; } normalize(source: any, options?: LowercaseNormalizerOptions): string { + options = this.getOptions(options); + if(typeof source !== "string") { if(options && options.shouldThrowIfTypeIsNotString) { throw new NormalizerInvalidSourceTypeError("The 'LowercaseNormalizer' expects the source value to be of type 'string'. Type '" + typeof source+ "' was received.", this.getUniqueKey(), options, source, typeof source) diff --git a/packages/data-mapping-common/src/normalizers/normalizers.ts b/packages/data-mapping-common/src/normalizers/normalizers.ts index 6b5d95d0..b76226ac 100644 --- a/packages/data-mapping-common/src/normalizers/normalizers.ts +++ b/packages/data-mapping-common/src/normalizers/normalizers.ts @@ -1,3 +1,4 @@ +export * from "./base.normalizer"; export * from "./date.normalizer"; export * from "./lowercase.normalizer"; export * from "./number.normalizer"; diff --git a/packages/data-mapping-common/src/normalizers/number.normalizer.ts b/packages/data-mapping-common/src/normalizers/number.normalizer.ts index 64172345..3dfe8424 100644 --- a/packages/data-mapping-common/src/normalizers/number.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/number.normalizer.ts @@ -1,8 +1,9 @@ import {DataNormalizerInterface} from "../interfaces/data-normalizer.interface"; import {NumberNormalizerOptions} from "../normalizer-options/number-normalizer.options"; import {TypeEnum, TypeUtils} from "@pristine-ts/metadata"; +import {BaseNormalizer} from "./base.normalizer"; -export class NumberNormalizer implements DataNormalizerInterface { +export class NumberNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { return NumberNormalizer.name; } @@ -10,6 +11,8 @@ export class NumberNormalizer implements DataNormalizerInterface { +export class StringNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { return StringNormalizer.name; } @@ -11,6 +12,8 @@ export class StringNormalizer implements DataNormalizerInterface