react-application-core
Version:
A react-based application core for the business applications.
253 lines • 9.34 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberConverter = void 0;
var R = require("ramda");
var inversify_1 = require("inversify");
var di_1 = require("../../di");
var util_1 = require("../../util");
var definitions_interface_1 = require("../../definitions.interface");
var NumberConverter = /** @class */ (function () {
/**
* @stable [22.10.2018]
*/
function NumberConverter() {
this.defaultFormatter = new Intl.NumberFormat();
this.integerFormatter = new Intl.NumberFormat(this.locale, this.integerFormatOptions);
this.fractionalFormatter = new Intl.NumberFormat(this.locale, this.fractionalFormatOptions);
this.format = this.format.bind(this);
this.formatFractional = this.formatFractional.bind(this);
this.formatFractionalCurrency = this.formatFractionalCurrency.bind(this);
this.formatInteger = this.formatInteger.bind(this);
this.formatIntegerCurrency = this.formatIntegerCurrency.bind(this);
this.id = this.id.bind(this);
/* @stable [09.09.2020] */
this.integerCurrencyFormatter = new Intl.NumberFormat(this.locale, __assign(__assign({}, this.currencyFormatOptions), this.integerFormatOptions));
/* @stable [09.09.2020] */
this.fractionalCurrencyFormatter = new Intl.NumberFormat(this.locale, __assign(__assign({}, this.currencyFormatOptions), this.fractionalFormatOptions));
}
/**
* @stable [09.09.2020]
* @param value
* @param radix
*/
NumberConverter.prototype.integer = function (value, radix) {
if (radix === void 0) { radix = 10; }
if (util_1.TypeUtils.isNumber(value)) {
return value;
}
var vAsString = value;
if (R.isNil(vAsString)) {
return value;
}
var result = parseInt(vAsString, radix);
return isNaN(result) ? value : result;
};
/**
* @stable [09.09.2020]
* @param value
* @param returnString
*/
NumberConverter.prototype.number = function (value, returnString) {
if (returnString === void 0) { returnString = true; }
if (util_1.TypeUtils.isNumber(value)) {
return value;
}
var vAsString = value;
if (R.isNil(vAsString)) {
return value;
}
var normalizedValue = vAsString.startsWith('-.')
? "-0" + vAsString.substring(1, vAsString.length)
: (vAsString.startsWith('.')
? "0" + vAsString
: vAsString);
var result = parseFloat(normalizedValue);
return (isNaN(result) || (!R.equals(String(result), normalizedValue) && returnString))
? normalizedValue
: result;
};
/**
* @stable [09.09.2020]
* @param value
*/
NumberConverter.prototype.asNumber = function (value) {
return this.number(value, false);
};
/**
* @stable [29.08.2019]
* @param {StringNumberT} value
* @param {(value: number) => number} converter
* @returns {number}
*/
NumberConverter.prototype.numberParameter = function (value, converter) {
if (util_1.TypeUtils.isUndef(value)) {
return definitions_interface_1.UNDEF;
}
if (value === null) {
return null;
}
if (util_1.TypeUtils.isString(value) && R.isEmpty(value.trim())) {
return null;
}
var v = this.asNumber(value);
return util_1.TypeUtils.isFn(converter) ? converter(v) : v;
};
/**
* @stable [09.09.2020]
* @param value
* @param formatter
* @param options
*/
NumberConverter.prototype.format = function (value, formatter, options) {
if (formatter === void 0) { formatter = this.defaultFormatter; }
return this.doFormat(value, formatter, options);
};
/**
* @stable [09.09.2020]
* @param value
* @param options
*/
NumberConverter.prototype.formatInteger = function (value, options) {
return this.doFormat(value, this.integerFormatter, options);
};
/**
* @stable [09.09.2020]
* @param value
* @param options
*/
NumberConverter.prototype.formatFractional = function (value, options) {
return this.doFormat(value, this.fractionalFormatter, options);
};
/**
* @stable [09.09.2020]
* @param value
* @param options
*/
NumberConverter.prototype.formatIntegerCurrency = function (value, options) {
return this.doFormat(value, this.integerCurrencyFormatter, options);
};
/**
* @stable [09.09.2020]
* @param value
* @param options
*/
NumberConverter.prototype.formatFractionalCurrency = function (value, options) {
return this.doFormat(value, this.fractionalCurrencyFormatter, options);
};
/**
* @stable [09.09.2020]
* @param value
*/
NumberConverter.prototype.id = function (value) {
return "#" + value;
};
/**
* @stable [09.09.2020]
* @param value
*/
NumberConverter.prototype.internalId = function (value) {
return "(#" + value + ")";
};
/**
* @stable [19.12.2020]
* @param value
*/
NumberConverter.prototype.currencyValue = function (value) {
var currency = this.settings.currency;
// {currency} | {shortCurrency}
return currency
.currencyValueTemplate
.replace('{value}', String(value))
.replace('{currency}', currency.currency)
.replace('{shortCurrency}', currency.shortCurrency);
};
/**
* @stable [09.09.2020]
* @param value
* @param formatter
* @param options
* @private
*/
NumberConverter.prototype.doFormat = function (value, formatter, options) {
return R.isNil(value) || !util_1.TypeUtils.isPositiveOrNegativeNumberLike(value)
? this.settings.numberConverter.na
: (R.isNil(options)
? formatter.format(this.asNumber(value))
: (new Intl.NumberFormat(this.locale, __assign(__assign({}, formatter.resolvedOptions()), options)).format(this.asNumber(value))));
};
Object.defineProperty(NumberConverter.prototype, "currencyFormatOptions", {
/**
* @stable [09.09.2020]
* @private
*/
get: function () {
return { style: 'currency', currency: this.settings.currency.currency };
},
enumerable: false,
configurable: true
});
Object.defineProperty(NumberConverter.prototype, "integerFormatOptions", {
/**
* @stable [09.09.2020]
* @private
*/
get: function () {
return this.settings.currency.integerFormatOptions;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NumberConverter.prototype, "fractionalFormatOptions", {
/**
* @stable [09.09.2020]
* @private
*/
get: function () {
return this.settings.currency.fractionalFormatOptions;
},
enumerable: false,
configurable: true
});
Object.defineProperty(NumberConverter.prototype, "locale", {
/**
* @stable [09.09.2020]
* @private
*/
get: function () {
return this.settings.currency.locale;
},
enumerable: false,
configurable: true
});
__decorate([
di_1.lazyInject(di_1.DI_TYPES.Settings),
__metadata("design:type", Object)
], NumberConverter.prototype, "settings", void 0);
NumberConverter = __decorate([
inversify_1.injectable(),
__metadata("design:paramtypes", [])
], NumberConverter);
return NumberConverter;
}());
exports.NumberConverter = NumberConverter;
//# sourceMappingURL=number-converter.service.js.map