@contrail/types
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -0,1 +1,2 @@ | ||
import { NumberFormatingOption, TypeProperty } from "../properties"; | ||
export interface LocalizationConfig { | ||
@@ -9,4 +10,6 @@ currencyCode: string; | ||
setLocalConfig(config: LocalizationConfig): void; | ||
formatValue(value: any, propertyType: any): string; | ||
getDisplayValue(obj: any, property: TypeProperty): string; | ||
formatValue(value: any, propertyType: any, numberFormat?: NumberFormatingOption): string; | ||
private formatNumber; | ||
formatCurrencyValue(value: any): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PropertyValueFormatter = void 0; | ||
const properties_1 = require("../properties"); | ||
class PropertyValueFormatter { | ||
@@ -17,9 +18,24 @@ constructor() { | ||
} | ||
formatValue(value, propertyType) { | ||
if (propertyType === 'currency') { | ||
return this.formatCurrencyValue(value); | ||
getDisplayValue(obj, property) { | ||
if (!obj || !property || !obj[property.slug]) { | ||
return ''; | ||
} | ||
return value; | ||
let value = obj[property.slug]; | ||
return this.formatValue(value, property.propertyType, property.numberFormat); | ||
} | ||
formatCurrencyValue(value) { | ||
formatValue(value, propertyType, numberFormat) { | ||
let formattedValue = value; | ||
switch (propertyType) { | ||
case properties_1.PropertyType.Currency: { | ||
formattedValue = this.formatNumber(value, numberFormat || { format: properties_1.NumberFormat.Currency }); | ||
break; | ||
} | ||
case properties_1.PropertyType.Formula: { | ||
formattedValue = this.formatNumber(value, numberFormat || { format: properties_1.NumberFormat.Decimal }); | ||
break; | ||
} | ||
} | ||
return formattedValue; | ||
} | ||
formatNumber(value, numberFormat = { format: properties_1.NumberFormat.Decimal }) { | ||
if (!value) { | ||
@@ -29,8 +45,20 @@ return value; | ||
let val = value; | ||
if (value && value[this.localizationConfig.currencyCode]) { | ||
val = value[this.localizationConfig.currencyCode]; | ||
const options = {}; | ||
if (numberFormat.precision !== null) { | ||
options.maximumFractionDigits = numberFormat.precision; | ||
options.minimumFractionDigits = numberFormat.precision; | ||
} | ||
return new Intl.NumberFormat(this.localizationConfig.locale, { style: 'currency', currency: this.localizationConfig.currencyCode }).format(val); | ||
if (numberFormat.format = properties_1.NumberFormat.Currency) { | ||
options.style = 'currency'; | ||
options.currency = this.localizationConfig.currencyCode; | ||
if (value && value[this.localizationConfig.currencyCode]) { | ||
val = value[this.localizationConfig.currencyCode]; | ||
} | ||
} | ||
return new Intl.NumberFormat(this.localizationConfig.locale, options).format(val); | ||
} | ||
formatCurrencyValue(value) { | ||
return this.formatNumber(value, { format: properties_1.NumberFormat.Currency }); | ||
} | ||
} | ||
exports.PropertyValueFormatter = PropertyValueFormatter; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const mockRequestFunction = jest.fn().mockResolvedValue({}); | ||
const properties_1 = require("../properties"); | ||
const property_value_formatter_1 = require("./property-value-formatter"); | ||
@@ -13,4 +14,3 @@ describe('Formatter', () => { | ||
objValFormatter.setLocalConfig(localizationConfig); | ||
const val = objValFormatter.formatValue('Black', 'string'); | ||
console.log(` formatted value ${val}`); | ||
const val = objValFormatter.formatValue('Black', properties_1.PropertyType.String); | ||
expect(val).toEqual("Black"); | ||
@@ -28,4 +28,3 @@ }); | ||
}; | ||
const val = objValFormatter.formatValue(data, 'currency'); | ||
console.log(` formatted value ${val}`); | ||
const val = objValFormatter.formatValue(data, properties_1.PropertyType.Currency); | ||
expect(val).toEqual("$54.00"); | ||
@@ -40,3 +39,3 @@ }); | ||
objValFormatter.setLocalConfig(localizationConfig); | ||
const val = objValFormatter.formatValue(45, 'currency'); | ||
const val = objValFormatter.formatValue(45, properties_1.PropertyType.Currency); | ||
expect(val).toEqual("$45.00"); | ||
@@ -51,5 +50,5 @@ }); | ||
objValFormatter.setLocalConfig(localizationConfig); | ||
const val = objValFormatter.formatValue(45, 'currency'); | ||
const val = objValFormatter.formatValue(45, properties_1.PropertyType.Currency); | ||
}); | ||
it('Japanese format currency values', () => { | ||
it('Japanese format currency values (no decimal - default)', () => { | ||
const objValFormatter = new property_value_formatter_1.PropertyValueFormatter(); | ||
@@ -61,5 +60,15 @@ const localizationConfig = { | ||
objValFormatter.setLocalConfig(localizationConfig); | ||
const val = objValFormatter.formatValue(75, 'currency'); | ||
const val = objValFormatter.formatValue(75, properties_1.PropertyType.Currency); | ||
expect(val).toEqual("¥75"); | ||
}); | ||
it('Japanese format currency values (with decimal - override)', () => { | ||
const objValFormatter = new property_value_formatter_1.PropertyValueFormatter(); | ||
const localizationConfig = { | ||
currencyCode: 'JPY', | ||
locale: 'ja-JP', | ||
}; | ||
objValFormatter.setLocalConfig(localizationConfig); | ||
const val = objValFormatter.formatValue(75, properties_1.PropertyType.Currency, { format: properties_1.NumberFormat.Currency, precision: 2 }); | ||
expect(val).toEqual("¥75.00"); | ||
}); | ||
}); |
@@ -7,3 +7,3 @@ import { PropertyType } from "."; | ||
formula?: string; | ||
formulaFormat?: NumberFormatingOption; | ||
numberFormat?: NumberFormatingOption; | ||
options?: Array<TypePropertyOption>; | ||
@@ -10,0 +10,0 @@ } |
{ | ||
"name": "@contrail/types", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"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
18931
28
451