Skip to content

Commit

Permalink
Merge pull request #66 from jaredwray/adding-in-helper-registry
Browse files Browse the repository at this point in the history
Adding in helper registry
  • Loading branch information
jaredwray authored Oct 29, 2024
2 parents 118b0e3 + eae5189 commit 53bb9fa
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"dependencies": {
"arr-flatten": "^1.1.0",
"array-sort": "^1.0.0",
"cacheable": "^1.8.2",
"create-frame": "^1.0.0",
"define-property": "^2.0.2",
"ent": "^2.2.1",
Expand Down
62 changes: 62 additions & 0 deletions src/helper-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { helpers as dateHelpers } from './helpers/date.js';

export enum HelperRegistryCompatibility {
NODEJS = 'nodejs',
BROWSER = 'browser',
}

export type HelperFilter = {
name?: string;
category?: string;
compatibility?: HelperRegistryCompatibility;
}

export type Helper = {
name: string;
category: string;
compatibility?: HelperRegistryCompatibility;
fn: Function;
}

export class HelperRegistry {
private _helpers: Array<Helper> = [];

constructor() {
this.init();
}

public init() {
// Date
this.registerHelpers(dateHelpers);
}

public register(helper: Helper) {
if (this.has(helper.name)) {
throw new Error(`Helper ${helper.name} already exists.`);
}
this._helpers.push(helper);
}

public registerHelpers(helpers: Array<Helper>) {
helpers.forEach(helper => this.register(helper));
}

public has(name: string): boolean {
return this._helpers.some(helper => helper.name === name);
}

public filter(filter: HelperFilter): Array<Helper> {
return this._helpers.filter(helper => {
return (!filter.name || helper.name === filter.name) &&
(!filter.category || helper.category === filter.category) &&
(!filter.compatibility || helper.compatibility === filter.compatibility);
});
}

public loadHandlebars(handlebars: any) {
this._helpers.forEach(helper => {
console.log(helper);
handlebars.registerHelper(helper.name, helper.fn);
});
}
}
11 changes: 11 additions & 0 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Helper} from '../helper-registry.js';

const year = () => new Date().getFullYear();

export const helpers: Array<Helper> = [
{
name: 'year',
category: 'date',
fn: year,
},
];
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as HandlebarsLib from 'handlebars';
import { HelperRegistry } from './helper-registry.js';
import helpersLib from '../helpers/helpers.js';

/**
Expand Down Expand Up @@ -26,4 +27,11 @@ export async function createHandlebars() {
const helpersFunction = await import('../helpers/helpers.js');
helpersFunction.default({ handlebars: handlebars });
return handlebars;
}

export function fumanchu() {
const registry = new HelperRegistry();
const handlebars = HandlebarsLib.create();
registry.loadHandlebars(handlebars);
return handlebars;
}
8 changes: 7 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var assert = require('assert');
const chai = require('chai');
const expect = chai.expect;
const {createHandlebars} = require('../dist/index.js');
const {createHandlebars, fumanchu} = require('../dist/index.js');

describe('Fumanchu Template Test', function() {
it('should render name correctly', async function() {
Expand Down Expand Up @@ -38,5 +38,11 @@ describe('Fumanchu Template Test', function() {
var fn = handlebars.compile('{{add value 5}}');
assert.equal(fn({value: 5}), '10');
});

it('does fumanchu register year', function() {
var handlebars = fumanchu();
var fn = handlebars.compile('{{year}}');
assert.equal(fn(), new Date().getFullYear().toString());
});
});

3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
},
"include": [
"helpers/**/*.js",
"src/**/*.js",
"src/index.ts"
"src/**/*.ts",
],
"exclude": [
"node_modules",
Expand Down

0 comments on commit 53bb9fa

Please sign in to comment.