diff --git a/packages/core/database/src/__tests__/percent2float.test.ts b/packages/core/database/src/__tests__/percent2float.test.ts new file mode 100644 index 000000000..1c50391be --- /dev/null +++ b/packages/core/database/src/__tests__/percent2float.test.ts @@ -0,0 +1,14 @@ +import { percent2float } from '../utils'; + +describe('percent2float', () => { + it('should be NaN', () => { + expect(percent2float('123')).toBe(NaN); + expect(percent2float('3a')).toBe(NaN); + expect(percent2float('3a%')).toBe(NaN); + }); + + it('should be a floating point number', () => { + expect(percent2float('123%')).toBe(1.23); + expect(percent2float('22.5507%')).toBe(0.225507); // not 0.22550699999999999 + }); +}); diff --git a/packages/core/database/src/__tests__/value-parsers/number.test.ts b/packages/core/database/src/__tests__/value-parsers/number.test.ts new file mode 100644 index 000000000..7498a0607 --- /dev/null +++ b/packages/core/database/src/__tests__/value-parsers/number.test.ts @@ -0,0 +1,46 @@ +import { NumberValueParser } from '../../value-parsers'; + +describe('number value parser', () => { + let parser: NumberValueParser; + + beforeEach(() => { + parser = new NumberValueParser({}, {}); + }); + + const expectValue = (value) => { + parser = new NumberValueParser({}, {}); + parser.setValue(value); + return expect(parser.getValue()); + }; + + it('should be number', () => { + expectValue(123).toBe(123); + expect(parser.errors.length === 0).toBeTruthy(); + expectValue('123').toBe(123); + expect(parser.errors.length === 0).toBeTruthy(); + expectValue('123%').toBe(1.23); + expect(parser.errors.length === 0).toBeTruthy(); + expectValue('22.5507%').toBe(0.225507); + expect(parser.errors.length === 0).toBeTruthy(); + }); + + it('should be null', () => { + expectValue('').toBe(null); + expect(parser.errors.length === 0).toBeTruthy(); + expectValue('n/a').toBe(null); + expect(parser.errors.length === 0).toBeTruthy(); + expectValue('-').toBe(null); + expect(parser.errors.length === 0).toBeTruthy(); + }); + + it('should be errors', () => { + expectValue({}).toBe(null); + expect(parser.errors.length > 0).toBeTruthy(); + expectValue('123a').toBe(null); + expect(parser.errors.length > 0).toBeTruthy(); + expectValue('123a%').toBe(null); + expect(parser.errors.length > 0).toBeTruthy(); + expectValue('aaa').toBe(null); + expect(parser.errors.length > 0).toBeTruthy(); + }); +}); diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index 78709d713..be47859a6 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -117,6 +117,13 @@ export function patchSequelizeQueryInterface(db: Database) { } export function percent2float(value: string) { + if (!value.endsWith('%')) { + return NaN; + } + let val = value.substring(0, value.length - 1); + if (isNaN(+val)) { + return NaN; + } const index = value.indexOf('.'); if (index === -1) { return parseFloat(value) / 100; diff --git a/packages/core/database/src/value-parsers/base-value-parser.ts b/packages/core/database/src/value-parsers/base-value-parser.ts index 48666d6ac..b04e2afbb 100644 --- a/packages/core/database/src/value-parsers/base-value-parser.ts +++ b/packages/core/database/src/value-parsers/base-value-parser.ts @@ -7,6 +7,7 @@ export class BaseValueParser { constructor(field: any, ctx: any) { this.field = field; this.ctx = ctx; + this.value = null; } toString() { diff --git a/packages/core/database/src/value-parsers/number-value-parser.ts b/packages/core/database/src/value-parsers/number-value-parser.ts index 75e3f0e2f..acd013b1a 100644 --- a/packages/core/database/src/value-parsers/number-value-parser.ts +++ b/packages/core/database/src/value-parsers/number-value-parser.ts @@ -5,19 +5,25 @@ export class NumberValueParser extends BaseValueParser { async setValue(value: any) { if (value === null || value === undefined || typeof value === 'number') { this.value = value; - } - if (typeof value === 'string') { + } else if (typeof value === 'string') { if (!value) { this.value = null; } else if (['n/a', '-'].includes(value.toLowerCase())) { this.value = null; - } else if (value.endsWith('%')) { - this.value = percent2float(value); - console.log(value, this.value); } else { - const val = +value; - this.value = isNaN(val) ? null : val; + if (value.endsWith('%')) { + value = percent2float(value); + } else { + value = +value; + } + if (isNaN(value)) { + this.errors.push(`Value invalid - "${value}"`); + } else { + this.value = value; + } } + } else { + this.errors.push(`Value invalid - ${JSON.stringify(value)}`); } } }