Skip to content

Commit

Permalink
Merge pull request #73 from jaredwray/adding-in-FumanchuOptions
Browse files Browse the repository at this point in the history
adding in FumanchuOptions
  • Loading branch information
jaredwray authored Nov 1, 2024
2 parents 1b0e040 + 35bda83 commit 6284b71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as HandlebarsLib from 'handlebars';
import { HelperRegistry } from './helper-registry.js';
import { HelperRegistry, HelperFilter } from './helper-registry.js';
import helpersLib from '../helpers/helpers.js';

/**
Expand Down Expand Up @@ -30,11 +30,36 @@ export async function createHandlebars() {
return handlebars;
}

/**
* @typedef {Object} FumanchuCachingOptions
* @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
* format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
* undefined then it will not have a time-to-live.
* @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
* @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0.
* @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
*/
export type FumanchuCachingOptions = {
ttl?: number | string;
useClone?: boolean;
lruSize?: number;
checkInterval?: number;
};

export type FumanchuOptions = {
handlebars?: typeof HandlebarsLib;
helpers?: Record<string, Function>;
name?: string | string[];
include?: HelperFilter[];
exclude?: HelperFilter[];
caching?: boolean | FumanchuCachingOptions;
};

/**
* Will return a Handlebars instance with Fumanchu helpers (experimental)
* @returns {Handlebars} Handlebars instance with helpers
*/
export function fumanchu() {
export function fumanchu(options?: FumanchuOptions) {
const registry = new HelperRegistry();
const handlebars = HandlebarsLib.create();
registry.loadHandlebars(handlebars);
Expand Down
2 changes: 1 addition & 1 deletion test/helper-registry.ts → test/helper-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('HelperRegistry Filter', () => {
category: 'test',
fn: () => 'test',
});
expect(registry.filter({name: 'test1'}).length).toBe(1);
expect(registry.filter({name: 'test1'}).length).toBe(0);
});
test('should filter by name', () => {
const registry = new HelperRegistry();
Expand Down

0 comments on commit 6284b71

Please sign in to comment.