template-processing-utils
v0.3.0
Published
A TypeScript library for template processing that works in both Node.js and browsers
Readme
Template Processing Utils
A TypeScript library for template processing that works in both Node.js and browsers.
Installation
npm install template-processing-utilsUsage
Handlebars Processor
import { handlebarsProcessor } from 'template-processing-utils';
// Create a processor with optional partials and helpers
const processor = handlebarsProcessor({
partials: {
greeting: 'Hello, {{name}}!',
},
helpers: {
uppercase: (str: string) => str.toUpperCase(),
},
});
// Process a template
const result = await processor.process('{{> greeting}} Welcome to {{uppercase location}}!', {
name: 'John',
location: 'New York',
});
// Result: "Hello, John! Welcome to NEW YORK!"Google Document Processor
import { googleProcessor } from 'template-processing-utils';
import { GoogleDocumentOptions } from 'template-processing-utils';
// Initialize with Google credentials
const options: GoogleDocumentOptions = {
credentials: {
client_id: 'your-client-id',
client_secret: 'your-client-secret',
refresh_token: 'your-refresh-token',
},
};
const processor = googleProcessor(options);
// Create a new document and process variables
const document = await processor.newDocument('Welcome Letter', 'Dear {{name}},\nWelcome to {{company}}!');
await processor.replaceVariablesInBody({
name: 'John',
company: 'Acme Corp',
});
const documentUrl = await document.getDocumentUrl();Browser
Using ES modules:
<script type="module">
import { handlebarsProcessor, googleProcessor } from './node_modules/template-processing-utils/dist/index.js';
// Handlebars example
const handlebars = handlebarsProcessor({
partials: {
greeting: 'Hello, {{name}}!',
},
});
const result = await handlebars.process('{{> greeting}} Welcome to {{location}}!', {
name: 'John',
location: 'New York',
});
// Google Document example
const google = googleProcessor({
credentials: {
client_id: 'your-client-id',
client_secret: 'your-client-secret',
refresh_token: 'your-refresh-token',
},
});
</script>Development
# Install dependencies
npm install
# Build the library
npm run build
# Run tests
npm test
# Run tests with coverage
npm test -- --coverage