Repository: FontoXML/fontoxpath
Pull request #497 (2022-06-02 10:52:28)

Allow custom function to return maps

bundlemon[bot]: <!-- bundlemon -->
## BundleMon

  
  
  
  
  
<details open>
<summary>Files updated (2)</summary>

Status | Path | Size | Limits
:------------: | ------------ | :------------: | :------------:
:white_check_mark: | fontoxpath.esm.js<br/> | 81.67KB (+16B +0.02%) | -
:white_check_mark: | fontoxpath.js<br/> | 81.55KB (+16B +0.02%) | -
</details>

  
  


Total files change +32B +0.02%

Final result: :white_check_mark:

[View report in BundleMon website ➡️](https://app.bundlemon.dev/projects/609024effb87a20009a6b096/reports/629896d7ccb4da9798f02bbd)

---
<p align="center"><a href="https://app.bundlemon.dev/projects/609024effb87a20009a6b096/reports?branch=return-type-map&resolution=all" target="_blank" rel="noreferrer noopener">Current branch size history</a> | <a href="https://app.bundlemon.dev/projects/609024effb87a20009a6b096/reports?branch=master&resolution=days" target="_blank" rel="noreferrer noopener">Target branch size history</a></p>
coveralls: 
[![Coverage Status](https://coveralls.io/builds/49671170/badge)](https://coveralls.io/builds/49671170)

Coverage increased (+0.001%) to 91.14% when pulling **070191b3adc7217afac375eec5b9a388a8a72640 on return-type-map** into **6996d9d99a56218a8ae9173aca777425f398b22b on master**.


Patch:
diff --git a/src/expressions/adaptJavaScriptValueToXPathValue.ts b/src/expressions/adaptJavaScriptValueToXPathValue.ts
index 451d2cf8..8a54aace 100644
--- a/src/expressions/adaptJavaScriptValueToXPathValue.ts
+++ b/src/expressions/adaptJavaScriptValueToXPathValue.ts
@@ -171,9 +171,13 @@ function adaptJavaScriptValueToXPath(
 			return createPointerValue(pointer, domFacade);
 		case ValueType.ITEM:
 			return adaptSingleJavaScriptValue(value, domFacade);
+		case ValueType.MAP:
+			return adaptSingleJavaScriptValue(value, domFacade);
 		default:
 			throw new Error(
-				`Values of the type "${type}" can not be adapted to equivalent XPath values.`
+				`Values of the type "${valueTypeToString(
+					type
+				)}" can not be adapted from JavaScript to equivalent XPath values.`
 			);
 	}
 }


Test patch:
diff --git a/test/specs/expressions/adaptJavaScriptValueToXPathValue.tests.ts b/test/specs/expressions/adaptJavaScriptValueToXPathValue.tests.ts
index bb5dca4f..a7830d9f 100644
--- a/test/specs/expressions/adaptJavaScriptValueToXPathValue.tests.ts
+++ b/test/specs/expressions/adaptJavaScriptValueToXPathValue.tests.ts
@@ -268,6 +268,16 @@ describe('adaptJavaScriptValueToSequence', () => {
 			chai.assert(xpathSequence.first().type === ValueType.MAP, 'is a map');
 		});
 
+		it('can automatically convert objects when they are typed as a map', () => {
+			const xpathSequence = adaptJavaScriptValueToSequence(
+				null,
+				{},
+				{ type: ValueType.MAP, mult: SequenceMultiplicity.EXACTLY_ONE }
+			);
+			chai.assert(xpathSequence.isSingleton(), 'is a singleton sequence');
+			chai.assert(xpathSequence.first().type === ValueType.MAP, 'is a map');
+		});
+
 		it('can automatically convert null to the empty sequence', () => {
 			const xpathSequence = adaptJavaScriptValueToSequence(null, null);
 			chai.assert(xpathSequence.isEmpty(), 'is the empty sequence');