@opra/common
Version:
Opra common package
37 lines (36 loc) • 1.09 kB
JavaScript
import { FilterValidationError } from '../../errors.js';
import { Literal } from '../abstract/literal.js';
export class NumberLiteral extends Literal {
value;
constructor(value) {
super(0);
if (typeof value === 'number' || typeof value === 'bigint') {
this.value = value;
return;
}
try {
// noinspection SuspiciousTypeOfGuard
if (typeof value === 'string') {
if (value.includes('.')) {
this.value = parseFloat(value);
return;
}
const n = Number(value);
if ('' + n === value)
this.value = n;
else
this.value = BigInt(value);
return;
}
}
catch {
//
}
throw new FilterValidationError(`Invalid number literal ${value}`);
}
toString() {
return typeof this.value === 'bigint'
? ('' + this.value).replace(/n$/, '')
: '' + this.value;
}
}