@rxap/xml-parser
Version:
Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into
48 lines • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeValue = serializeValue;
/**
* Serializes a value into a string representation.
*
* This function takes a JavaScript value and converts it into a string representation suitable for
* parsing back using `parseValue`. It handles boolean, null, undefined, numbers, strings, JSON arrays,
* and JSON objects.
*
* @param {any} value - The value to be serialized.
* @returns {string} - The string representation of the value:
* - Returns 'true' or 'false' for boolean values.
* - Returns 'null' for null.
* - Returns 'undefined' for undefined.
* - Returns the string representation of a number.
* - Returns a quoted string for strings.
* - Returns JSON string for arrays and objects.
*/
function serializeValue(value) {
if (typeof value === 'boolean') {
return value.toString();
}
if (value === null) {
return 'null';
}
if (value === undefined) {
return '';
}
if (typeof value === 'number') {
return value.toString();
}
if (typeof value === 'string') {
// Escape double quotes for safety in string representation
return value.replace(/"/g, '\\"');
}
if (Array.isArray(value) || typeof value === 'object') {
try {
// Attempt to JSON stringify arrays and plain objects
return JSON.stringify(value);
}
catch (e) {
console.error('Could not serialize value: ' + e.message, value);
}
}
return String(value);
}
//# sourceMappingURL=serialize-value.js.map