diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ac5f95..9aed4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add ESLint deprecation check (#203) - Add custom code option which is called after content is ready (#231) - Add option to enabled/disable wrapping to support empty lines in HTML (#235) +- Add handlebars (startsWith, endsWith, match) (#211) ## 4.1.0 (2023-07-16) diff --git a/src/helpers/handlebars.test.ts b/src/helpers/handlebars.test.ts index 2eb58ff..824ca69 100644 --- a/src/helpers/handlebars.test.ts +++ b/src/helpers/handlebars.test.ts @@ -43,6 +43,33 @@ describe('Handlebars helpers', () => { expect(handler(['hello', 'world'], 'hello1')).toBeFalsy(); }); + it('StartsWith', () => { + const handler = handlebarsMock.getHelper('startsWith'); + + expect(handler).toBeDefined(); + + expect(handler('hello world', 'hello')).toBeTruthy(); + expect(handler('hello world', 'bye')).toBeFalsy(); + }); + + it('EndsWith', () => { + const handler = handlebarsMock.getHelper('endsWith'); + + expect(handler).toBeDefined(); + + expect(handler('hello world', 'world')).toBeTruthy(); + expect(handler('hello world', 'planet')).toBeFalsy(); + }); + + it('Match', () => { + const handler = handlebarsMock.getHelper('match'); + + expect(handler).toBeDefined(); + + expect(handler('This is a cool plugin.', /(cool)\s(plugin)/)).toBeTruthy(); + expect(handler('This is a cool plugin.', /^This is a plugin$/)).toBeFalsy(); + }); + it('Date', () => { const handler = handlebarsMock.getHelper('date'); diff --git a/src/helpers/handlebars.ts b/src/helpers/handlebars.ts index 0227240..15d44bd 100644 --- a/src/helpers/handlebars.ts +++ b/src/helpers/handlebars.ts @@ -28,6 +28,21 @@ export const registerHelpers = (handlebars: any) => { */ handlebars.registerHelper('contains', (arr: string[], value: string): boolean => arr.indexOf(value) !== -1); + /** + * startsWith + */ + handlebars.registerHelper('startsWith', (left: string, right: string): boolean => left.startsWith(right)); + + /** + * endsWith + */ + handlebars.registerHelper('endsWith', (left: string, right: string): boolean => left.endsWith(right)); + + /** + * match + */ + handlebars.registerHelper('match', (left: string, right: string): boolean => left.match(right) !== null); + /** * Date */