@adpt/core
Version:
AdaptJS core library
59 lines • 2.51 kB
JavaScript
;
/*
* Copyright 2018-2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const utils_1 = require("@adpt/utils");
const ld = tslib_1.__importStar(require("lodash"));
const serialize_1 = require("../observers/serialize");
function isObject(x) { return ld.isObject(x); }
function hasPreparedFullObservationsShape(candidate) {
if (!isObject(candidate))
return false;
const plugin = candidate.plugin;
const observer = candidate.observer;
if (plugin && !isObject(plugin))
return false;
if (observer)
return hasPreparedFullObservationsShape(observer);
return true;
}
exports.hasPreparedFullObservationsShape = hasPreparedFullObservationsShape;
function parseFullObservationsJson(json) {
const candidate = JSON.parse(json);
if (!hasPreparedFullObservationsShape(candidate)) {
if (!isObject(candidate))
throw new Error("Full observations is not an object");
if (candidate.observer) {
serialize_1.reconstituteAllObservations(candidate.observer); //Should throw better error than below
}
throw new Error("Illegal shape for full observations"); //Otherwise generic error
}
const observer = candidate.observer ? serialize_1.reconstituteAllObservations(candidate.observer) : undefined;
const plugin = candidate.plugin;
return utils_1.removeUndef({ plugin, observer });
}
exports.parseFullObservationsJson = parseFullObservationsJson;
function stringifyFullObservations(observations) {
const observer = observations.observer;
const prepped = {
plugin: observations.plugin,
observer: observer ? serialize_1.prepareAllObservationsForJson(observer) : observer
};
return JSON.stringify(utils_1.removeUndef(prepped));
}
exports.stringifyFullObservations = stringifyFullObservations;
//# sourceMappingURL=serialize.js.map