From 35bda834bec0ab6af34824ebc9d2aa2f14daff83 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Fri, 1 Nov 2024 15:21:37 -0700 Subject: [PATCH] adding in FumanchuOptions --- src/index.ts | 29 +++++++++++++++++-- ...er-registry.ts => helper-registry.test.ts} | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) rename test/{helper-registry.ts => helper-registry.test.ts} (97%) diff --git a/src/index.ts b/src/index.ts index 1f599f0..6f5cc96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; /** @@ -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; + 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); diff --git a/test/helper-registry.ts b/test/helper-registry.test.ts similarity index 97% rename from test/helper-registry.ts rename to test/helper-registry.test.ts index ea79c1e..3a59cbc 100644 --- a/test/helper-registry.ts +++ b/test/helper-registry.test.ts @@ -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();