@lodestar/params
Version:
Chain parameters required for lodestar
48 lines • 1.47 kB
JavaScript
import { beaconPresetTypes } from "./types.js";
/**
* Render BeaconPreset to JSON strings
* - Numbers: Render as a quoted decimal string
*/
export function presetToJson(preset) {
const json = {};
for (const key of Object.keys(preset)) {
json[key] = serializePresetValue(preset[key]);
}
return json;
}
/**
* Type Wrapper to ensure that all values of BeaconPreset are number.
* If there are more types, expand this function with a type switch
*/
function serializePresetValue(value) {
return value.toString(10);
}
/**
* Parse JSON strings of BeaconPreset
* - Numbers: Convert quoted decimal string to number
*/
export function presetFromJson(json) {
const beaconPreset = {};
for (const key of Object.keys(beaconPresetTypes)) {
const value = json[key];
if (value !== undefined) {
beaconPreset[key] = deserializePresetValue(value, key);
}
}
return beaconPreset;
}
/**
* Ensure that all values of parsed BeaconPreset are numbers
* If there are more types, expand this function with a type switch
*/
function deserializePresetValue(valueStr, keyName) {
if (typeof valueStr !== "string") {
throw Error(`Invalid ${keyName} value ${valueStr} expected string`);
}
const value = parseInt(valueStr, 10);
if (Number.isNaN(value)) {
throw Error(`Invalid ${keyName} value ${valueStr} expected number`);
}
return value;
}
//# sourceMappingURL=json.js.map