Skip to content

Commit

Permalink
Error out if the text isn't given as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 1, 2021
1 parent 0e7fc87 commit f4a5297
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function subsetFont(
text,
{ targetFormat = fontverter.detectFormat(originalFont), preserveNameIds } = {}
) {
if (typeof text !== 'string') {
throw new Error('The subset text must be given as a string');
}

const [exports, heapu8] = await loadAndInitializeHarfbuzz();

originalFont = await fontverter.convert(originalFont, 'truetype');
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('subset-font', function () {
);
});

describe('when not supplying the subset text as a string', function () {
it('should fail with an error', async function () {
await expect(
() => subsetFont(this.sfntFont, ['f', 'o', 'o']),
'to error with',
'The subset text must be given as a string'
);
});
});

describe('with no targetFormat given', function () {
it('should return the subset as truetype', async function () {
const result = await subsetFont(this.sfntFont, 'abcd');
Expand Down

2 comments on commit f4a5297

@zonyitoo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, since you are using for (const c of texts), it should be completely Ok to let users to pass an Array of strings (codepoints) to your API.

@papandreou
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know, just trying to keep the api surface area small :)

Please sign in to comment.