Skip to content

Commit

Permalink
Test and export templateHelpers
Browse files Browse the repository at this point in the history
Usage:

```javascript
import templateHelpers from 'chordsheetjs';

const {
  isChordLyricsPair,
  lineHasContents,
  isTag,
  isComment,
  stripHTML,
  each,
  when,
  hasTextContents,
  lineClasses,
  paragraphClasses,
  evaluate,
  fontStyleTag,
} = templateHelpers;
```

Resolves #934
  • Loading branch information
martijnversluis committed Aug 31, 2023
1 parent 78602f1 commit 6e1ff35
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/formatter/templates/html_div_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { renderChord } from '../../helpers';

import {
each,
evaluate, fontStyleTag,
evaluate,
fontStyleTag,
isChordLyricsPair,
isComment,
isEvaluatable,
isTag, keep,
isTag,
lineClasses,
lineHasContents,
paragraphClasses,
Expand Down
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ import Ternary from './chord_sheet/chord_pro/ternary';
import TextFormatter from './formatter/text_formatter';
import UltimateGuitarParser from './parser/ultimate_guitar_parser';

import {
isEvaluatable,
isChordLyricsPair,
lineHasContents,
isTag,
isComment,
stripHTML,
each,
when,
hasTextContents,
lineClasses,
paragraphClasses,
evaluate,
fontStyleTag,
} from './template_helpers';

import {
CHORUS,
INDETERMINATE,
Expand Down Expand Up @@ -51,6 +67,22 @@ export { default as Ternary } from './chord_sheet/chord_pro/ternary';
export { default as TextFormatter } from './formatter/text_formatter';
export { default as UltimateGuitarParser } from './parser/ultimate_guitar_parser';

export const templateHelpers = {
isEvaluatable,
isChordLyricsPair,
lineHasContents,
isTag,
isComment,
stripHTML,
each,
when,
hasTextContents,
lineClasses,
paragraphClasses,
evaluate,
fontStyleTag,
};

export {
CHORUS,
INDETERMINATE,
Expand Down Expand Up @@ -87,4 +119,5 @@ export default {
NONE,
TAB,
VERSE,
templateHelpers,
};
18 changes: 3 additions & 15 deletions src/template_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ interface WhenCallback {
(): string;
}

interface KeepCallback {
(_items: any[]): string;
}

export { isEvaluatable } from './utilities';

export const isChordLyricsPair = (item: Item): boolean => item instanceof ChordLyricsPair;
Expand All @@ -44,10 +40,6 @@ export function when(condition: any, callback: WhenCallback): string {
return condition ? callback() : '';
}

export function keep(variables: any[], callback: KeepCallback): string {
return callback.call(null, variables);
}

export const hasTextContents = (line: Line): boolean => (
line.items.some((item) => (
(item instanceof ChordLyricsPair && !isEmptyString(item.lyrics))
Expand Down Expand Up @@ -76,13 +68,9 @@ export const paragraphClasses = (paragraph: Paragraph): string => {
return classes.join(' ');
};

export const evaluate = (item: Evaluatable, metadata: Metadata, configuration: Configuration): string => {
if (!metadata) {
throw new Error('cannot evaluate, metadata is null');
}

return item.evaluate(metadata, configuration.get('metadata.separator'));
};
export const evaluate = (item: Evaluatable, metadata: Metadata, configuration: Configuration): string => (
item.evaluate(metadata, configuration.get('metadata.separator'))
);

export function fontStyleTag(font: Font) {
const cssString = font.toCssString();
Expand Down
1 change: 1 addition & 0 deletions test/default_export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('default export', () => {
'INDETERMINATE',
'VERSE',
'NONE',
'templateHelpers',
].forEach((constantName) => {
it(`contains ${constantName}`, () => {
expect(typeof chordsheetjs[constantName]).not.toEqual('undefined');
Expand Down
3 changes: 2 additions & 1 deletion test/parser/chord_pro_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
CHORUS,
NONE,
VERSE,
TAB, Ternary,
TAB,
Ternary,
} from '../../src';

import '../matchers';
Expand Down
Loading

0 comments on commit 6e1ff35

Please sign in to comment.