Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function ULong(value, unsigned = true, radix = 10) {
if(typeof value === 'number') {
// Some JSON libs use numbers for values under 53 bits or strings for larger.
// Accomidate but double-check it..
if(value > Number.MAX_SAFE_INTEGER)
throw new TypeError('value parameter overflow')
value = Long.fromString(String(value), unsigned, radix)
} else if(typeof value === 'string') {
value = Long.fromString(value, unsigned, radix)
} else if(!Long.isLong(value)) {
throw new TypeError('value parameter is a requied Long, Number or String')
}
return value
}
no_overflow53(value, field_name=""){
if (typeof value === "number") {
if (value > MAX_SAFE_INT || value < MIN_SAFE_INT) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
if (typeof value === "string") {
var int = parseInt(value);
if (value > MAX_SAFE_INT || value < MIN_SAFE_INT) {
throw new Error(`overflow ${field_name} ${value}`);
}
return;
}
if (Long.isLong(value) ){
// typeof value.toInt() is 'number'
this.no_overflow53(value.toInt(), field_name);
return;
}
throw `unsupported type ${field_name}: (${typeof value}) ${value}`;
},
to_long(value, field_name = "", unsigned = false) {
if (this.is_empty(value)) {
return value;
}
if (Long.isLong(value)) {
return value;
}
this.no_overflow64(value, field_name, unsigned);
if (typeof value === "number") {
value = "" + value;
}
return Long.fromString(value, unsigned);
},
require_long(value, field_name=""){
if (!Long.isLong(value)) {
throw new Error(`Long value required ${field_name} ${value}`);
}
return value;
},
to_string(value, field_name = '') {
if (this.is_empty(value)) {
return value;
}
if (typeof value === 'string') {
return value;
}
if (typeof value === 'number') {
this.no_overflow53(value, field_name);
return `${value}`;
}
if (Long.isLong(value)) {
return value.toString();
}
throw new Error(`unsupported type ${field_name}: (${typeof value}) ${value}`);
},
require_long(value, field_name=""){
if (!Long.isLong(value)) {
throw new Error(`Long value required ${field_name} ${value}`);
}
return value;
},
to_long(value, field_name=""){
if (is_empty(value) ){ return value; }
if (Long.isLong(value) ){ return value; }
_my.no_overflow64(value, field_name);
if (typeof value === "number") {
value = ""+value;
}
return Long.fromString(value);
},
require_long(value, field_name = "") {
if (!Long.isLong(value)) {
throw new Error(`Long value required ${field_name} ${value}`);
}
return value;
},
require_long(value, field_name = '') {
if (!Long.isLong(value)) {
throw new Error(`Long value required ${field_name} | ${value}`);
}
return value;
},