@mos-connection/helper
Version:
Helper functions for the MOS-connection library
175 lines • 5.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isXMLObject = exports.isSingularArray = exports.isSingular = exports.ensureXMLObjectArray = exports.ensureSingularArray = exports.ensureSingular = exports.ensureXMLObject = exports.ensureString = exports.ensureStringLiteral = exports.ensureArray = void 0;
const lib_1 = require("../mosModel/lib");
function ensureArray(v) {
if (typeof v === 'object' && Array.isArray(v))
return v;
else
return [v];
}
exports.ensureArray = ensureArray;
/**
* Ensures that the returned value is a string literal.
* If the input value is not of the correct type, will throw (if strict) or return the fallback value.
*/
function ensureStringLiteral(xmlValue, options, strict, fallback) {
const value = ensureSingular(xmlValue, strict);
if (!value) {
if (strict) {
throw new Error(`Expected a string, got: "${value}"`);
}
else {
return fallback;
}
}
if ((0, lib_1.assertStringLiteral)(value, options)) {
return value;
}
else if (strict) {
throw new Error(`Invalid literal value: "${value}" (valid values: ${options.join(', ')})`);
}
else {
return fallback;
}
}
exports.ensureStringLiteral = ensureStringLiteral;
/**
* Ensures that the returned value is a string.
* If the input value is not of the correct type, will throw (if strict) or return the fallback value.
*/
function ensureString(value, strict, fallback = '') {
if (typeof value === 'string') {
return value;
}
else if (strict)
throw new Error(`Expected a string, got: ${JSON.stringify(value)}`);
else
return fallback;
}
exports.ensureString = ensureString;
/**
* Ensures that the returned value is an object.
* If the input value is not of the correct type, will throw (if strict) or return the fallback value.
*/
function ensureXMLObject(value, strict, fallback = {}) {
if (typeof value === 'object') {
if (Array.isArray(value)) {
if (value.length === 0)
return {};
if (value.length > 1) {
if (strict)
throw new Error(`Expected only one value, got ${value.length} values: ${JSON.stringify(value)}`);
else
return fallback;
}
return ensureXMLObject(value[0], strict);
}
else {
return value;
}
}
else if (strict)
throw new Error(`Expected an object, got: ${value}`);
else
return fallback;
}
exports.ensureXMLObject = ensureXMLObject;
/**
* Ensures that the returned value is a singular value (ie a string or undefined).
* If the input value is not of the correct type, will throw (if strict) or return the fallback value.
*/
function ensureSingular(value, strict) {
// Quick-fix if it is in a xml element:
if (isXMLTextElement(value))
value = value.text;
// Quick-fix for empty object
if ((0, lib_1.isEmpty)(value))
value = '';
if (typeof value === 'object') {
if (Array.isArray(value)) {
if (value.length === 0)
return undefined;
if (value.length > 1) {
if (strict)
throw new Error(`Expected only one value, got ${value.length} values: ${JSON.stringify(value)}`);
else
return undefined;
}
return ensureSingular(value[0], strict);
}
else if (strict)
throw new Error(`Expected only one value, got object: ${JSON.stringify(value)}`);
else
return undefined;
}
else {
return value;
}
}
exports.ensureSingular = ensureSingular;
/**
* Ensures that the returned value is an array containing only singular values
* If the input value is not of the correct type, will throw (if strict) or return an empty array
*/
function ensureSingularArray(value, strict) {
if (typeof value === 'object' && Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = ensureSingular(value[i], strict);
}
return value;
}
else if (strict)
throw new Error(`Expected an Array, got: ${JSON.stringify(value)}`);
else
return [];
}
exports.ensureSingularArray = ensureSingularArray;
/**
* Ensures that the returned value is an array containing only XMLObjects
* If the input value is not of the correct type, will throw (if strict) or return an empty array
*/
function ensureXMLObjectArray(value, strict) {
const objs = [];
for (const obj of ensureArray(value)) {
objs.push(ensureXMLObject(obj, strict));
}
return objs;
}
exports.ensureXMLObjectArray = ensureXMLObjectArray;
function isSingular(value) {
try {
ensureSingular(value, true);
return true;
}
catch {
return false;
}
}
exports.isSingular = isSingular;
function isSingularArray(value) {
try {
ensureSingularArray(value, true);
return true;
}
catch {
return false;
}
}
exports.isSingularArray = isSingularArray;
function isXMLObject(value) {
try {
ensureXMLObject(value, true);
return true;
}
catch {
return false;
}
}
exports.isXMLObject = isXMLObject;
function isXMLTextElement(xml) {
return (typeof xml === 'object' &&
xml.$type === 'text' &&
typeof xml.text === 'string');
}
//# sourceMappingURL=ensureMethods.js.map