Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handlebars (startsWith, endsWith, match) #211

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions src/helpers/handlebars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
15 changes: 15 additions & 0 deletions src/helpers/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading