@contrail/types
Advanced tools
Comparing version 3.0.72-alpha.0 to 3.0.72-alpha.1
@@ -23,2 +23,4 @@ import { DateFormatingOptions, NumberFormatingOptions, TypeProperty } from '../type-properties'; | ||
formatUserList(object: any): any; | ||
formatValueWithPrecision(value: any, property: TypeProperty): number | null; | ||
private hasPrecisionToEnforceOnProperty; | ||
parseValue(value: any, typeProperty: TypeProperty): any; | ||
@@ -25,0 +27,0 @@ parseNumberValue(value: any): number; |
@@ -175,2 +175,30 @@ "use strict"; | ||
} | ||
formatValueWithPrecision(value, property) { | ||
const isEmpty = Boolean(value === undefined || value === null || (typeof value === 'number' && Number.isNaN(value))); | ||
if (isEmpty) { | ||
return null; | ||
} | ||
if (!this.hasPrecisionToEnforceOnProperty(value, property)) { | ||
return value; | ||
} | ||
const precision = property.numberFormat?.precision; | ||
if (property.numberFormat?.format === type_properties_1.NumberFormat.Percent) { | ||
const percentAdjustedPrecision = precision + 2; | ||
return parseFloat(value.toFixed(percentAdjustedPrecision)); | ||
} | ||
return parseFloat(value.toFixed(precision)); | ||
} | ||
hasPrecisionToEnforceOnProperty(value, property) { | ||
const precision = property.numberFormat?.precision; | ||
const numericPropertyTypes = [ | ||
type_properties_1.PropertyType.Currency, | ||
type_properties_1.PropertyType.Number, | ||
type_properties_1.PropertyType.Percent, | ||
type_properties_1.PropertyType.Formula, | ||
]; | ||
return Boolean(property.propertyType && | ||
numericPropertyTypes.includes(property.propertyType) && | ||
typeof precision === 'number' && | ||
typeof value === 'number'); | ||
} | ||
parseValue(value, typeProperty) { | ||
@@ -177,0 +205,0 @@ let parsedValue = value; |
@@ -10,4 +10,2 @@ import { TypeProperty } from '../type-properties'; | ||
static buildFunctionFromString(functionString: string): any; | ||
private static formatValueForProperty; | ||
private static hasPrecisionToEnforceOnProperty; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FormulaFunctionProcessor = void 0; | ||
const type_properties_1 = require("../type-properties"); | ||
const formatter_1 = require("../formatter"); | ||
const formula_order_helper_1 = require("./formula-order-helper"); | ||
@@ -13,5 +13,6 @@ class FormulaFunctionProcessor { | ||
const formulaPropertiesInOrder = (0, formula_order_helper_1.getExecutionOrder)(formulaProperties); | ||
const formatter = new formatter_1.PropertyValueFormatter(); | ||
for (const property of formulaPropertiesInOrder) { | ||
const formulaValue = await this.processFormulaFunction(property, entity, context); | ||
const formattedValue = this.formatValueForProperty(formulaValue, property); | ||
const formulaValue = await FormulaFunctionProcessor.processFormulaFunction(property, entity, context); | ||
const formattedValue = formatter.formatValueWithPrecision(formulaValue, property); | ||
entity[property.slug] = formattedValue; | ||
@@ -34,3 +35,3 @@ } | ||
entities.forEach((entity) => { | ||
this.processFormulaFunctionsForEntity(entity, properties); | ||
FormulaFunctionProcessor.processFormulaFunctionsForEntity(entity, properties); | ||
}); | ||
@@ -63,34 +64,4 @@ } | ||
} | ||
static formatValueForProperty(value, property) { | ||
const isEmpty = Boolean(value === undefined || value === null || (typeof value === 'number' && Number.isNaN(value))); | ||
if (isEmpty) { | ||
return null; | ||
} | ||
if (FormulaFunctionProcessor.hasPrecisionToEnforceOnProperty(value, property)) { | ||
const precision = property.numberFormat?.precision; | ||
if (property.numberFormat?.format === type_properties_1.NumberFormat.Percent) { | ||
const percentAdjustedPrecision = precision + 2; | ||
return parseFloat(value.toFixed(percentAdjustedPrecision)); | ||
} | ||
else { | ||
return parseFloat(value.toFixed(precision)); | ||
} | ||
} | ||
return value; | ||
} | ||
static hasPrecisionToEnforceOnProperty(value, property) { | ||
const precision = property.numberFormat?.precision; | ||
const numericPropertyTypes = [ | ||
type_properties_1.PropertyType.Currency, | ||
type_properties_1.PropertyType.Number, | ||
type_properties_1.PropertyType.Percent, | ||
type_properties_1.PropertyType.Formula, | ||
]; | ||
return Boolean(property.propertyType && | ||
numericPropertyTypes.includes(property.propertyType) && | ||
typeof precision === 'number' && | ||
typeof value === 'number'); | ||
} | ||
} | ||
exports.FormulaFunctionProcessor = FormulaFunctionProcessor; | ||
FormulaFunctionProcessor.localFunctionCache = new Map(); |
import { TypeProperty } from '../type-properties'; | ||
export declare class FormulaProcessor { | ||
static processFormula(formula: string | undefined, data: any): number | null; | ||
static processFormulaForProperty(property: TypeProperty, data: any): number | null; | ||
static processFormulasForEntities(entities: Array<any>, properties: Array<TypeProperty>): void; | ||
static processFormulasForEntity(entity: any, properties: Array<TypeProperty>): void; | ||
} |
@@ -7,4 +7,6 @@ "use strict"; | ||
const formula_order_helper_1 = require("./formula-order-helper"); | ||
const formatter_1 = require("../formatter"); | ||
class FormulaProcessor { | ||
static processFormula(formula = '', data) { | ||
static processFormulaForProperty(property, data) { | ||
const formula = property.formula ?? ''; | ||
const converted = util_1.StringUtil.parseVariables(formula, data); | ||
@@ -15,3 +17,4 @@ const value = parseFloat(eval(converted)); | ||
} | ||
return value; | ||
const formattedValue = new formatter_1.PropertyValueFormatter().formatValueWithPrecision(value, property); | ||
return formattedValue; | ||
} | ||
@@ -27,3 +30,3 @@ static processFormulasForEntities(entities, properties) { | ||
formulaPropertiesInOrder.forEach((property) => { | ||
entity[property.slug] = this.processFormula(property.formula, entity); | ||
entity[property.slug] = this.processFormulaForProperty(property, entity); | ||
}); | ||
@@ -30,0 +33,0 @@ } |
@@ -5,3 +5,3 @@ "use strict"; | ||
console.log('Start test'); | ||
const formula = '{retailPrice}*{volume}-{retailPrice}'; | ||
const property = { slug: 'computed', formula: '{retailPrice}*{volume}-{retailPrice}' }; | ||
const data = { | ||
@@ -11,3 +11,3 @@ retailPrice: 50, | ||
}; | ||
const value = formula_processor_1.FormulaProcessor.processFormula(formula, data); | ||
const value = formula_processor_1.FormulaProcessor.processFormulaForProperty(property, data); | ||
console.log('value: ', value); |
{ | ||
"name": "@contrail/types", | ||
"version": "3.0.72-alpha.0", | ||
"version": "3.0.72-alpha.1", | ||
"description": "Types Utility module", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82607
1932