Skip to content

Commit

Permalink
feat: support text parameter (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Dec 17, 2020
1 parent deadca7 commit bfad587
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class GoogleFontsHelper {
}

constructURL (): string | false {
const { families, display, subsets } = this.fonts
const { families, display, subsets, text } = this.fonts
const family = convertFamiliesToArray(families ?? {})

if (family.length < 1) {
Expand All @@ -40,6 +40,10 @@ export class GoogleFontsHelper {
query.subset = subset.join(',')
}

if (text) {
query.text = encodeURI(text)
}

return unescape(format({
protocol: 'https',
hostname: 'fonts.googleapis.com',
Expand Down Expand Up @@ -92,6 +96,11 @@ export class GoogleFontsHelper {
result.subsets = subsets.split(',')
}

const text = searchParams.get('text')
if (text) {
result.text = decodeURI(text)
}

return new GoogleFontsHelper(result)
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface GoogleFonts {
families?: Families
display?: string
subsets?: string[] | string
text?: string
}

export interface Families {
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ describe('Google Fonts Helper', () => {
families: { Roboto: true }
}).constructURL()).toEqual('https://fonts.googleapis.com/css2?family=Roboto')

expect(new GoogleFontsHelper({
families: { Roboto: true },
text: 'Foo Bar'
}).constructURL()).toEqual('https://fonts.googleapis.com/css2?family=Roboto&text=Foo%20Bar')

expect(new GoogleFontsHelper({
families: { Roboto: true, Lato: false },
display: 'swap',
Expand Down Expand Up @@ -187,6 +192,13 @@ describe('Google Fonts Helper', () => {
}
})

expect(GoogleFontsHelper.parse('https://fonts.googleapis.com/css2?family=Roboto&text=Foo%20Bar').getFonts()).toStrictEqual({
families: {
Roboto: true
},
text: 'Foo Bar'
})

expect(GoogleFontsHelper.parse('https://fonts.googleapis.com/css?family=Roboto&family=Lato:&display=swap&subset=cyrillic').getFonts()).toStrictEqual({
families: {
Roboto: true
Expand Down

0 comments on commit bfad587

Please sign in to comment.