Skip to content

Commit

Permalink
Import with props (#95)
Browse files Browse the repository at this point in the history
* initial commit. all tests pass

* bump version
  • Loading branch information
geoffhendrey authored Jan 6, 2025
1 parent d128b47 commit 534d729
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stated-js",
"version": "0.1.47",
"version": "0.1.48",
"license": "Apache-2.0",
"description": "JSONata embedded in JSON",
"main": "./dist/src/index.js",
Expand Down
9 changes: 5 additions & 4 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,9 @@ export default class TemplateProcessor {

public static NOOP = Symbol('NOOP');

private getImport = (metaInfo: MetaInfo):(templateToImport:string)=>Promise<symbol> => { //we provide the JSON Pointer that targets where the imported content will go
//import the template to the location pointed to by jsonPtr
return async (importMe) => {
private getImport = (metaInfo: MetaInfo):(templateToImport:string, mergeMe?:object)=>Promise<symbol> => { //we provide the JSON Pointer that targets where the imported content will go
//import the template to the location pointed to by jsonPtr. `mergeMe` is like props merges into the imported object before the object is loaded into the template
return async (importMe:any, mergeMe?:object) => {
let resp;
const parsedUrl = this.parseURL(importMe);
if (parsedUrl) { //remote download
Expand Down Expand Up @@ -714,7 +714,8 @@ export default class TemplateProcessor {
if (resp === undefined) {
throw new Error(`Import failed for '${importMe}' at '${metaInfo.jsonPointer__}'`);
}
await this.setContentInTemplate(resp, metaInfo);
const template = mergeMe?{...resp, ...mergeMe}:resp;
await this.setContentInTemplate(template, metaInfo);
return TemplateProcessor.NOOP;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5678,6 +5678,22 @@ test("test fibonacci", async () => {
}
});

test("test import with props", async () => {
const tp = new TemplateProcessor({
a:42,
b: "${$import({'c':'doink','d':'boink', 'e':42},{'e':-1, 'f':'nice props'})}"
});
try {
await tp.initialize();
expect(tp.output).toEqual({
a:42,
b: {'c':'doink','d':'boink', 'e':-1, 'f':'nice props'}
});
} finally {
await tp.close();
}
});




0 comments on commit 534d729

Please sign in to comment.