Skip to content

Commit cf63dc4

Browse files
committed
Use node test module
1 parent 8d26d39 commit cf63dc4

File tree

105 files changed

+1650
-1672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1650
-1672
lines changed

.vscode/launch.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"dependencies": {
3030
"@oozcitak/dom": "^2.0.1",
3131
"@oozcitak/infra": "^2.0.1",
32-
"@oozcitak/util": "^9.0.2",
32+
"@oozcitak/util": "^9.0.4",
3333
"js-yaml": "^4.1.0"
3434
},
3535
"devDependencies": {
@@ -56,6 +56,7 @@
5656
"scripts": {
5757
"test": "tsc && tsx --test --experimental-test-coverage test/**/*test.ts",
5858
"cover": "tsc && tsx --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=./coverage/lcov.info test/**/*test.ts",
59-
"publish-public": "tsc && npm run cover && npm publish --access public"
59+
"publish-public": "tsc && npm run cover && npm publish --access public",
60+
"servedocs": "(cd docs && bundle exec jekyll serve)"
6061
}
6162
}

src/builder/XMLBuilderImpl.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class XMLBuilderImpl implements XMLBuilder {
2727

2828
/**
2929
* Initializes a new instance of `XMLBuilderNodeImpl`.
30-
*
30+
*
3131
* @param domNode - the DOM node to wrap
3232
*/
3333
constructor(domNode: Node) {
@@ -53,19 +53,19 @@ export class XMLBuilderImpl implements XMLBuilder {
5353
p3?: AttributesObject): XMLBuilder {
5454

5555
let namespace: string | null | undefined
56-
let name: string | ExpandObject | undefined
56+
let name: string | ExpandObject | undefined | null
5757
let attributes: AttributesObject | undefined
5858

59-
if (isObject(p1)) {
59+
if (isObject<string>(p1)) {
6060
// ele(obj: ExpandObject)
6161
return new ObjectReader(this._options).parse(this, p1)
62-
} else if (p1 !== null && /^\s*</.test(p1)) {
62+
} else if (isString(p1) && p1 !== null && /^\s*</.test(p1)) {
6363
// parse XML document string
6464
return new XMLReader(this._options).parse(this, p1)
65-
} else if (p1 !== null && /^\s*[\{\[]/.test(p1)) {
65+
} else if (isString(p1) && p1 !== null && /^\s*[\{\[]/.test(p1)) {
6666
// parse JSON string
6767
return new JSONReader(this._options).parse(this, p1)
68-
} else if (p1 !== null && /^(\s*|(#.*)|(%.*))*---/.test(p1)) {
68+
} else if (isString(p1) && p1 !== null && /^(\s*|(#.*)|(%.*))*---/.test(p1)) {
6969
// parse YAML string
7070
return new YAMLReader(this._options).parse(this, p1)
7171
}
@@ -86,7 +86,7 @@ export class XMLBuilderImpl implements XMLBuilder {
8686

8787
[namespace, name] = this._extractNamespace(
8888
sanitizeInput(namespace, this._options.invalidCharReplacement),
89-
sanitizeInput(name, this._options.invalidCharReplacement), true)
89+
sanitizeInput(name as string, this._options.invalidCharReplacement), true)
9090

9191
// inherit namespace from parent
9292
if (namespace === undefined) {
@@ -130,7 +130,7 @@ export class XMLBuilderImpl implements XMLBuilder {
130130
/** @inheritdoc */
131131
att(p1: AttributesObject | string | null, p2?: string, p3?: string): XMLBuilder {
132132

133-
if (isMap(p1) || isObject(p1)) {
133+
if (isMap<string>(p1) || isObject<string>(p1)) {
134134
// att(obj: AttributesObject)
135135
// expand if object
136136
forEachObject(p1, (attName, attValue) => this.att(attName, attValue), this)
@@ -228,7 +228,7 @@ export class XMLBuilderImpl implements XMLBuilder {
228228
throw new Error("Attribute namespace must be a string. " + this._debugInfo())
229229
}
230230

231-
if (isArray(name) || isSet(name)) {
231+
if (isArray<string>(name) || isSet<string>(name)) {
232232
// removeAtt(names: string[])
233233
// removeAtt(namespace: string, names: string[])
234234
forEachArray(name, attName =>
@@ -316,15 +316,15 @@ export class XMLBuilderImpl implements XMLBuilder {
316316
}
317317
}
318318

319-
if (isArray(target) || isSet(target)) {
320-
forEachArray(target, item => {
319+
if (isArray<string>(target) || isSet<string>(target)) {
320+
forEachArray<string>(target, item => {
321321
item += ""
322322
const insIndex = item.indexOf(' ')
323323
const insTarget = (insIndex === -1 ? item : item.substr(0, insIndex))
324324
const insValue = (insIndex === -1 ? '' : item.substr(insIndex + 1))
325325
this.ins(insTarget, insValue)
326326
}, this)
327-
} else if (isMap(target) || isObject(target)) {
327+
} else if (isMap<string>(target) || isObject<string>(target)) {
328328
forEachObject(target, (insTarget, insValue) => this.ins(insTarget, insValue), this)
329329
} else {
330330
const child = this._doc.createProcessingInstruction(
@@ -619,7 +619,7 @@ export class XMLBuilderImpl implements XMLBuilder {
619619
* Gets the next descendant of the given node of the tree rooted at `root`
620620
* in depth-first pre-order. Returns a three-tuple with
621621
* [descendant, descendant_index, descendant_level].
622-
*
622+
*
623623
* @param root - root node of the tree
624624
* @param self - whether to visit the current node along with child nodes
625625
* @param recursive - whether to visit all descendant nodes in tree-order or
@@ -638,7 +638,7 @@ export class XMLBuilderImpl implements XMLBuilder {
638638
* Gets the next descendant of the given node of the tree rooted at `root`
639639
* in depth-first pre-order. Returns a three-tuple with
640640
* [descendant, descendant_index, descendant_level].
641-
*
641+
*
642642
* @param root - root node of the tree
643643
* @param node - current node
644644
* @param recursive - whether to visit all descendant nodes in tree-order or
@@ -675,7 +675,7 @@ export class XMLBuilderImpl implements XMLBuilder {
675675

676676
/**
677677
* Converts the node into its string or object representation.
678-
*
678+
*
679679
* @param options - serialization options
680680
*/
681681
private _serialize(writerOptions: WriterOptions): XMLSerializedValue {
@@ -701,7 +701,7 @@ export class XMLBuilderImpl implements XMLBuilder {
701701

702702
/**
703703
* Extracts a namespace and name from the given string.
704-
*
704+
*
705705
* @param namespace - namespace
706706
* @param name - a string containing both a name and namespace separated by an
707707
* `'@'` character
@@ -732,7 +732,7 @@ export class XMLBuilderImpl implements XMLBuilder {
732732

733733
/**
734734
* Updates the element's namespace.
735-
*
735+
*
736736
* @param ns - new namespace
737737
*/
738738
private _updateNamespace(ns: string | null): void {
@@ -797,7 +797,7 @@ export class XMLBuilderImpl implements XMLBuilder {
797797

798798
/**
799799
* Returns debug information for this node.
800-
*
800+
*
801801
* @param name - node name
802802
*/
803803
protected _debugInfo(name?: string): string {

src/readers/ObjectReader.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ export class ObjectReader extends BaseReader<ExpandObject> {
2323

2424
let lastChild: XMLBuilder | null = null
2525

26-
if (isFunction(obj)) {
26+
if (isFunction<ExpandObject>(obj)) {
2727
// evaluate if function
28-
lastChild = this.parse(node, Function.prototype.apply(obj, this))
29-
} else if (isArray(obj) || isSet(obj)) {
28+
lastChild = this.parse(node, obj.call(this))
29+
} else if (isArray<ExpandObject>(obj) || isSet<ExpandObject>(obj)) {
3030
forEachArray(obj, item => lastChild = this.parse(node, item), this)
31-
} else if (isMap(obj) || isObject(obj)) {
31+
} else if (isMap<ExpandObject>(obj) || isObject<ExpandObject>(obj)) {
3232
// expand if object
33-
forEachObject(obj, (key, val) => {
34-
if (isFunction(val)) {
33+
forEachObject<ExpandObject>(obj, (key, val) => {
34+
if (isFunction<ExpandObject>(val)) {
3535
// evaluate if function
36-
val = Function.prototype.apply(val, this)
36+
val = val.call(this)
3737
}
3838

3939
if (!options.ignoreConverters && key.indexOf(options.convert.att) === 0) {
4040
// assign attributes
4141
if (key === options.convert.att) {
42-
if (isArray(val) || isSet(val)) {
42+
if (isArray<ExpandObject>(val) || isSet<ExpandObject>(val)) {
4343
throw new Error("Invalid attribute: " + val.toString() + ". " + (node as any)._debugInfo())
4444
} else /* if (isMap(val) || isObject(val)) */ {
4545
forEachObject(val, (attrKey, attrVal) => {
@@ -49,30 +49,30 @@ export class ObjectReader extends BaseReader<ExpandObject> {
4949
}
5050
} else {
5151
lastChild = this.attribute(node, undefined,
52-
this.sanitize(key.substr(options.convert.att.length)),
53-
this._decodeAttributeValue(this.sanitize(val))) || lastChild
52+
this.sanitize(key.substring(options.convert.att.length)),
53+
this._decodeAttributeValue(this.sanitize(val?.toString()))) || lastChild
5454
}
5555
} else if (!options.ignoreConverters && key.indexOf(options.convert.text) === 0) {
5656
// text node
5757
if (isMap(val) || isObject(val)) {
5858
// if the key is #text expand child nodes under this node to support mixed content
5959
lastChild = this.parse(node, val)
6060
} else {
61-
lastChild = this.text(node, this._decodeText(this.sanitize(val))) || lastChild
61+
lastChild = this.text(node, this._decodeText(this.sanitize(val?.toString()))) || lastChild
6262
}
6363
} else if (!options.ignoreConverters && key.indexOf(options.convert.cdata) === 0) {
6464
// cdata node
65-
if (isArray(val) || isSet(val)) {
65+
if (isArray<string>(val) || isSet<string>(val)) {
6666
forEachArray(val, item => lastChild = this.cdata(node, this.sanitize(item)) || lastChild, this)
6767
} else {
68-
lastChild = this.cdata(node, this.sanitize(val)) || lastChild
68+
lastChild = this.cdata(node, this.sanitize(val?.toString())) || lastChild
6969
}
7070
} else if (!options.ignoreConverters && key.indexOf(options.convert.comment) === 0) {
7171
// comment node
72-
if (isArray(val) || isSet(val)) {
72+
if (isArray<string>(val) || isSet<string>(val)) {
7373
forEachArray(val, item => lastChild = this.comment(node, this.sanitize(item)) || lastChild, this)
7474
} else {
75-
lastChild = this.comment(node, this.sanitize(val)) || lastChild
75+
lastChild = this.comment(node, this.sanitize(val?.toString())) || lastChild
7676
}
7777
} else if (!options.ignoreConverters && key.indexOf(options.convert.ins) === 0) {
7878
// processing instruction
@@ -81,7 +81,7 @@ export class ObjectReader extends BaseReader<ExpandObject> {
8181
const insTarget = (insIndex === -1 ? val : val.substr(0, insIndex))
8282
const insValue = (insIndex === -1 ? '' : val.substr(insIndex + 1))
8383
lastChild = this.instruction(node, this.sanitize(insTarget), this.sanitize(insValue)) || lastChild
84-
} else if (isArray(val) || isSet(val)) {
84+
} else if (isArray<string>(val) || isSet<string>(val)) {
8585
forEachArray(val, item => {
8686
const insIndex = item.indexOf(' ')
8787
const insTarget = (insIndex === -1 ? item : item.substr(0, insIndex))
@@ -115,12 +115,12 @@ export class ObjectReader extends BaseReader<ExpandObject> {
115115
// expand child nodes under parent
116116
this.parse(parent, val)
117117
}
118-
} else if (val != null && val !== '') {
118+
} else if (val != null && (!isString(val) || val !== '')) {
119119
// leaf element node with a single text node
120120
const parent = this.element(node, undefined, this.sanitize(key))
121121
if (parent) {
122122
lastChild = parent
123-
this.text(parent, this._decodeText(this.sanitize(val)))
123+
this.text(parent, this._decodeText(this.sanitize(val?.toString())))
124124
}
125125
} else {
126126
// leaf element node
@@ -131,7 +131,7 @@ export class ObjectReader extends BaseReader<ExpandObject> {
131131
// skip null and undefined nodes
132132
} else {
133133
// text node
134-
lastChild = this.text(node, this._decodeText(this.sanitize(obj))) || lastChild
134+
lastChild = this.text(node, this._decodeText(this.sanitize(obj?.toString()))) || lastChild
135135
}
136136

137137
return lastChild || node

src/writers/YAMLWriter.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
1616

1717
/**
1818
* Initializes a new instance of `YAMLWriter`.
19-
*
19+
*
2020
* @param builderOptions - XML builder options
2121
* @param writerOptions - serialization options
2222
*/
@@ -42,7 +42,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
4242

4343
/**
4444
* Produces an XML serialization of the given node.
45-
*
45+
*
4646
* @param node - node to serialize
4747
* @param writerOptions - serialization options
4848
*/
@@ -69,7 +69,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
6969

7070
/**
7171
* Produces an XML serialization of the given object.
72-
*
72+
*
7373
* @param obj - object to serialize
7474
* @param options - serialization options
7575
* @param level - depth of the XML tree
@@ -86,7 +86,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
8686
if (!isObject(val)) {
8787
markup += this._val(val) + this._endLine(options)
8888
} else if (isEmpty(val)) {
89-
markup += '""' + this._endLine(options)
89+
markup += '""' + this._endLine(options)
9090
} else {
9191
markup += this._convertObject(val, options, level, true)
9292
}
@@ -99,7 +99,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
9999
} else {
100100
markup += this._beginLine(options, level) + this._key(key)
101101
}
102-
if (!isObject(val)) {
102+
if (!isObject(val) && !isArray(val)) {
103103
markup += ' ' + this._val(val) + this._endLine(options)
104104
} else if (isEmpty(val)) {
105105
markup += ' ""' + this._endLine(options)
@@ -116,7 +116,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
116116
/**
117117
* Produces characters to be prepended to a line of string in pretty-print
118118
* mode.
119-
*
119+
*
120120
* @param options - serialization options
121121
* @param level - current depth of the XML tree
122122
* @param isArray - whether this line is an array item
@@ -134,7 +134,7 @@ export class YAMLWriter extends BaseWriter<YAMLWriterOptions, string> {
134134
/**
135135
* Produces characters to be appended to a line of string in pretty-print
136136
* mode.
137-
*
137+
*
138138
* @param options - serialization options
139139
*/
140140
private _endLine(options: Required<YAMLWriterOptions>): string {

0 commit comments

Comments
 (0)