@freemework/common
Version:
Common library of the Freemework Project.
259 lines • 8.95 kB
JavaScript
import { FException } from "../exception/index.js";
import { FDecimal } from "../primitive/index.js";
export class FConfigurationValue {
/**
* Factory constructor
*/
static factory(key, value, sourceURI, overriden) {
return new _FConfigurationValue(key, value, sourceURI, overriden);
}
}
import { FConfigurationValueException } from "./f_configuration_value_exception.js";
class _FConfigurationValue extends FConfigurationValue {
_key;
_value;
_sourceURI;
_overridden;
get sourceURI() { return this._sourceURI; }
get overridden() { return this._overridden; }
get key() { return this._key; }
get isNull() { return this._value === null; }
get asBase64() {
this.assertNotNullValue(this._value, "asBase64");
return this.fromBase64(this._value);
}
get asBase64Nullable() {
if (this._value === null) {
return null;
}
return this.fromBase64(this._value);
}
get asBoolean() {
this.assertNotNullValue(this._value, "asBoolean");
return this.fromBoolean(this._value);
}
get asBooleanNullable() {
if (this._value === null) {
return null;
}
return this.fromBoolean(this._value);
}
get asDateIso8601() {
this.assertNotNullValue(this._value, "asDateIso8601");
return this.fromDateIso8601(this._value);
}
get asDateIso8601Nullable() {
if (this._value === null) {
return null;
}
return this.fromDateIso8601(this._value);
}
get asDateTimestamp() {
this.assertNotNullValue(this._value, "asDateTimestamp");
return this.fromDateTimestamp(this._value);
}
get asDateTimestampNullable() {
if (this._value === null) {
return null;
}
return this.fromDateTimestamp(this._value);
}
get asDecimal() {
this.assertNotNullValue(this._value, "asDecimal");
return this.fromDecimal(this._value);
}
get asDecimalNullable() {
if (this._value === null) {
return null;
}
return this.fromDecimal(this._value);
}
get asInteger() {
this.assertNotNullValue(this._value, "asInteger");
return this.fromInteger(this._value);
}
get asIntegerNullable() {
if (this._value === null) {
return null;
}
return this.fromInteger(this._value);
}
get asIntegerNegative() {
this.assertNotNullValue(this._value, "asIntegerNegative");
return this.fromIntegerNegative(this._value);
}
get asIntegerNegativeNullable() {
if (this._value === null) {
return null;
}
return this.fromIntegerNegative(this._value);
}
get asIntegerPositive() {
this.assertNotNullValue(this._value, "asIntegerPositive");
return this.fromIntegerPositive(this._value);
}
get asIntegerPositiveNullable() {
if (this._value === null) {
return null;
}
return this.fromIntegerPositive(this._value);
}
get asNumber() {
this.assertNotNullValue(this._value, "asNumber");
return this.fromNumber(this._value);
}
get asNumberNullable() {
if (this._value === null) {
return null;
}
return this.fromNumber(this._value);
}
get asPortNumber() {
this.assertNotNullValue(this._value, "asPortNumber");
return this.fromPortNumber(this._value);
}
get asPortNumberNullable() {
if (this._value === null) {
return null;
}
return this.fromPortNumber(this._value);
}
get asString() {
this.assertNotNullValue(this._value, "asString");
return this.fromString(this._value);
}
get asStringNullable() {
if (this._value === null) {
return null;
}
return this.fromString(this._value);
}
get asUrl() {
this.assertNotNullValue(this._value, "asUrl");
return this.fromUrl(this._value);
}
get asUrlNullable() {
if (this._value === null) {
return null;
}
return this.fromUrl(this._value);
}
constructor(_key, _value, _sourceURI, _overridden) {
super();
this._key = _key;
this._value = _value;
this._sourceURI = _sourceURI;
this._overridden = _overridden;
}
toJSON() {
return this._value;
}
assertNotNullValue(value, callerProperty) {
if (value === null) {
throw new FConfigurationValueException(this, `Cannot represent null value ${callerProperty}`, this.key);
}
}
fromBase64(value) {
const parsedData = Buffer.from(value, "base64");
const restoredValue = parsedData.toString("base64");
if (restoredValue !== value) {
const partOfValue = value.slice(0, 4);
const maskValue = `${partOfValue}...`;
throw new FConfigurationValueException(this, `Cannot parse value '${maskValue}' as base64.`, this.key);
}
return parsedData;
}
fromBoolean(value) {
const lowerCaseValue = value.toLowerCase();
switch (lowerCaseValue) {
case "true":
case "enabled":
case "yes":
return true;
case "false":
case "disabled":
case "no":
return false;
default:
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to boolean type.`, this.key);
}
}
fromDateIso8601(value) {
const date = new Date(value);
if (date.toString() === "Invalid Date") {
throw new FConfigurationValueException(this, `Cannot parse value '${value}' as Date ISO8601. Invalid Date.`, this.key);
}
return date;
}
fromDateTimestamp(value) {
const friendlyValue = Number.parseInt(value, 10);
if (friendlyValue.toString() !== value) {
throw new FConfigurationValueException(this, `Cannot parse value '${value}' as Date timestamp. Unparsable timestamp.`, this.key);
}
const date = new Date(friendlyValue);
if (date.toString() === "Invalid Date") {
throw new FConfigurationValueException(this, `Cannot parse value '${value}' as Date timestamp. Invalid Date.`, this.key);
}
return date;
}
fromDecimal(value) {
try {
return FDecimal.parse(value);
}
catch (e) {
throw new FConfigurationValueException(this, `Cannot parse value '${value}' as FDecimal.`, this.key, FException.wrapIfNeeded(e));
}
}
fromInteger(value) {
const friendlyValue = Number.parseInt(value, 10);
if (friendlyValue.toString() === value) {
return friendlyValue;
}
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to integer type.`, this.key);
}
fromIntegerNegative(value) {
const friendlyValue = this.fromInteger(value);
if (friendlyValue < 0) {
return friendlyValue;
}
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to integer negative type.`, this.key);
}
fromIntegerPositive(value) {
const friendlyValue = this.fromInteger(value);
if (friendlyValue > 0) {
return friendlyValue;
}
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to integer positive type.`, this.key);
}
fromNumber(value) {
const friendlyValue = Number.parseFloat(value);
if (friendlyValue.toString() === value) {
return friendlyValue;
}
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to float type.`, this.key);
}
fromPortNumber(value) {
const friendlyValue = Number.parseInt(value);
if (friendlyValue.toString() !== value) {
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to TCP port number.`, this.key);
}
if (friendlyValue < 0 || friendlyValue > 65535) {
throw new FConfigurationValueException(this, `Cannot convert the value '${value}' to TCP port number. Value out of range 0 - 65535.`, this.key);
}
return friendlyValue;
}
fromString(value) {
return value;
}
fromUrl(value) {
try {
return new URL(value);
}
catch (e) {
const partOfValue = value.slice(0, 4);
const maskValue = `${partOfValue}...`;
throw new FConfigurationValueException(this, `Cannot parse value '${maskValue}' as URL.`, this.key, FException.wrapIfNeeded(e));
}
}
}
//# sourceMappingURL=f_configuration_value.js.map