Skip to content

Commit

Permalink
fix: cdn sample
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 12, 2024
1 parent b1092c3 commit bdb6d58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
25 changes: 18 additions & 7 deletions public/cdn-sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@ <h2>Output:</h2>
<pre id="output"></pre>
</div>
</div>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@rudderstack/json-template-engine/build/json-template.min.js"
></script>
<script>

<script type="module">
import { JsonTemplateEngine } from 'https://cdn.jsdelivr.net/npm/@rudderstack/json-template-engine/build/json-template.min.js';
const template = document.getElementById('template');
const data = document.getElementById('data');
const output = document.getElementById('output');
const result = JsonTemplateEngine.evaluateAsSync(template.value, {}, JSON.parse(data.value));
output.innerText = JSON.stringify(result, null, 2);
function updateOutput() {
try {
const result = JsonTemplateEngine.evaluateAsSync(template.value, {}, JSON.parse(data.value));
output.innerText = JSON.stringify(result, null, 2);
} catch (error) {
output.innerText = error.message;
}
}
template.addEventListener('input', () => {
updateOutput()
});
data.addEventListener('input', () => {
updateOutput()
});
updateOutput();
</script>
</body>
</html>
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ engine.evaluate({ name: 'World' }); // => 'Hello World'
```

### Use CDN directly in the browser
URL: https://cdn.jsdelivr.net/npm/@rudderstack/json-template-engine/build/json-template.min.js
URL: https://cdn.jsdelivr.net/npm/@rudderstack/json-template-engine/build/json-template.min.js
```html
<script type="module">
import { JsonTemplateEngine } from 'https://cdn.jsdelivr.net/npm/@rudderstack/json-template-engine/build/json-template.min.js';
const engine = JsonTemplateEngine.createAsSync(`'Hello ' + .name`);
engine.evaluate({ name: 'World' });
</script>
```

Refer this [example](public/cdn-sample.html) for more details.

Expand Down
2 changes: 1 addition & 1 deletion src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class JsonTemplateTranslator {
const exprCode = this.translateExpr(this.expr, dest, ctx);
const functions = Object.values(this.standardFunctions);
if (functions.length > 0) {
code.push(functions.join('').replaceAll(/\s+/g, ' '));
code.push(functions.join('').replace(/\s+/g, ' '));
}
code.push(`let ${dest};`);
code.push(this.vars.map((elm) => `let ${elm};`).join(''));
Expand Down

0 comments on commit bdb6d58

Please sign in to comment.