Skip to content

Commit

Permalink
FIO-8037: added number component normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
KatrinKhilko committed May 8, 2024
1 parent d8e8c0b commit 1a640b5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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

0 comments on commit 1a640b5

Please sign in to comment.