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

FIO-8037: added number component normalization #91

Merged
merged 1 commit into from
May 20, 2024
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
34 changes: 34 additions & 0 deletions src/process/normalize/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,37 @@ it('Should normalize a radio component value with a number', () => {
normalizeProcessSync(context);
expect(context.data).to.deep.equal({radio: 0});
});

it('Should normalize a number component value with a string value', () => {
const numberComp = {
type: 'number',
key: 'number',
input: true,
label: 'Number'
};
const data = {
number: '000123'
};
const context = generateProcessorContext(numberComp, data);
normalizeProcessSync(context);
expect(context.data).to.deep.equal({number: 123});
});

it('Should normalize a number component value with a multiple values allowed', () => {
const numberComp = {
type: 'number',
key: 'number',
input: true,
label: 'Number',
multiple: true
};
const data = {
number: [
'000.0123',
'123'
]
};
const context = generateProcessorContext(numberComp, data);
normalizeProcessSync(context);
expect(context.data).to.deep.equal({number: [0.0123, 123]});
});
23 changes: 22 additions & 1 deletion src/process/normalize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
DefaultValueScope,
ProcessorInfo,
ProcessorContext,
TimeComponent
TimeComponent,
NumberComponent
} from "types";

type NormalizeScope = DefaultValueScope & {
Expand All @@ -42,6 +43,7 @@ const isSelectBoxesComponent = (component: any): component is SelectBoxesCompone
const isTagsComponent = (component: any): component is TagsComponent => component.type === "tags";
const isTextFieldComponent = (component: any): component is TextFieldComponent => component.type === "textfield";
const isTimeComponent = (component: any): component is TimeComponent => component.type === "time";
const isNumberComponent = (component: any): component is NumberComponent => component.type === "number";

const normalizeAddressComponentValue = (component: AddressComponent, value: any) => {
if (!component.multiple && Boolean(component.enableManualMode) && value && !value.mode) {
Expand Down Expand Up @@ -273,6 +275,22 @@ const normalizeTimeComponentValue = (component: TimeComponent, value: string) =>
return value;
};

const normalizeSingleNumberComponentValue = (component: NumberComponent, value: any) => {
if (!isNaN(parseFloat(value)) && isFinite(value)) {
return +value;
}

return value;
}

const normalizeNumberComponentValue = (component: NumberComponent, value: any) => {
if (component.multiple && Array.isArray(value)) {
return value.map((singleValue) => normalizeSingleNumberComponentValue(component, singleValue));
}

return normalizeSingleNumberComponentValue(component, value);
};

export const normalizeProcess: ProcessorFn<NormalizeScope> = async (context) => {
return normalizeProcessSync(context);
}
Expand Down Expand Up @@ -317,6 +335,9 @@ export const normalizeProcessSync: ProcessorFnSync<NormalizeScope> = (context) =
} else if (isTimeComponent(component)) {
set(data, path, normalizeTimeComponentValue(component, value));
scope.normalize[path].normalized = true;
} else if (isNumberComponent(component)) {
set(data, path, normalizeNumberComponentValue(component, value));
scope.normalize[path].normalized = true;
}

// Next perform component-type-agnostic transformations (i.e. super())
Expand Down
Loading