vcard4
Version:
An RFC 6350 compliant JavaScript library for generating and parsing version 4.0 vCards. Can also generate RFC 6351 compliant XML vCards and RFC 7095 compliant jCards. TypeScript type declarations are provided.
64 lines (53 loc) • 1.88 kB
JavaScript
import { BaseValue } from "./BaseValue.js";
import { MissingArgument, InvalidArgument } from "../errors/index.js";
export class IntegerType extends BaseValue {
static type = "INTEGER";
static identifier = "IntegerType";
#intValue;
#validate(intValue) {
if (typeof intValue === "undefined")
throw new MissingArgument("Value for IntegerType must be supplied");
else if (typeof intValue !== "number" && typeof intValue !== "bigint")
throw new TypeError(
"Value for IntegerType must be of type number or bigint"
);
else if (/\./.test(intValue.toString()))
throw new InvalidArgument("Invalid value for IntegerType");
else if (
typeof intValue === "number" &&
!(
-Number.MAX_SAFE_INTEGER < intValue &&
intValue < Number.MAX_SAFE_INTEGER
)
)
throw new InvalidArgument(
`The maximum value is ${Number.MAX_SAFE_INTEGER}, and the minimum value is ${Number.MIN_SAFE_INTEGER} for number IntegerType`
);
else if (
typeof intValue === "bigint" &&
!(-9223372036854775809n < intValue && intValue < 9223372036854775808n)
)
throw new InvalidArgument(
"The maximum value is 9223372036854775807n, and the minimum value is -9223372036854775808n for bigint IntegerType"
);
}
get value() {
return `${this.#intValue}`;
}
get valueXML() {
return `<${this.constructor.type.toLowerCase()}>${
this.#intValue
}</${this.constructor.type.toLowerCase()}>`;
}
get valueJSON() {
return [this.constructor.type.toLowerCase(), this.#intValue];
}
constructor(intValue) {
super();
this.#validate(intValue);
this.#intValue = intValue;
this.checkAbstractPropertiesAndMethods();
Object.freeze(this);
}
}
Object.freeze(IntegerType);