Skip to content

Commit 35b0c3d

Browse files
Change option names to improve backwards compatibility
1 parent 1a34da0 commit 35b0c3d

5 files changed

Lines changed: 516 additions & 516 deletions

File tree

examples/example.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var example2 = function() {
4141
"lastName": "Smith",
4242
"dateOfBirth": new Date(1964, 7, 26),
4343
"address": {
44-
"__attr": {
44+
"@": {
4545
"type": "home"
4646
},
4747
"streetAddress": "3212 22nd St",
@@ -51,22 +51,22 @@ var example2 = function() {
5151
},
5252
"phone": [
5353
{
54-
"__attr": {
54+
"@": {
5555
"type": "home"
5656
},
57-
"__val": "123-555-4567"
57+
"#": "123-555-4567"
5858
},
5959
{
60-
"__attr": {
60+
"@": {
6161
"type": "cell"
6262
},
63-
"__val": "890-555-1234"
63+
"#": "890-555-1234"
6464
},
6565
{
66-
"__attr": {
66+
"@": {
6767
"type": "work"
6868
},
69-
"__val": "567-555-8901"
69+
"#": "567-555-8901"
7070
}
7171
],
7272
"email": "john@smith.com"
@@ -81,18 +81,13 @@ example2();
8181
*/
8282
var example3 = function() {
8383
var options = {
84-
alias: "exAlias",
85-
arrayHandlers: {
86-
"exArr": function() {
87-
return "exArrInner";
88-
}
89-
},
90-
attrPrefix: "exAttr",
84+
aliasString: "exAlias",
85+
attributeString: "exAttr",
9186
cdataKeys: [
9287
"exCdata",
9388
"exCdata2"
9489
],
95-
decl: {
90+
declaration: {
9691
include: true,
9792
encoding: "UTF-16",
9893
standalone: "yes",
@@ -115,7 +110,12 @@ var example3 = function() {
115110
return value + 17;
116111
}
117112
},
118-
valPrefix: "exVal"
113+
valueString: "exVal",
114+
wrapHandlers: {
115+
"exArr": function() {
116+
return "exArrInner";
117+
}
118+
}
119119
};
120120

121121
var obj = {

src/main.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function parseString(str: string, parentElement: XmlAttribute | XmlElement,
3333
options: IOptions): void
3434
{
3535
let requiresCdata = (s: string) => {
36-
return (options.cdata && (s.indexOf("<") !== -1
37-
|| s.indexOf("&") !== -1))
36+
return (options.cdataInvalidChars && (s.indexOf("<") !== -1
37+
|| s.indexOf("&") !== -1))
3838
|| options.cdataKeys.indexOf(parentElement.name) !== -1
3939
|| options.cdataKeys.indexOf("*") !== -1;
4040
};
@@ -96,16 +96,17 @@ function parseObjectOrMapEntry(key: string, value: any,
9696
options: IOptions): void
9797
{
9898
// Alias key
99-
if (key === options.alias) {
99+
if (key === options.aliasString) {
100100
if (!isType(value, "String")) {
101-
throw new Error("alias value for " + value + " should be a string");
101+
throw new Error("aliasString value for " + value
102+
+ " should be a string");
102103
}
103104
parentElement.name = value;
104105
return;
105106
}
106107

107108
// Attributes key
108-
if (key.indexOf(options.attrPrefix) === 0) {
109+
if (key.indexOf(options.attributeString) === 0) {
109110
if (isType(value, "Object")) {
110111
for (let subkey of Object.keys(value)) {
111112
parseAttribute(subkey, value[subkey], parentElement, options);
@@ -118,7 +119,7 @@ function parseObjectOrMapEntry(key: string, value: any,
118119
}
119120

120121
// Value key
121-
if (key.indexOf(options.valPrefix) === 0) {
122+
if (key.indexOf(options.valueString) === 0) {
122123
if (isType(value, "String") || isType(value, "Number")
123124
|| isType(value, "Boolean") || isType(value, "Null")
124125
|| isType(value, "Undefined"))
@@ -182,11 +183,11 @@ function parseArrayOrSet(key: string, arrayOrSet: any,
182183
parentElement: XmlElement, options: IOptions): void
183184
{
184185
let arrayNameFunc: (key: string, value: any) => string;
185-
if (options.arraySetWrapHandlers.hasOwnProperty("*")) {
186-
arrayNameFunc = options.arraySetWrapHandlers["*"];
186+
if (options.wrapHandlers.hasOwnProperty("*")) {
187+
arrayNameFunc = options.wrapHandlers["*"];
187188
}
188-
if (options.arraySetWrapHandlers.hasOwnProperty(key)) {
189-
arrayNameFunc = options.arraySetWrapHandlers[key];
189+
if (options.wrapHandlers.hasOwnProperty(key)) {
190+
arrayNameFunc = options.wrapHandlers[key];
190191
}
191192

192193
let arrayKey = key;
@@ -197,7 +198,7 @@ function parseArrayOrSet(key: string, arrayOrSet: any,
197198
arrayKey = arrayNameFuncKey;
198199
arrayElement = parentElement.element(key);
199200
} else if (!isType(arrayNameFuncKey, "Null")) {
200-
throw new Error("arraySetWrapHandlers function for " + arrayKey
201+
throw new Error("wrapHandlers function for " + arrayKey
201202
+ " should return a string or null");
202203
}
203204
}
@@ -269,8 +270,8 @@ function parseToDocument(root: string, value: any,
269270
options: IOptions): XmlDocument
270271
{
271272
let document: XmlDocument = new XmlDocument(root);
272-
if (options.decl.include) {
273-
document.decl(options.decl);
273+
if (options.declaration.include) {
274+
document.decl(options.declaration);
274275
}
275276
if (options.dtd.include) {
276277
document.dtd(options.dtd.name, options.dtd.sysId, options.dtd.pubId);
@@ -282,18 +283,17 @@ function parseToDocument(root: string, value: any,
282283
/**
283284
* Returns a XML string representation of the specified object.
284285
*
285-
* @param {string} root The name of the root XML element. When the object
286-
* is converted to XML, it will be a child of this
287-
* root element.
286+
* @param {string} root The name of the root XML element. When the
287+
* object is converted to XML, it will be a
288+
* child of this root element.
288289
* @param {*} object The object to convert to XML.
289-
* @param {IOptions} [options] Options for parsing the object and formatting the
290-
* resulting XML.
290+
* @param {IOptions} [options] Options for parsing the object and
291+
* formatting the resulting XML.
291292
*
292293
* @returns {string} An XML string representation of the specified object.
293294
*/
294295
export function parse(root: string, object: any,
295-
options: IOptions = {}): string
296-
{
296+
options: IOptions = {}): string {
297297
options = validateOptions(options);
298298
let document = parseToDocument(root, object, options);
299299
return document.toString(options.format);

0 commit comments

Comments
 (0)