UNPKG

@mos-connection/helper

Version:

Helper functions for the MOS-connection library

151 lines 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getXMLAttributes = exports.parseRequired = exports.parseOptional = exports.getParseMosTypes = void 0; const model_1 = require("@mos-connection/model"); const ParseError_1 = require("./ParseError"); const ensureMethods_1 = require("../utils/ensureMethods"); function getParseMosTypes(strict) { const mosTypes = (0, model_1.getMosTypes)(strict); const specialMosTypes = getSpecialMosTypes(strict); return { strict: mosTypes.strict, mosString128: wrapParseMethods(mosTypes.mosString128, true, strict), mosDuration: wrapParseMethods(mosTypes.mosDuration, true, strict), mosTime: wrapParseMethods(mosTypes.mosTime, true, strict), string: wrapParseMethods(specialMosTypes.string, true, strict), stringEnum: wrapParseMethods(specialMosTypes.stringEnum, false, strict), number: wrapParseMethods(specialMosTypes.number, true, strict), }; } exports.getParseMosTypes = getParseMosTypes; function wrapParseMethods(mosType, valueIsSingular, strict) { return { createOptional: wrapParseMethodCreateOptional(mosType, valueIsSingular, strict), createRequired: wrapParseMethodCreateRequired(mosType, valueIsSingular, strict), validate: mosType.validate, valueOf: mosType.valueOf, stringify: mosType.stringify, is: mosType.is, }; } function wrapParseMethodCreateOptional(mosType, valueIsSingular, strict) { return parseOptional(mosType.create, valueIsSingular, strict); } function wrapParseMethodCreateRequired(mosType, valueIsSingular, strict) { return parseRequired(mosType.create, mosType.fallback, valueIsSingular, strict); } function parseOptional(parser, valueIsSingular, strict) { return (value, path) => { try { // handle empty string: if (typeof value === 'string' && !value.trim()) value = undefined; // handle empty object (can happen when parsing an empty xml tag): if (typeof value === 'object' && Object.keys(value).length === 0) value = undefined; if (valueIsSingular) value = (0, ensureMethods_1.ensureSingular)(value, strict); if ((valueIsSingular && (value === undefined || value === '')) || (!valueIsSingular && (value.value === undefined || value.value === ''))) return undefined; return parser(value); } catch (e) { throw ParseError_1.ParseError.handleCaughtError(path, e); } }; } exports.parseOptional = parseOptional; function parseRequired(parser, fallback, valueIsSingular, strict) { return (value, path) => { try { // handle empty string: if (typeof value === 'string' && !value.trim()) value = undefined; // handle empty object (can happen when parsing an empty xml tag): if (typeof value === 'object' && Object.keys(value).length === 0) value = undefined; if (valueIsSingular) value = (0, ensureMethods_1.ensureSingular)(value, strict); if (value === undefined || value === '') { // Something might be wrong. value is undefined, but should not be (?) if (strict) { // This will throw if the mosType doesn't handle undefined: return parser(value); } else { return fallback(); } } else { return parser(value); } } catch (e) { throw ParseError_1.ParseError.handleCaughtError(path, e); } }; } exports.parseRequired = parseRequired; function getSpecialMosTypes(strict) { const string = { create: (anyValue) => { if (typeof anyValue !== 'string') throw new Error(`Expected a string, got: "${anyValue}"`); return anyValue; }, validate: (_value) => true, valueOf: (value) => value, stringify: (value) => value, is: (value) => typeof value !== 'string', fallback: () => '', }; const stringEnum = { create: (createValue) => { if (!createValue.enum) throw new Error(`Expected an object with an "enum" key, got: "${createValue}"`); const key = `${createValue.value}`; if (!key) throw new Error(`Expected a value, got: "${createValue.value}"`); const enumValues = Object.values(createValue.enum); if (!enumValues.includes(key)) { if (strict) { throw new Error(`Unknown value, got: "${key}", possible values: ${enumValues.join(', ')}`); } else return ''; } return key; }, validate: (_value) => true, valueOf: (value) => value, stringify: (value) => value, is: (value) => typeof value !== 'string', fallback: () => '', }; const number = { create: (anyValue) => { if (typeof anyValue !== 'string') throw new Error(`Expected a string, got: "${anyValue}"`); return parseFloat(anyValue); }, validate: (_value) => true, valueOf: (value) => value, stringify: (value) => `${value}`, is: (value) => typeof value !== 'number', fallback: () => 0, }; return { string, stringEnum, number, }; } function getXMLAttributes(obj) { if (obj.attributes && typeof obj.attributes === 'object' && !Array.isArray(obj.attributes)) return obj.attributes; else return {}; } exports.getXMLAttributes = getXMLAttributes; //# sourceMappingURL=parseMosTypes.js.map