Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added a way to specify default option for the normalizers. #712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export abstract class BaseNormalizer<Options> {
constructor(protected readonly defaultOptions?:Partial<Options>) {
}

getOptions(options?: Options): Options {
// Merge the options into the default options (Not doing a deep merge)
return Object.assign({}, this.defaultOptions, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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<Date | undefined, DateNormalizerOptions> {
export class DateNormalizer extends BaseNormalizer<DateNormalizerOptions> implements DataNormalizerInterface<Date | undefined, DateNormalizerOptions> {
getUniqueKey(): string {
return DateNormalizer.name;
}

normalize(source: any, options?: DateNormalizerOptions): Date | undefined {
const typeEnum = TypeUtils.getTypeOfValue(source);

options = this.getOptions(options);

if (typeEnum === undefined) {
if (options?.returnUndefinedOnInvalidDate === false) {
return new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {DataNormalizerInterface} from "../interfaces/data-normalizer.interface";
import {DataNormalizerUniqueKey} from "../types/data-normalizer-unique-key.type";
import {NormalizerInvalidSourceTypeError} from "../errors/normalizer-invalid-source-type.error";
import {LowercaseNormalizerOptions} from "../normalizer-options/lowercase-normalizer.options";
import {BaseNormalizer} from "./base.normalizer";

export class LowercaseNormalizer implements DataNormalizerInterface<string, LowercaseNormalizerOptions>{
export class LowercaseNormalizer extends BaseNormalizer<LowercaseNormalizerOptions> implements DataNormalizerInterface<string, LowercaseNormalizerOptions>{
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./base.normalizer";
export * from "./date.normalizer";
export * from "./lowercase.normalizer";
export * from "./number.normalizer";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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<number | undefined, NumberNormalizerOptions> {
export class NumberNormalizer extends BaseNormalizer<NumberNormalizerOptions> implements DataNormalizerInterface<number | undefined, NumberNormalizerOptions> {
getUniqueKey(): string {
return NumberNormalizer.name;
}

normalize(source: any, options?: NumberNormalizerOptions): number | undefined {
const typeEnum = TypeUtils.getTypeOfValue(source);

options = this.getOptions(options);

switch (typeEnum) {
case TypeEnum.String:
const value = parseFloat(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import {DataNormalizerInterface} from "../interfaces/data-normalizer.interface";
import {TypeEnum, TypeUtils} from "@pristine-ts/metadata";
import {StringNormalizerOptions} from "../normalizer-options/string-normalizer.options";
import {format} from "date-fns";
import {BaseNormalizer} from "./base.normalizer";

export class StringNormalizer implements DataNormalizerInterface<string | undefined, StringNormalizerOptions> {
export class StringNormalizer extends BaseNormalizer<StringNormalizerOptions> implements DataNormalizerInterface<string | undefined, StringNormalizerOptions> {
getUniqueKey(): string {
return StringNormalizer.name;
}

normalize(source: any, options?: StringNormalizerOptions): string | undefined {
const typeEnum = TypeUtils.getTypeOfValue(source);

options = this.getOptions(options);

if (typeEnum === undefined || typeEnum === TypeEnum.Null) {
if (options?.ignoreUndefined === false) {
return "";
Expand Down
Loading