Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jempl",
"version": "1.0.0",
"description": "A JSON templating engine with conditionals, loops, and custom functions",
"description": "A JSON templating engine with conditionals, loops, and user-defined custom functions",
"main": "src/index.js",
"types": "types/index.d.ts",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion spec/render/renderErrors.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ in:
- "${unknownFunc()}"
- {}
- functions: {}
throws: "Render Error: Unknown function 'unknownFunc' (now)"
throws: "Render Error: Unknown function 'unknownFunc' (no custom functions provided)"

---
case: unknown function with available functions
Expand Down
3 changes: 0 additions & 3 deletions src/functions.js

This file was deleted.

7 changes: 5 additions & 2 deletions src/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import { parseValue } from "./utils.js";
* });
*
* @example
* // Template with custom function
* // Template with custom functions
* const ast = parse(
* { timestamp: "${formatDate(now())}" },
* { functions: { formatDate: (date) => new Date(date).toISOString() } }
* { functions: {
* formatDate: (date) => new Date(date).toISOString(),
* now: () => Date.now()
* }}
* );
*/
const parse = (template, options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/parse/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export const parsePathReference = (expr) => {
};

/**
* Parses a variable expression like ${name} or ${user.profile.name} or function calls like ${now()}
* Parses a variable expression like ${name} or ${user.profile.name} or function calls like ${customFunction()}
* @param {string} expr - The expression without ${ and }
* @param {Object} functions - Available functions for validation
* @returns {Object} Variable or Function node
Expand Down
10 changes: 3 additions & 7 deletions src/parseAndRender.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import parse from "./parse/index.js";
import render from "./render.js";
import * as defaultFunctions from "./functions.js";

/**
* Convenience function that parses a template and renders it with data in one step
Expand Down Expand Up @@ -47,21 +46,18 @@ import * as defaultFunctions from "./functions.js";
const parseAndRender = (template, data, options = {}) => {
const { functions = {}, partials = {} } = options;

// Merge default functions with custom functions
const allFunctions = { ...defaultFunctions, ...functions };

// Parse the template into an AST
const ast = parse(template, { functions: allFunctions });
const ast = parse(template, { functions });

// Parse all partials into ASTs
const parsedPartials = {};
for (const [name, partialTemplate] of Object.entries(partials)) {
parsedPartials[name] = parse(partialTemplate, { functions: allFunctions });
parsedPartials[name] = parse(partialTemplate, { functions });
}

// Render the AST with the data and partials
return render(ast, data, {
functions: allFunctions,
functions,
partials: parsedPartials,
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
*
* @example
* // Render with custom functions
* const ast = parse({ timestamp: "${now()}" });
* const ast = parse({ timestamp: "${now()}" }, { functions: { now: () => Date.now() } });
* const result = render(ast, {}, { now: () => Date.now() });
* // result: { timestamp: 1234567890123 }
*/
Expand Down