Skip to content

Commit f3063eb

Browse files
committed
chore: add export loadHelpers function
1 parent 077ceb3 commit f3063eb

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ npm install handlebars-helpers-ctrf
1212

1313
```javascript
1414
import Handlebars from 'handlebars'
15-
import { registry } from 'handlebars-helpers-ctrf';
15+
import { loadHelpers } from 'handlebars-helpers-ctrf';
16+
1617
// Register all helpers with Handlebars
17-
registry.loadHandlebars(Handlebars);
18+
loadHelpers(Handlebars);
19+
1820

1921
// Now you can use the helpers in your templates
2022
const template = Handlebars.compile('{{append "test" ".html"}}');

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "handlebars-helpers-ctrf",
3-
"version": "0.0.4-next.7",
4-
"description": "",
3+
"version": "0.0.4-next.8",
4+
"description": "A collection of Handlebars helpers for working with Common Test Report Format",
55
"type": "module",
66
"main": "dist/index.js",
77
"types": "dist/index.d.ts",
@@ -31,10 +31,7 @@
3131
"types/",
3232
"README.md"
3333
],
34-
"repository": {
35-
"type": "git",
36-
"url": "https://github.com/ctrf-io/handlebars-helpers-ctrf"
37-
},
34+
"repository": "github:ctrf-io/handlebars-helpers-ctrf",
3835
"homepage": "https://ctrf.io",
3936
"author": "Matthew Thomas",
4037
"license": "MIT",

src/helper-registry.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface HandlebarsInstance {
88
helpers: Record<string, unknown>;
99
}
1010

11+
export type Handlebars = HandlebarsInstance;
12+
1113
/**
1214
* Describes a filter for selecting helpers by name or category.
1315
*/
@@ -93,7 +95,25 @@ export class HelperRegistry {
9395
* Loads all registered helpers into a Handlebars instance.
9496
* @param handlebars - The Handlebars instance.
9597
*/
96-
public loadHandlebars(handlebars: HandlebarsInstance) {
98+
public loadHandlebars(
99+
handlebars:
100+
| HandlebarsInstance
101+
| {
102+
registerHelper: (
103+
name: string,
104+
fn: (...args: unknown[]) => unknown,
105+
) => void;
106+
},
107+
) {
108+
// Type guard to ensure handlebars has the required methods
109+
if (typeof handlebars !== "object" || handlebars === null) {
110+
throw new Error("Handlebars instance must be an object");
111+
}
112+
113+
if (typeof handlebars.registerHelper !== "function") {
114+
throw new Error("Handlebars instance must have a registerHelper method");
115+
}
116+
97117
for (const helper of this._helpers) {
98118
handlebars.registerHelper(helper.name, helper.fn);
99119
}

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ registry.registerHelpers([
4343

4444
registry.loadHandlebars(Handlebars);
4545

46+
/**
47+
* load helpers into Handlebars instance.
48+
* @param handlebarsInstance - The Handlebars instance to load helpers into
49+
*/
50+
export function loadHelpers(handlebarsInstance: {
51+
registerHelper: (name: string, fn: (...args: unknown[]) => unknown) => void;
52+
}) {
53+
registry.loadHandlebars(handlebarsInstance);
54+
}
55+
4656
/**
4757
* Exports the default helper registry and the HelperRegistry class.
4858
*/

0 commit comments

Comments
 (0)