新增调试信息
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
import { XmlElement } from "xmlcreate";
|
||||
import { IOptions } from "./options";
|
||||
export { IOptions, IDeclarationOptions, IDtdOptions, IFormatOptions, ITypeHandlers, IWrapHandlers, } from "./options";
|
||||
/**
|
||||
* Indicates that an object of a particular type should be suppressed from the
|
||||
* XML output.
|
||||
*
|
||||
* See the `typeHandlers` property in {@link IOptions} for more details.
|
||||
*/
|
||||
export declare class Absent {
|
||||
private static _instance;
|
||||
private constructor();
|
||||
/**
|
||||
* Returns the sole instance of Absent.
|
||||
*/
|
||||
static get instance(): Absent;
|
||||
}
|
||||
/**
|
||||
* Converts the specified object to XML and adds the XML representation to the
|
||||
* specified XmlElement object using the specified options.
|
||||
*
|
||||
* This function does not add a root element. In addition, it does not add an
|
||||
* XML declaration or DTD, and the associated options in {@link IOptions} are
|
||||
* ignored. If desired, these must be added manually.
|
||||
*/
|
||||
export declare function parseToExistingElement(element: XmlElement<unknown>, object: unknown, options?: IOptions): void;
|
||||
/**
|
||||
* Returns a XML string representation of the specified object using the
|
||||
* specified options.
|
||||
*
|
||||
* `root` is the name of the root XML element. When the object is converted
|
||||
* to XML, it will be a child of this root element.
|
||||
*/
|
||||
export declare function parse(root: string, object: unknown, options?: IOptions): string;
|
||||
+282
@@ -0,0 +1,282 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.parse = exports.parseToExistingElement = exports.Absent = void 0;
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
var xmlcreate_1 = require("xmlcreate");
|
||||
var options_1 = require("./options");
|
||||
var utils_1 = require("./utils");
|
||||
/**
|
||||
* Indicates that an object of a particular type should be suppressed from the
|
||||
* XML output.
|
||||
*
|
||||
* See the `typeHandlers` property in {@link IOptions} for more details.
|
||||
*/
|
||||
var Absent = /** @class */ (function () {
|
||||
function Absent() {
|
||||
}
|
||||
Object.defineProperty(Absent, "instance", {
|
||||
/**
|
||||
* Returns the sole instance of Absent.
|
||||
*/
|
||||
get: function () {
|
||||
return Absent._instance;
|
||||
},
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
Absent._instance = new Absent();
|
||||
return Absent;
|
||||
}());
|
||||
exports.Absent = Absent;
|
||||
/**
|
||||
* Gets the type handler associated with a value.
|
||||
*/
|
||||
function getHandler(value, options) {
|
||||
var type = Object.prototype.toString.call(value);
|
||||
var handler;
|
||||
if (Object.prototype.hasOwnProperty.call(options.typeHandlers, "*")) {
|
||||
handler = options.typeHandlers["*"];
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(options.typeHandlers, type)) {
|
||||
handler = options.typeHandlers[type];
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
/**
|
||||
* Parses a string into XML and adds it to the parent element or attribute.
|
||||
*/
|
||||
function parseString(str, parentElement, options) {
|
||||
var requiresCdata = function (s) {
|
||||
return ((options.cdataInvalidChars &&
|
||||
(s.indexOf("<") !== -1 || s.indexOf("&") !== -1)) ||
|
||||
options.cdataKeys.indexOf(parentElement.name) !== -1 ||
|
||||
options.cdataKeys.indexOf("*") !== -1);
|
||||
};
|
||||
if (parentElement instanceof xmlcreate_1.XmlElement) {
|
||||
if (requiresCdata(str)) {
|
||||
var cdataStrs = str.split("]]>");
|
||||
for (var i = 0; i < cdataStrs.length; i++) {
|
||||
if (requiresCdata(cdataStrs[i])) {
|
||||
parentElement.cdata({
|
||||
charData: cdataStrs[i],
|
||||
replaceInvalidCharsInCharData: options.replaceInvalidChars,
|
||||
});
|
||||
}
|
||||
else {
|
||||
parentElement.charData({
|
||||
charData: cdataStrs[i],
|
||||
replaceInvalidCharsInCharData: options.replaceInvalidChars,
|
||||
});
|
||||
}
|
||||
if (i < cdataStrs.length - 1) {
|
||||
parentElement.charData({
|
||||
charData: "]]>",
|
||||
replaceInvalidCharsInCharData: options.replaceInvalidChars,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
parentElement.charData({
|
||||
charData: str,
|
||||
replaceInvalidCharsInCharData: options.replaceInvalidChars,
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
parentElement.text({
|
||||
charData: str,
|
||||
replaceInvalidCharsInCharData: options.replaceInvalidChars,
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Parses an attribute into XML and adds it to the parent element.
|
||||
*/
|
||||
function parseAttribute(name, value, parentElement, options) {
|
||||
var attribute = parentElement.attribute({
|
||||
name: name,
|
||||
replaceInvalidCharsInName: options.replaceInvalidChars,
|
||||
});
|
||||
parseString((0, utils_1.stringify)(value), attribute, options);
|
||||
}
|
||||
/**
|
||||
* Parses an object or Map entry into XML and adds it to the parent element.
|
||||
*/
|
||||
function parseObjectOrMapEntry(key, value, parentElement, options) {
|
||||
// Alias key
|
||||
if (key === options.aliasString) {
|
||||
parentElement.name = (0, utils_1.stringify)(value);
|
||||
return;
|
||||
}
|
||||
// Attributes key
|
||||
if (key.indexOf(options.attributeString) === 0 && (0, utils_1.isObject)(value)) {
|
||||
for (var _i = 0, _a = Object.keys(value); _i < _a.length; _i++) {
|
||||
var subkey = _a[_i];
|
||||
parseAttribute(subkey, (0, utils_1.stringify)(value[subkey]), parentElement, options);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Value key
|
||||
if (key.indexOf(options.valueString) === 0) {
|
||||
parseValue(key, (0, utils_1.stringify)(value), parentElement, options);
|
||||
return;
|
||||
}
|
||||
// Standard handling (create new element for entry)
|
||||
var element = parentElement;
|
||||
if (!(0, utils_1.isArray)(value) && !(0, utils_1.isSet)(value)) {
|
||||
// If handler for value returns absent, then do not add element
|
||||
var handler = getHandler(value, options);
|
||||
if (!(0, utils_1.isUndefined)(handler)) {
|
||||
if (handler(value) === Absent.instance) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
element = parentElement.element({
|
||||
name: key,
|
||||
replaceInvalidCharsInName: options.replaceInvalidChars,
|
||||
useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
|
||||
});
|
||||
}
|
||||
parseValue(key, value, element, options);
|
||||
}
|
||||
/**
|
||||
* Parses an Object or Map into XML and adds it to the parent element.
|
||||
*/
|
||||
function parseObjectOrMap(objectOrMap, parentElement, options) {
|
||||
if ((0, utils_1.isMap)(objectOrMap)) {
|
||||
objectOrMap.forEach(function (value, key) {
|
||||
parseObjectOrMapEntry((0, utils_1.stringify)(key), value, parentElement, options);
|
||||
});
|
||||
}
|
||||
else {
|
||||
for (var _i = 0, _a = Object.keys(objectOrMap); _i < _a.length; _i++) {
|
||||
var key = _a[_i];
|
||||
parseObjectOrMapEntry(key, objectOrMap[key], parentElement, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Parses an array or Set into XML and adds it to the parent element.
|
||||
*/
|
||||
function parseArrayOrSet(key, arrayOrSet, parentElement, options) {
|
||||
var arrayNameFunc;
|
||||
if (Object.prototype.hasOwnProperty.call(options.wrapHandlers, "*")) {
|
||||
arrayNameFunc = options.wrapHandlers["*"];
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(options.wrapHandlers, key)) {
|
||||
arrayNameFunc = options.wrapHandlers[key];
|
||||
}
|
||||
var arrayKey = key;
|
||||
var arrayElement = parentElement;
|
||||
if (!(0, utils_1.isUndefined)(arrayNameFunc)) {
|
||||
var arrayNameFuncKey = arrayNameFunc(arrayKey, arrayOrSet);
|
||||
if (!(0, utils_1.isNull)(arrayNameFuncKey)) {
|
||||
arrayKey = arrayNameFuncKey;
|
||||
arrayElement = parentElement.element({
|
||||
name: key,
|
||||
replaceInvalidCharsInName: options.replaceInvalidChars,
|
||||
useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
|
||||
});
|
||||
}
|
||||
}
|
||||
arrayOrSet.forEach(function (item) {
|
||||
var element = arrayElement;
|
||||
if (!(0, utils_1.isArray)(item) && !(0, utils_1.isSet)(item)) {
|
||||
// If handler for value returns absent, then do not add element
|
||||
var handler = getHandler(item, options);
|
||||
if (!(0, utils_1.isUndefined)(handler)) {
|
||||
if (handler(item) === Absent.instance) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
element = arrayElement.element({
|
||||
name: arrayKey,
|
||||
replaceInvalidCharsInName: options.replaceInvalidChars,
|
||||
useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
|
||||
});
|
||||
}
|
||||
parseValue(arrayKey, item, element, options);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Parses an arbitrary JavaScript value into XML and adds it to the parent
|
||||
* element.
|
||||
*/
|
||||
function parseValue(key, value, parentElement, options) {
|
||||
// If a handler for a particular type is user-defined, use that handler
|
||||
// instead of the defaults
|
||||
var handler = getHandler(value, options);
|
||||
if (!(0, utils_1.isUndefined)(handler)) {
|
||||
value = handler(value);
|
||||
}
|
||||
if ((0, utils_1.isObject)(value) || (0, utils_1.isMap)(value)) {
|
||||
parseObjectOrMap(value, parentElement, options);
|
||||
return;
|
||||
}
|
||||
if ((0, utils_1.isArray)(value) || (0, utils_1.isSet)(value)) {
|
||||
parseArrayOrSet(key, value, parentElement, options);
|
||||
return;
|
||||
}
|
||||
parseString((0, utils_1.stringify)(value), parentElement, options);
|
||||
}
|
||||
/**
|
||||
* Converts the specified object to XML and adds the XML representation to the
|
||||
* specified XmlElement object using the specified options.
|
||||
*
|
||||
* This function does not add a root element. In addition, it does not add an
|
||||
* XML declaration or DTD, and the associated options in {@link IOptions} are
|
||||
* ignored. If desired, these must be added manually.
|
||||
*/
|
||||
function parseToExistingElement(element, object, options) {
|
||||
var opts = new options_1.Options(options);
|
||||
parseValue(element.name, object, element, opts);
|
||||
}
|
||||
exports.parseToExistingElement = parseToExistingElement;
|
||||
/**
|
||||
* Returns a XML string representation of the specified object using the
|
||||
* specified options.
|
||||
*
|
||||
* `root` is the name of the root XML element. When the object is converted
|
||||
* to XML, it will be a child of this root element.
|
||||
*/
|
||||
function parse(root, object, options) {
|
||||
var opts = new options_1.Options(options);
|
||||
var document = new xmlcreate_1.XmlDocument({
|
||||
validation: opts.validation,
|
||||
});
|
||||
if (opts.declaration.include) {
|
||||
document.decl(opts.declaration);
|
||||
}
|
||||
if (opts.dtd.include) {
|
||||
document.dtd({
|
||||
// Validated in options.ts
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
name: opts.dtd.name,
|
||||
pubId: opts.dtd.pubId,
|
||||
sysId: opts.dtd.sysId,
|
||||
});
|
||||
}
|
||||
var rootElement = document.element({
|
||||
name: root,
|
||||
replaceInvalidCharsInName: opts.replaceInvalidChars,
|
||||
useSelfClosingTagIfEmpty: opts.useSelfClosingTagIfEmpty,
|
||||
});
|
||||
parseToExistingElement(rootElement, object, options);
|
||||
return document.toString(opts.format);
|
||||
}
|
||||
exports.parse = parse;
|
||||
+477
@@ -0,0 +1,477 @@
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
/**
|
||||
* The options associated with parsing an object and formatting the resulting
|
||||
* XML.
|
||||
*/
|
||||
export interface IOptions {
|
||||
/**
|
||||
* If an object or map contains a key that, when converted to a string,
|
||||
* is equal to the value of `aliasString`, then the name of the XML element
|
||||
* containing the object will be replaced with the value associated with
|
||||
* said key.
|
||||
*
|
||||
* For example, if `aliasString` is `"="`, then the following object:
|
||||
* ```javascript
|
||||
* {
|
||||
* "abc": {
|
||||
* "=": "def"
|
||||
* "#": "ghi"
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* <def>ghi</def>
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* The default alias string is `"="`.
|
||||
*/
|
||||
aliasString?: string;
|
||||
/**
|
||||
* If an object or map contains a key that, when converted to a string,
|
||||
* begins with the value of `attributeString`, then the value mapped by
|
||||
* said key will be interpreted as attributes for the XML element for that
|
||||
* object.
|
||||
*
|
||||
* The keys of the value of `attributeString` are interpreted as attribute
|
||||
* names, while the values mapping to those keys are interpreted as
|
||||
* attribute values.
|
||||
*
|
||||
* For example, if `attributeString` is `"@"`, then the following object:
|
||||
* ```javascript
|
||||
* {
|
||||
* "abc": {
|
||||
* "@1": {
|
||||
* "ghi": "jkl",
|
||||
* "mno": "pqr"
|
||||
* },
|
||||
* "stu": "vwx",
|
||||
* "@2": {
|
||||
* "yza": "bcd"
|
||||
* },
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* <abc ghi='jkl' mno='pqr' yza='bcd'>
|
||||
* <stu>vwx</stu>
|
||||
* </abc>
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* The default attribute string is `"@"`.
|
||||
*/
|
||||
attributeString?: string;
|
||||
/**
|
||||
* Whether to enclose any text containing the characters `<` or `&`
|
||||
* in CDATA sections. If this is false, these characters shall be replaced
|
||||
* with XML escape characters instead.
|
||||
*
|
||||
* By default, this is disabled.
|
||||
*/
|
||||
cdataInvalidChars?: boolean;
|
||||
/**
|
||||
* If an object or map contains a key that, when converted to a string, is
|
||||
* equal to an item in `cdataKeys`, then the value mapped by said key will
|
||||
* be enclosed in a CDATA section.
|
||||
*
|
||||
* For example, if `cdataKeys` is:
|
||||
* ```javascript
|
||||
* [
|
||||
* "abc"
|
||||
* ]
|
||||
* ```
|
||||
* then the following object:
|
||||
* ```javascript
|
||||
* {
|
||||
* "abc": "def&",
|
||||
* "ghi": "jkl",
|
||||
* "mno": "pqr<"
|
||||
* }
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* <abc><![CDATA[def&]]></ghi>
|
||||
* <ghi>jlk</ghi>
|
||||
* <mno>pqr<</mno>
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* If `cdataKeys` has a key named `"*"`, then that entry will match all
|
||||
* keys.
|
||||
*
|
||||
* By default, this is an empty array.
|
||||
*/
|
||||
cdataKeys?: string[];
|
||||
/**
|
||||
* The options associated with the XML declaration.
|
||||
*/
|
||||
declaration?: IDeclarationOptions;
|
||||
/**
|
||||
* The options associated with the XML document type definition.
|
||||
*/
|
||||
dtd?: IDtdOptions;
|
||||
/**
|
||||
* The options associated with the formatting of the XML document.
|
||||
*/
|
||||
format?: IFormatOptions;
|
||||
/**
|
||||
* Whether to replace any characters that are not valid in XML in particular
|
||||
* contexts with the Unicode replacement character, U+FFFD.
|
||||
*
|
||||
* At present this is limited to attribute names and values; element names
|
||||
* and character data; CDATA sections; and comments. This may be extended
|
||||
* in future.
|
||||
*
|
||||
* By default, this is disabled.
|
||||
*/
|
||||
replaceInvalidChars?: boolean;
|
||||
/**
|
||||
* If a value has a type (as defined by calling `Object.prototype.toString`
|
||||
* on the value) equal to a key in `typeHandlers`, then said value will be
|
||||
* replaced by the return value of the function mapped to by the key in
|
||||
* `typeHandlers`. This function is called with the value as a parameter.
|
||||
*
|
||||
* If one of these functions returns the sole instance of {@link Absent},
|
||||
* then the value will be suppressed from the XML output altogether.
|
||||
*
|
||||
* For example, if `typeHandlers` is:
|
||||
* ```javascript
|
||||
* {
|
||||
* "[object Date]": function(value) {
|
||||
* return value.getYear();
|
||||
* },
|
||||
* "[object Null]": function(value) {
|
||||
* return Absent.instance;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* then the following object:
|
||||
* ```javascript
|
||||
* {
|
||||
* "abc": new Date(2012, 10, 31),
|
||||
* "def": null
|
||||
* }
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* <abc>2012</abc>
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* If `typeHandlers` has a key named `"*"`, then that entry will match all
|
||||
* values, unless there is a more specific entry.
|
||||
*
|
||||
* Note that normal parsing still occurs for the value returned by the
|
||||
* function; it is not directly converted to a string.
|
||||
*
|
||||
* The default value is an empty object.
|
||||
*/
|
||||
typeHandlers?: ITypeHandlers;
|
||||
/**
|
||||
* Whether to use a self-closing tag for empty elements.
|
||||
*
|
||||
* For example, the following element will be used:
|
||||
* ```xml
|
||||
* <element/>
|
||||
* ```
|
||||
* instead of:
|
||||
* ```xml
|
||||
* <element></element>
|
||||
* ```
|
||||
*
|
||||
* By default, this is enabled.
|
||||
*/
|
||||
useSelfClosingTagIfEmpty?: boolean;
|
||||
/**
|
||||
* Whether to throw an exception if basic XML validation fails while
|
||||
* building the document.
|
||||
*
|
||||
* By default, this is enabled.
|
||||
*/
|
||||
validation?: boolean;
|
||||
/**
|
||||
* If an object or map contains a key that, when converted to a string,
|
||||
* begins with the value of `valueString`, then the value mapped by said key
|
||||
* will be represented as bare text within the XML element for that object.
|
||||
*
|
||||
* For example, if `valueString` is `"#"`, then the following object:
|
||||
* ```javascript
|
||||
* new Map([
|
||||
* ["#1", "abc"],
|
||||
* ["def", "ghi"],
|
||||
* ["#2", "jkl"]
|
||||
* ])
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* abc
|
||||
* <def>ghi</def>
|
||||
* jkl
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* The default value is `"#"`.
|
||||
*/
|
||||
valueString?: string;
|
||||
/**
|
||||
* If an object or map contains a key that, when converted to a string, is
|
||||
* equal to a key in `wrapHandlers`, and the key in said object or map maps
|
||||
* to an array or set, then all items in the array or set will be wrapped
|
||||
* in an XML element with the same name as the key.
|
||||
*
|
||||
* The key in `wrapHandlers` must map to a function that is called with the
|
||||
* key name, as well as the array or set, as parameters. This function must
|
||||
* return a string or value that can be converted to a string, which will
|
||||
* become the name for each XML element for each item in the array or set.
|
||||
* Alternatively, this function may return `null` to indicate that no
|
||||
* wrapping should occur.
|
||||
*
|
||||
* For example, if `wrapHandlers` is:
|
||||
* ```javascript
|
||||
* {
|
||||
* "abc": function(key, value) {
|
||||
* return "def";
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* then the following object:
|
||||
* ```javascript
|
||||
* {
|
||||
* "ghi": "jkl",
|
||||
* "mno": {
|
||||
* "pqr": ["s", "t"]
|
||||
* },
|
||||
* "uvw": {
|
||||
* "abc": ["x", "y"]
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* will result in the following XML for a root element named `"root"`:
|
||||
* ```xml
|
||||
* <root>
|
||||
* <ghi>jkl</ghi>
|
||||
* <mno>
|
||||
* <pqr>s</pqr>
|
||||
* <pqr>t</pqr>
|
||||
* </mno>
|
||||
* <uwv>
|
||||
* <abc>
|
||||
* <def>x</def>
|
||||
* <def>y</def>
|
||||
* </abc>
|
||||
* </uwv>
|
||||
* </root>
|
||||
* ```
|
||||
*
|
||||
* If `wrapHandlers` has a key named `"*"`, then that entry will
|
||||
* match all arrays and sets, unless there is a more specific entry.
|
||||
*
|
||||
* The default value is an empty object.
|
||||
*/
|
||||
wrapHandlers?: IWrapHandlers;
|
||||
}
|
||||
/**
|
||||
* Implementation of the IOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
export declare class Options implements IOptions {
|
||||
aliasString: string;
|
||||
attributeString: string;
|
||||
cdataInvalidChars: boolean;
|
||||
cdataKeys: string[];
|
||||
declaration: DeclarationOptions;
|
||||
dtd: DtdOptions;
|
||||
format: FormatOptions;
|
||||
replaceInvalidChars: boolean;
|
||||
typeHandlers: TypeHandlers;
|
||||
useSelfClosingTagIfEmpty: boolean;
|
||||
validation: boolean;
|
||||
valueString: string;
|
||||
wrapHandlers: WrapHandlers;
|
||||
constructor(options?: IOptions);
|
||||
}
|
||||
/**
|
||||
* The options associated with the XML declaration. An example of an XML
|
||||
* declaration is as follows:
|
||||
*
|
||||
* ```xml
|
||||
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
* ```
|
||||
*/
|
||||
export interface IDeclarationOptions {
|
||||
/**
|
||||
* Whether to include a declaration in the generated XML. By default,
|
||||
* one is included.
|
||||
*/
|
||||
include?: boolean;
|
||||
/**
|
||||
* The encoding attribute to be included in the declaration. If defined,
|
||||
* this value must be a valid encoding. By default, no encoding attribute
|
||||
* is included.
|
||||
*/
|
||||
encoding?: string;
|
||||
/**
|
||||
* The value of the standalone attribute to be included in the declaration.
|
||||
* If defined, this value must be "yes" or "no". By default, no standalone
|
||||
* attribute is included.
|
||||
*/
|
||||
standalone?: string;
|
||||
/**
|
||||
* The XML version to be included in the declaration. If defined, this
|
||||
* value must be a valid XML version number. Defaults to "1.0".
|
||||
*/
|
||||
version?: string;
|
||||
}
|
||||
/**
|
||||
* Implementation of the IDeclarationOptions interface used to provide default
|
||||
* values to fields.
|
||||
*/
|
||||
export declare class DeclarationOptions implements IDeclarationOptions {
|
||||
include: boolean;
|
||||
encoding?: string;
|
||||
standalone?: string;
|
||||
version?: string;
|
||||
constructor(declarationOptions?: IDeclarationOptions);
|
||||
}
|
||||
/**
|
||||
* The options associated with the XML document type definition (DTD). An
|
||||
* example of a DTD is as follows:
|
||||
*
|
||||
* ```xml
|
||||
* <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||||
* "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
* ```
|
||||
*/
|
||||
export interface IDtdOptions {
|
||||
/**
|
||||
* Whether to include a DTD in the generated XML. By default, no DTD is
|
||||
* included.
|
||||
*/
|
||||
include?: boolean;
|
||||
/**
|
||||
* The name of the DTD. This value cannot be left undefined if `include`
|
||||
* is true.
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* The system identifier of the DTD, excluding quotation marks. By default,
|
||||
* no system identifier is included.
|
||||
*/
|
||||
sysId?: string;
|
||||
/**
|
||||
* The public identifier of the DTD, excluding quotation marks. If a public
|
||||
* identifier is provided, a system identifier must be provided as well.
|
||||
* By default, no public identifier is included.
|
||||
*/
|
||||
pubId?: string;
|
||||
}
|
||||
/**
|
||||
* Implementation of the IDtdOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
export declare class DtdOptions implements IDtdOptions {
|
||||
include: boolean;
|
||||
name?: string;
|
||||
sysId?: string;
|
||||
pubId?: string;
|
||||
constructor(validation: boolean, dtdOptions?: IDtdOptions);
|
||||
}
|
||||
/**
|
||||
* The options associated with the formatting of the XML document.
|
||||
*/
|
||||
export interface IFormatOptions {
|
||||
/**
|
||||
* Whether double quotes or single quotes should be used in XML attributes.
|
||||
* By default, single quotes are used.
|
||||
*/
|
||||
doubleQuotes?: boolean;
|
||||
/**
|
||||
* The indent string used for pretty-printing. The default indent string is
|
||||
* four spaces.
|
||||
*/
|
||||
indent?: string;
|
||||
/**
|
||||
* The newline string used for pretty-printing. The default newline string
|
||||
* is "\n".
|
||||
*/
|
||||
newline?: string;
|
||||
/**
|
||||
* Whether pretty-printing is enabled. By default, pretty-printing is
|
||||
* enabled.
|
||||
*/
|
||||
pretty?: boolean;
|
||||
}
|
||||
/**
|
||||
* Implementation of the IFormatOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
export declare class FormatOptions implements IFormatOptions {
|
||||
doubleQuotes?: boolean;
|
||||
indent?: string;
|
||||
newline?: string;
|
||||
pretty?: boolean;
|
||||
constructor(formatOptions?: IFormatOptions);
|
||||
}
|
||||
/**
|
||||
* Map for the `typeHandlers` property in the {@link IOptions} interface.
|
||||
*/
|
||||
export interface ITypeHandlers {
|
||||
/**
|
||||
* Mapping between the type of a value in an object to a function taking
|
||||
* this value and returning a replacement value.
|
||||
*/
|
||||
[type: string]: (value: any) => unknown;
|
||||
}
|
||||
/**
|
||||
* Implementation of the ITypeHandlers interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
export declare class TypeHandlers implements ITypeHandlers {
|
||||
[type: string]: (value: any) => unknown;
|
||||
constructor(typeHandlers?: ITypeHandlers);
|
||||
}
|
||||
/**
|
||||
* Map for the `wrapHandlers` property in the {@link IOptions} interface.
|
||||
*/
|
||||
export interface IWrapHandlers {
|
||||
/**
|
||||
* Mapping between the string version of a key in an object or map with a
|
||||
* value that is an array or set to a function taking the string version
|
||||
* of that key, as well as that array or set.
|
||||
*
|
||||
* This function returns either a string that will become the name for each
|
||||
* XML element for each item in the array or set, or `null` to indicate that
|
||||
* wrapping should not occur.
|
||||
*/
|
||||
[key: string]: (key: string, value: any) => string | null;
|
||||
}
|
||||
/**
|
||||
* Implementation of the IWrapHandlers interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
export declare class WrapHandlers implements IWrapHandlers {
|
||||
[key: string]: (key: string, value: any) => string | null;
|
||||
constructor(wrapHandlers?: IWrapHandlers);
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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 });
|
||||
exports.WrapHandlers = exports.TypeHandlers = exports.FormatOptions = exports.DtdOptions = exports.DeclarationOptions = exports.Options = void 0;
|
||||
var utils_1 = require("./utils");
|
||||
/**
|
||||
* Implementation of the IOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
var Options = /** @class */ (function () {
|
||||
function Options(options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
this.aliasString = "=";
|
||||
this.attributeString = "@";
|
||||
this.cdataInvalidChars = false;
|
||||
this.cdataKeys = [];
|
||||
this.replaceInvalidChars = false;
|
||||
this.useSelfClosingTagIfEmpty = true;
|
||||
this.validation = true;
|
||||
this.valueString = "#";
|
||||
if (!(0, utils_1.isUndefined)(options.validation)) {
|
||||
this.validation = options.validation;
|
||||
}
|
||||
if (!(0, utils_1.isUndefined)(options.aliasString)) {
|
||||
this.aliasString = options.aliasString;
|
||||
}
|
||||
if (!(0, utils_1.isUndefined)(options.attributeString)) {
|
||||
this.attributeString = options.attributeString;
|
||||
}
|
||||
if (!(0, utils_1.isUndefined)(options.cdataInvalidChars)) {
|
||||
this.cdataInvalidChars = options.cdataInvalidChars;
|
||||
}
|
||||
if (!(0, utils_1.isUndefined)(options.cdataKeys)) {
|
||||
this.cdataKeys = options.cdataKeys;
|
||||
}
|
||||
this.declaration = new DeclarationOptions(options.declaration);
|
||||
this.dtd = new DtdOptions(this.validation, options.dtd);
|
||||
this.format = new FormatOptions(options.format);
|
||||
if (!(0, utils_1.isUndefined)(options.replaceInvalidChars)) {
|
||||
this.replaceInvalidChars = options.replaceInvalidChars;
|
||||
}
|
||||
this.typeHandlers = new TypeHandlers(options.typeHandlers);
|
||||
if (!(0, utils_1.isUndefined)(options.useSelfClosingTagIfEmpty)) {
|
||||
this.useSelfClosingTagIfEmpty = options.useSelfClosingTagIfEmpty;
|
||||
}
|
||||
if (!(0, utils_1.isUndefined)(options.valueString)) {
|
||||
this.valueString = options.valueString;
|
||||
}
|
||||
this.wrapHandlers = new WrapHandlers(options.wrapHandlers);
|
||||
}
|
||||
return Options;
|
||||
}());
|
||||
exports.Options = Options;
|
||||
/**
|
||||
* Implementation of the IDeclarationOptions interface used to provide default
|
||||
* values to fields.
|
||||
*/
|
||||
var DeclarationOptions = /** @class */ (function () {
|
||||
function DeclarationOptions(declarationOptions) {
|
||||
if (declarationOptions === void 0) { declarationOptions = {}; }
|
||||
this.include = true;
|
||||
if (!(0, utils_1.isUndefined)(declarationOptions.include)) {
|
||||
this.include = declarationOptions.include;
|
||||
}
|
||||
// Validation performed by xmlcreate
|
||||
this.encoding = declarationOptions.encoding;
|
||||
this.standalone = declarationOptions.standalone;
|
||||
this.version = declarationOptions.version;
|
||||
}
|
||||
return DeclarationOptions;
|
||||
}());
|
||||
exports.DeclarationOptions = DeclarationOptions;
|
||||
/**
|
||||
* Implementation of the IDtdOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
var DtdOptions = /** @class */ (function () {
|
||||
function DtdOptions(validation, dtdOptions) {
|
||||
if (dtdOptions === void 0) { dtdOptions = {}; }
|
||||
this.include = false;
|
||||
if (!(0, utils_1.isUndefined)(dtdOptions.include)) {
|
||||
this.include = dtdOptions.include;
|
||||
}
|
||||
if (validation && (0, utils_1.isUndefined)(dtdOptions.name) && this.include) {
|
||||
throw new Error("options.dtd.name should be defined if" +
|
||||
" options.dtd.include is true");
|
||||
}
|
||||
this.name = dtdOptions.name;
|
||||
this.sysId = dtdOptions.sysId;
|
||||
this.pubId = dtdOptions.pubId;
|
||||
}
|
||||
return DtdOptions;
|
||||
}());
|
||||
exports.DtdOptions = DtdOptions;
|
||||
/**
|
||||
* Implementation of the IFormatOptions interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
var FormatOptions = /** @class */ (function () {
|
||||
function FormatOptions(formatOptions) {
|
||||
if (formatOptions === void 0) { formatOptions = {}; }
|
||||
this.doubleQuotes = formatOptions.doubleQuotes;
|
||||
this.indent = formatOptions.indent;
|
||||
this.newline = formatOptions.newline;
|
||||
this.pretty = formatOptions.pretty;
|
||||
}
|
||||
return FormatOptions;
|
||||
}());
|
||||
exports.FormatOptions = FormatOptions;
|
||||
/**
|
||||
* Implementation of the ITypeHandlers interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
var TypeHandlers = /** @class */ (function () {
|
||||
function TypeHandlers(typeHandlers) {
|
||||
if (typeHandlers === void 0) { typeHandlers = {}; }
|
||||
for (var key in typeHandlers) {
|
||||
if (Object.prototype.hasOwnProperty.call(typeHandlers, key)) {
|
||||
this[key] = typeHandlers[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return TypeHandlers;
|
||||
}());
|
||||
exports.TypeHandlers = TypeHandlers;
|
||||
/**
|
||||
* Implementation of the IWrapHandlers interface used to provide default values
|
||||
* to fields.
|
||||
*/
|
||||
var WrapHandlers = /** @class */ (function () {
|
||||
function WrapHandlers(wrapHandlers) {
|
||||
if (wrapHandlers === void 0) { wrapHandlers = {}; }
|
||||
for (var key in wrapHandlers) {
|
||||
if (Object.prototype.hasOwnProperty.call(wrapHandlers, key)) {
|
||||
this[key] = wrapHandlers[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return WrapHandlers;
|
||||
}());
|
||||
exports.WrapHandlers = WrapHandlers;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
export declare function isUndefined(val: unknown): val is undefined;
|
||||
export declare function isNull(val: unknown): val is null;
|
||||
export declare function isObject(val: unknown): val is Record<string, unknown>;
|
||||
export declare function isArray(val: unknown): val is unknown[];
|
||||
export declare function isFunction(val: unknown): val is Function;
|
||||
export declare function isSet(val: unknown): val is Set<unknown>;
|
||||
export declare function isMap(val: unknown): val is Map<unknown, unknown>;
|
||||
/**
|
||||
* Returns a string representation of the specified value, as given by the
|
||||
* value's toString() method (if it has one) or the global String() function
|
||||
* (if it does not).
|
||||
*
|
||||
* @param value The value to convert to a string.
|
||||
*
|
||||
* @returns A string representation of the specified value.
|
||||
*/
|
||||
export declare function stringify(value: any): string;
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Copyright (C) 2016-2020 Michael Kourlas
|
||||
*
|
||||
* 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 });
|
||||
exports.stringify = exports.isMap = exports.isSet = exports.isFunction = exports.isArray = exports.isObject = exports.isNull = exports.isUndefined = void 0;
|
||||
function isUndefined(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Undefined]";
|
||||
}
|
||||
exports.isUndefined = isUndefined;
|
||||
function isNull(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Null]";
|
||||
}
|
||||
exports.isNull = isNull;
|
||||
function isObject(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Object]";
|
||||
}
|
||||
exports.isObject = isObject;
|
||||
function isArray(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Array]";
|
||||
}
|
||||
exports.isArray = isArray;
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
function isFunction(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Function]";
|
||||
}
|
||||
exports.isFunction = isFunction;
|
||||
function isSet(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Set]";
|
||||
}
|
||||
exports.isSet = isSet;
|
||||
function isMap(val) {
|
||||
return Object.prototype.toString.call(val) === "[object Map]";
|
||||
}
|
||||
exports.isMap = isMap;
|
||||
/**
|
||||
* Returns a string representation of the specified value, as given by the
|
||||
* value's toString() method (if it has one) or the global String() function
|
||||
* (if it does not).
|
||||
*
|
||||
* @param value The value to convert to a string.
|
||||
*
|
||||
* @returns A string representation of the specified value.
|
||||
*/
|
||||
// eslint-disable-next-line max-len
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
||||
function stringify(value) {
|
||||
if (!isUndefined(value) && !isNull(value)) {
|
||||
if (isFunction(value === null || value === void 0 ? void 0 : value.toString)) {
|
||||
value = value.toString();
|
||||
}
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
exports.stringify = stringify;
|
||||
Reference in New Issue
Block a user