Skip to content

Commit 966620d

Browse files
Refactoring
1 parent 0822954 commit 966620d

5 files changed

Lines changed: 64 additions & 64 deletions

File tree

src/main.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ import {XmlAttribute, XmlDocument, XmlElement} from "xmlcreate";
4141
function parseString(str: string, parentElement: XmlAttribute | XmlElement,
4242
options: Options): void
4343
{
44-
let requiresCdata = (s: string) => {
44+
const requiresCdata = (s: string) => {
4545
return (options.cdataInvalidChars && (s.indexOf("<") !== -1
4646
|| s.indexOf("&") !== -1))
4747
|| options.cdataKeys.indexOf(parentElement.name) !== -1
4848
|| options.cdataKeys.indexOf("*") !== -1;
4949
};
5050

5151
if (parentElement instanceof XmlElement && requiresCdata(str)) {
52-
let cdataStrs = str.split("]]>");
52+
const cdataStrs = str.split("]]>");
5353
for (let i = 0; i < cdataStrs.length; i++) {
5454
if (requiresCdata(cdataStrs[i])) {
5555
parentElement.cdata(cdataStrs[i]);
@@ -78,7 +78,7 @@ function parseString(str: string, parentElement: XmlAttribute | XmlElement,
7878
function parseAttribute(name: string, value: string, parentElement: XmlElement,
7979
options: Options): void
8080
{
81-
let attribute = parentElement.attribute(name, "");
81+
const attribute = parentElement.attribute(name, "");
8282
if (isPrimitive(value)) {
8383
parseString(stringify(value), attribute, options);
8484
} else {
@@ -116,7 +116,7 @@ function parseObjectOrMapEntry(key: string, value: any,
116116
// Attributes key
117117
if (key.indexOf(options.attributeString) === 0) {
118118
if (isObject(value)) {
119-
for (let subkey of Object.keys(value)) {
119+
for (const subkey of Object.keys(value)) {
120120
parseAttribute(subkey, value[subkey], parentElement, options);
121121
}
122122
} else {
@@ -163,7 +163,7 @@ function parseObjectOrMap(objectOrMap: any, parentElement: XmlElement,
163163
options);
164164
});
165165
} else {
166-
for (let key of Object.keys(objectOrMap)) {
166+
for (const key of Object.keys(objectOrMap)) {
167167
parseObjectOrMapEntry(key, objectOrMap[key], parentElement,
168168
options);
169169
}
@@ -194,7 +194,7 @@ function parseArrayOrSet(key: string, arrayOrSet: any,
194194
let arrayKey = key;
195195
let arrayElement = parentElement;
196196
if (!isUndefined(arrayNameFunc)) {
197-
let arrayNameFuncKey = arrayNameFunc(arrayKey, arrayOrSet);
197+
const arrayNameFuncKey = arrayNameFunc(arrayKey, arrayOrSet);
198198
if (isString(arrayNameFuncKey)) {
199199
arrayKey = arrayNameFuncKey;
200200
arrayElement = parentElement.element(key);
@@ -228,7 +228,7 @@ function parseValue(key: string, value: any, parentElement: XmlElement,
228228
{
229229
// If a handler for a particular type is user-defined, use that handler
230230
// instead of the defaults
231-
let type = Object.prototype.toString.call(value);
231+
const type = Object.prototype.toString.call(value);
232232
let handler: ((value: any) => any) | undefined;
233233
if (options.typeHandlers.hasOwnProperty("*")) {
234234
handler = options.typeHandlers["*"];
@@ -260,14 +260,14 @@ function parseValue(key: string, value: any, parentElement: XmlElement,
260260
* @param value The value to convert to XML.
261261
* @param options Options for parsing the value into XML.
262262
*
263-
* @returns {XmlDocument} An XML document corresponding to the specified value.
263+
* @returns An XML document corresponding to the specified value.
264264
*
265265
* @private
266266
*/
267267
function parseToDocument(root: string, value: any,
268268
options: Options): XmlDocument
269269
{
270-
let document: XmlDocument = new XmlDocument(root);
270+
const document: XmlDocument = new XmlDocument(root);
271271
if (options.declaration.include) {
272272
document.decl(options.declaration);
273273
}
@@ -287,10 +287,10 @@ function parseToDocument(root: string, value: any,
287287
* @param options Options for parsing the object and formatting the resulting
288288
* XML.
289289
*
290-
* @returns {string} An XML string representation of the specified object.
290+
* @returns An XML string representation of the specified object.
291291
*/
292292
export function parse(root: string, object: any, options?: IOptions): string {
293-
let opts: Options = new Options(options);
294-
let document = parseToDocument(root, object, opts);
293+
const opts: Options = new Options(options);
294+
const document = parseToDocument(root, object, opts);
295295
return document.toString(opts.format);
296296
}

src/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ export class TypeHandlers implements ITypeHandlers {
556556
+ " undefined");
557557
}
558558

559-
for (let key in typeHandlers) {
559+
for (const key in typeHandlers) {
560560
if (typeHandlers.hasOwnProperty(key)) {
561561
if (!isFunction(typeHandlers[key])) {
562562
throw new TypeError("options.typeHandlers['" + key + "']" +
@@ -600,7 +600,7 @@ export class WrapHandlers implements IWrapHandlers {
600600
+ " undefined");
601601
}
602602

603-
for (let key in wrapHandlers) {
603+
for (const key in wrapHandlers) {
604604
if (wrapHandlers.hasOwnProperty(key)) {
605605
if (!isFunction(wrapHandlers[key])) {
606606
throw new TypeError("options.wrapHandlers['" + key + "']" +

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function isStringArray(val: any): val is string[] {
8383
if (!isArray(val)) {
8484
return false;
8585
}
86-
for (let entry of val) {
86+
for (const entry of val) {
8787
if (!isString(entry)) {
8888
return false;
8989
}
@@ -119,7 +119,7 @@ export function isMap(val: any): boolean {
119119
*
120120
* @param value The value to convert to a string.
121121
*
122-
* @returns {string} A string representation of the specified value.
122+
* @returns A string representation of the specified value.
123123
*
124124
* @private
125125
*/

test/src/main.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe("parser", () => {
317317

318318
describe("options", () => {
319319
describe("aliasString", () => {
320-
let aliasStringOptions: IOptions = {
320+
const aliasStringOptions: IOptions = {
321321
aliasString: "_customAliasString",
322322
declaration: {
323323
include: false
@@ -464,7 +464,7 @@ describe("parser", () => {
464464
});
465465

466466
describe("attributeString", () => {
467-
let attributeStringOptions: IOptions = {
467+
const attributeStringOptions: IOptions = {
468468
attributeString: "attributeString",
469469
declaration: {
470470
include: false
@@ -558,7 +558,7 @@ describe("parser", () => {
558558
});
559559

560560
describe("cdataInvalidChars", () => {
561-
let cdataInvalidCharsOptions: IOptions = {
561+
const cdataInvalidCharsOptions: IOptions = {
562562
cdataInvalidChars: true,
563563
declaration: {
564564
include: false
@@ -603,7 +603,7 @@ describe("parser", () => {
603603
});
604604

605605
describe("cdataKeys", () => {
606-
let cdataKeysOptions: IOptions = {
606+
const cdataKeysOptions: IOptions = {
607607
cdataKeys: [
608608
"test1"
609609
],
@@ -614,7 +614,7 @@ describe("parser", () => {
614614
pretty: false
615615
}
616616
};
617-
let cdataKeysWildcardOptions: IOptions = {
617+
const cdataKeysWildcardOptions: IOptions = {
618618
cdataKeys: [
619619
"test1",
620620
"*"
@@ -678,7 +678,7 @@ describe("parser", () => {
678678
});
679679

680680
describe("declaration", () => {
681-
let declOptions: IOptions = {
681+
const declOptions: IOptions = {
682682
declaration: {
683683
encoding: "UTF-8",
684684
include: true,
@@ -706,7 +706,7 @@ describe("parser", () => {
706706
});
707707

708708
describe("dtd", () => {
709-
let dtdOptions: IOptions = {
709+
const dtdOptions: IOptions = {
710710
declaration: {
711711
include: false
712712
},
@@ -737,7 +737,7 @@ describe("parser", () => {
737737
});
738738

739739
describe("format", () => {
740-
let formatOptions: IOptions = {
740+
const formatOptions: IOptions = {
741741
declaration: {
742742
include: false
743743
},
@@ -764,10 +764,10 @@ describe("parser", () => {
764764
});
765765

766766
describe("typeHandlers", () => {
767-
let typeHandlers: ITypeHandlers = {
767+
const typeHandlers: ITypeHandlers = {
768768
"[object Number]": (val: any) => val + 17
769769
};
770-
let typeHandlersOptions: IOptions = {
770+
const typeHandlersOptions: IOptions = {
771771
declaration: {
772772
include: false
773773
},
@@ -777,7 +777,7 @@ describe("parser", () => {
777777
typeHandlers
778778
};
779779

780-
let typeHandlersWildcard: ITypeHandlers = {
780+
const typeHandlersWildcard: ITypeHandlers = {
781781
"[object Number]": (val: any) => val + 17,
782782
"*": (val: any) => {
783783
if (isString(val)) {
@@ -787,7 +787,7 @@ describe("parser", () => {
787787
}
788788
}
789789
};
790-
let typeHandlersWildcardOptions: IOptions = {
790+
const typeHandlersWildcardOptions: IOptions = {
791791
declaration: {
792792
include: false
793793
},
@@ -826,7 +826,7 @@ describe("parser", () => {
826826
});
827827

828828
describe("valueString", () => {
829-
let valueStringOptions: IOptions = {
829+
const valueStringOptions: IOptions = {
830830
declaration: {
831831
include: false
832832
},
@@ -909,14 +909,14 @@ describe("parser", () => {
909909
});
910910

911911
describe("wrapHandlers", () => {
912-
let wrapHandlers: IWrapHandlers = {
912+
const wrapHandlers: IWrapHandlers = {
913913
"test1": () => "test2",
914914
"test3": (key: string, value: any) =>
915915
"test4" + key + (isSet(value)
916916
? value.values().next().value : value[0]),
917917
"test17": () => null
918918
};
919-
let wrapHandlersOptions: IOptions = {
919+
const wrapHandlersOptions: IOptions = {
920920
declaration: {
921921
include: false
922922
},
@@ -926,14 +926,14 @@ describe("parser", () => {
926926
wrapHandlers
927927
};
928928

929-
let wrapHandlersWildcard: IWrapHandlers = {
929+
const wrapHandlersWildcard: IWrapHandlers = {
930930
"test1": () => "test2",
931931
"test3": (key: string, value: any) =>
932932
"test4" + key + (isSet(value)
933933
? value.values().next().value : value[0]),
934934
"*": () => "test5"
935935
};
936-
let wrapHandlersWildcardOptions: IOptions = {
936+
const wrapHandlersWildcardOptions: IOptions = {
937937
declaration: {
938938
include: false
939939
},

0 commit comments

Comments
 (0)